taucmdr.cf.objects module

TAU Common Framework (CF) common objects.

class taucmdr.cf.objects.KeyedRecord[source]

Bases: object

Data 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 a KeyedRecord subclass 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 all()[source]

Iterate over class instances.

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 keys()[source]

Get the name of the key field in all instances.

Returns:All instance keys.
Return type:list
class taucmdr.cf.objects.KeyedRecordCreator[source]

Bases: type

Metaclass to create a new KeyedRecord instance.

Change object creation procedure so that only one instance of a KeyedRecord exists 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

__call__(*args, **kwargs)[source]

Create the new instance.

static __new__(mcs, name, bases, dct)[source]

Set __instances__ attribute as soon as the class is created.

We do this in __new__ so that each subclass has its own __instances__ attribute. This is how we keep subclass instances from being listed among their base class instances.

class taucmdr.cf.objects.TrackedInstance[source]

Bases: object

Base class for classes that need to keep track of their instances.

Each subclass tracks of its own instances separately. Unliked KeyedRecordCreator there 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
static __new__(*args, **kwargs)[source]

Ensure that __instances__ is set and track new instances.

classmethod all()[source]

Iterate over class instances.