Showing posts with label primitive. Show all posts
Showing posts with label primitive. Show all posts

Tuesday, September 25, 2012

Simplicity Through Primitiveness

Software development craftsmanship involves seeking simplicity. Not only because it's an art form, but because it's practical to do so. Simplicity directly translates into software with fewer moving parts. Fewer moving parts, in turn, mean less risk. I can go on and on about how great simplicity is and how we should all try to reach the simplest possible solutions. But most programmers know what simplicity is, and have grown to appreciate it for any number of reasons. It's generally thought of as a positive side-effect of writing good code. Or the inverse — simplicity is a side-effect of good code.

Monday, February 9, 2009

Testing types with boduch

The boduch Python library provides a simple type testing utility function that allows truth tests for both primitive and user-defined data types. Python does offer built-in type testing utilities, but there is currently no unification between user-defined classes and primitive types. Here is an example of how to use the is_type() function.
#boduch type testing example.

from boduch.type import is_type

class MyBaseType:
pass

class MyType(MyBaseType):
pass

if __name__=="__main__":
print "Testing string type."
print is_type("my string", "str")
print "Testing boolean type."
print is_type(False, "boolean")
print "Testing a class type."
print is_type(MyType(), "MyType")
print "Testing for a base class."
print is_type(MyType(), "MyBaseType")
As you can see, we can significantly cut down the number of lines required for type testing with a single function. Also note, is_type() can also test if the specified instance belongs in an inheritance lattice, as demonstrated in this example.