paint-brush
Use Dynamic Classes to Debug in Pythonby@giwyni
454 reads
454 reads

Use Dynamic Classes to Debug in Python

by GIWYNIMay 1st, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Dynamic classes in Python are well described in this link:http://jelly.codes/articles/python-dynamically-creating-classes. The class is a convenient feature in Python 3 to create classes and instantiate them on the fly. Simulation of large systems by a small class (or dynamic class) cuts both the complexity and time in developing tests. The '# usage shown below' section calculates the stimulusCheckAmt to illustrate the use of the dynamic class to test the change.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Use Dynamic Classes to Debug in Python
GIWYNI HackerNoon profile picture

Oh! If all your code worked as it was supposed to.. Of-course you just made a tiny change and the whole system came crashing down! The existing system seems almost alive, and resists any code change by crashing! Quite often, developers declare a truce in the form of a 'code-freeze'! After a deep breath (perhaps several weeks..) the code-freeze is lifted, and the battle is renewed.

This is generally the reality. This article is specific to python and describes a technique to create a test harness (or test setup). The situation is that while the changed code may be small, it is part of a much larger system. There is no easy way to keep (and maintain) a test environment and even then it is much harder to generate the set of conditions in the larger system to effectively test the changed code.

Consider an imaginary code change to a large system. For Example: You have to make the change to issue the Covid 19 stimulus checks. This change has to be incorporated into the much larger tax system.

Based on a few conditions in the tax system (income, tax-status, name, address etc.) the change made will print the stimulus checks. A way to tackle testing the change is to simulate the large tax system as an Object with attributes and methods that return the conditions desired for testing the change. The diagram below illustrates this:

Simulating a huge unwieldy system by a small dynamic class is a giant leap in simplifying testing.

A small class would be fine as well, so why dynamic? The answer is the syntactic sugar that makes declaration/creation of dynamic classes just a few lines of code in Python.

Here is the definition of the dynamic class that returns values for income, address and tax_status that refer to the picture above:

#coded in Python 3.8 
def getAddress(St):
    return f'99 Lombardy St, {St}'
def getTaxStatus(income):
    if income<50000: return "low"
    else: return "high"

dummyTaxSystem=type('dummyTaxSystem', (object,),
            {'income':49500,
                    'getAddress':lambda self:getAddress('CA'),
                    'getTaxStatus':lambda self: getTaxStatus(self.income)
            }
        ) #creates class dummyTaxSystem

#Usage shown below: 

X=dummyTaxSystem()
stimulusCheckAmt = 10000 if X.getTaxStatus() == "high" else 20000
print(f' Income: {X.income}; Address: {X.getAddress()}; stimulusAmt: {stimulusCheckAmt}; TaxStatus: {X.getTaxStatus()} ')

The '

dummyTaxSystem=type
..' line above creates the 'dymmyTaxSystem' class which has attributes (income) and methods (getAddress, getTaxStatus).

The '#usage shown below' section calculates the stimulusCheckAmt to illustrate the use of the dynamic class.

The dynamic class is a convenient feature in Python 3 to create classes and instantiate them on the fly.

Use of this to simulate complex systems as a simple class, allows tests to be developed quickly.

Dynamic classes in Python are well described in this link:

http://jelly.codes/articles/python-dynamically-creating-classes

Testing and Debugging consume most of the development time. Further, experience tells you that this time is on the order of all the code in the system, and not just on the small piece that was changed. Therefore simulation of large systems by a small class (or dynamic class) cuts both the complexity and time in developing tests.