/usr/share/pyshared/PyTrilinos/PropertyBase.py is in python-pytrilinos 10.4.0.dfsg-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | # -*- python -*-
class PropertyBase(object):
"""
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'
>>>
"""
def __init__(self, **kwargs):
# Check for existence of self.props
if not hasattr(self, 'props'):
raise AttributeError, \
"\n'%s', derived from PropertyBase does not have a props\n" \
"attribute, which should be implimented as a static member\n" \
"of '%s', whose keys are attribute names and\n" \
"whose values are types." % (self.__class__.__name__,
self.__class__.__name__)
# Initialize the props values
self.initializePropValues()
# Fill properties with intitial values of None
for name in self.props:
self.__dict__[name] = None
# If defaults is an attribute, use it to set the default values
if hasattr(self, 'defaults'):
for name in self.defaults:
self.__setattr__(name, self.defaults[name])
# Fill properties specified by keyword arguments, while performing type
# checks
for name in kwargs:
self.__setattr__(name, kwargs[name])
def initializePropValues(self):
# Loop over the property specifications and convert each scalar to a
# one-tuple
for name in self.props:
propSpec = self.props[name]
if isinstance(propSpec, (tuple, list)):
if len(propSpec) == 0:
self.props[name] = (propSpec,)
else:
self.props[name] = (propSpec,)
def __setattr__(self, name, value):
# For illegal property names, raise an exception
if name not in self.props:
raise AttributeError, \
"'%s' is not a valid attribute name" % name
# For legal value types, assign the property value to the named class
# attribute
propSpecs = self.props[name]
for propSpec in propSpecs:
if isinstance(propSpec, type):
if isinstance(value, propSpec):
self.__dict__[name] = value
break
elif value == propSpec:
self.__dict__[name] = value
break
# For illegal value types, raise an exception
else:
if len(propSpecs) == 1 and isinstance(propSpecs[0], type):
raise TypeError, \
"For attribute '%s', value should be of %s.\n" \
"Got attribute of %s" % (name, propSpecs[0], type(value))
else:
raise ValueError, \
"For attribute '%s', value '%s'\n" \
"and type %s is not among the enumerated values:\n" \
"%s" % (name, str(value), type(value), str(propSpecs))
def __getattr__(self, name):
# For illegal property names, raise an exception
if name not in self.props:
raise AttributeError, \
"'%s' is not a valid attribute name" % name
# Return the named property value
return self.__dict__[name]
def __str__(self):
# Build a human readable string representation
result = ''
for name in self.props:
attribute = self.__getattr__(name)
if attribute is self: attribute = 'self reference'
result += name + ' = ' + repr(attribute) + "\n"
if len(result): result = result[:-1]
return result
def __repr__(self):
# Build an evaluatable string representation
result = ''
for name in self.props:
attribute = self.__getattr__(name)
if attribute is self: attribute = 'self reference'
if attribute is not None:
result += name + '=' + repr(attribute) + ", "
if len(result) > 2: result = result[:-2]
return self.__class__.__name__ + "(" + result + ")"
|