bronx.patterns.getbytag¶
GetByTag does not provide any “official” design pattern but we consider it to be somehow an extension of the Singleton pattern.
The main objective is to manage and keep alive a collection of objects that are identified by a unique tag. For a given tag, only one object can be created. If one attempts to create an object with an already existing tag, the constructor will return the previously created object that matches the tag.
Basic example (see GetByTag class documentation for more advanced features):
>>> class A(GetByTag):
... def __init__(self, data=None):
... self.data = data
>>> class A1(A):
... # A1 and A will share the same list of tags...
... _tag_topcls = False
>>> class B(GetByTag):
... def __init__(self, data=None):
... self.data = data
>>> a = A(data=1)
>>> print(a.tag)
default
>>> b = B(data=1)
>>> print(b.tag)
default
>>> b is not a
True
>>> a1 = A1(data=10)
>>> print(a1.tag)
default
>>> a1 is a # Chocking? No... since a and a1 have been created using the same default tag.
True
# A new object of tag 'toto' can be created as follows:
>>> t = A1('toto')
>>> t is not a
True
>>> tbis = A1(tag='toto')
>>> t is tbis
True
Classes¶
- class bronx.patterns.getbytag.GetByTag(*args, **kw)[source]¶
Bases:
objectUtility to retrieve a new/existing object by a special argument named
tag. If an object had already been created with that tag, return this object.Optionally,
GetByTagcan manage the active/passive state of the created objects. It’s done using the notion of focus. Focus can be set, caught or checked using theset_focus()class method, thecatch_focus()method and thehas_focus()method. Some actions can be launched when focus is gained or lost simply by subclassing thefocus_gain_allow(),focus_gain_hook()andfocus_loose_hook()methods.Some class variables may have an impact on GetByTag behaviour:
_tag_default: Sets the defaulttag(if thetagattribute is omitted when calling the constructor, the_tag_defaultstring will be used._tag_implicit_new: If set toFalse, unlessnew=Trueis specified when calling the constructor, it won’t be allowed to create new objects (a RuntimeError exception will be thrown)._tag_topcls: if set toFalse, the tags list will be shared with the parent class (and possibly other siblings). Thetag_classes()class method allows to retrieve the list of Classes sharing the same list of tags
- focus_gain_allow()[source]¶
This method is called on the target object prior to any focus change.
It might be useful if one wants to perform checks and raise an exception.
- has_focus(select='default')[source]¶
Return a boolean value on equality of current tag and focus tag.
- classmethod set_focus(obj, select='default')[source]¶
Define a new tag value for the focus in the scope of the
selectvalue.
- property tag¶
The current object’s tag.
- classmethod tag_check(tag)[source]¶
Check if the tag is in list of actual keys of the objects instanciated.
- classmethod tag_classes()[source]¶
Return a list of current classes that have been registered with the same GetByTag root.
- classmethod tag_clear()[source]¶
Clear all internal information about objects and focus for that class.
- classmethod tag_focus(select='default')[source]¶
Return the tag value of the actual object with focus according to the
selectvalue.
- classmethod tag_items()[source]¶
Proxy to the
itemsmethod of the internal dictionary table of objects.