taucmdr.cf.objects module¶
TAU Common Framework (CF) common objects.
-
class
taucmdr.cf.objects.KeyedRecord[source]¶ Bases:
objectData record with a unique key.
Subclasses must declare a
__key__member defining the attribute to be used as the key. The first parameter to __init__ must be the key value for the new instances. Only one instance of aKeyedRecordsubclass exists per key. Calling the constructor with a previously used key will return the existing instance, unmodified.Example:
class Foo(KeyedRecord): def __init__(self, a): self.a = a potato = Foo('vegtable') carrot = Foo('vegtable') steak = Foo('meat') for inst in Foo.all(): print inst.a >>> vegetable >>> meat potato is carrot >>> True carrot is steak >>> False
-
classmethod
find(key)[source]¶ Find an instance.
Parameters: key – An instance key value. Raises: KeyError– No instance has this key value.Returns: The instance with the matching key value. Return type: KeyedRecord
-
classmethod
-
class
taucmdr.cf.objects.KeyedRecordCreator[source]¶ Bases:
typeMetaclass to create a new
KeyedRecordinstance.Change object creation procedure so that only one instance of a
KeyedRecordexists for a given key argument. Overriding__new__would be less creepy, but then we can’t prevent__init__from being called on the returned class instance, i.e. the instance returned is reinitialized every time it is retrieved. This metaclass guarantees we call__new__and__init__only once per class instance.To learn more about metaclasses: * http://docs.python.org/2/reference/datamodel.html * http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python * http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python
-
class
taucmdr.cf.objects.TrackedInstance[source]¶ Bases:
objectBase class for classes that need to keep track of their instances.
Each subclass tracks of its own instances separately. Unliked
KeyedRecordCreatorthere is no restriction on the value of the class instance attributes.Example:
class Foo(TrackedInstance): def __init__(self, x): self.x = x class Bar(Foo): def __init__(self, x, y): super(Bar,self).__init__(x) self.y = y fish = Foo('haddock') chips = Foo('potatoes') fries = Foo('potatoes') drink = Bar('water', 'sugar') for inst in Foo.all(): print inst.x >>> haddock >>> potatoes >>> potatoes for inst in Bar.all(): print inst.x >>> water
