PropertyBase is a base class for simple property classes that have a
restricted set of property names and type-checked property values. To
define a property class, do the following:
1. Define a class that inherits from PropertyBase
2. Add a static data member named 'props', which is a dictionary. Its
keys should be valid property names, and its values should specify the
type of the corresponding property. This type specification can be a
type object, or a sequence of objects which define an enumeration.
3. Optionally define a static data member named 'defaults', with all or a
subset of the 'props' keys, and default values for those properties.
Keys in 'defaults' but not in 'props' will cause an AttributeError
exception to be raised, and values in 'defaults' that are of the wrong
type will cause a TypeError exception to be raised. Properties not
specified in 'defaults' will default to None.
4. Define the derived constructor such that it calls the base constructor:
def __init__(self, **kwargs):
PropertyBase.__init__(self, **kwargs)
And that is it. As an example, if we wanted a class that kept track of the
name, author, number of pages and price of a book:
>>> class BookProperties(PropertyBase):
... props = {'name':str, 'author':str, 'pages':int, 'price':float}
... defaults = {'price' : 9.99 }
... def __init__(self, **kwargs):
... PropertyBase.__init__(self, **kwargs)
...
>>> bookProps = BookProperties(name='A Tale of Two Cities')
>>> bookProps.author = 'Charles Dickens'
>>> print bookProps
price = 9.9900000000000002
pages = None
name = 'A Tale of Two Cities'
author = 'Charles Dickens'
>>>