fandango.dicts module¶
Contents
Description¶
## file : dicts.py ## ## description : see below ## ## project : Tango Control System ## ## $Author: Sergi Rubio Manrique, srubio@cells.es $ ## ## $Revision: 2008 $ ## ## copyleft : ALBA Synchrotron Controls Section, CELLS ## Bellaterra ## Spain ## ############################################################################# ## ## This file is part of Tango Control System ## ## Tango Control System is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License as published ## by the Free Software Foundation; either version 3 of the License, or ## (at your option) any later version. ## ## Tango Control System is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, see <http://www.gnu.org/licenses/>. ########################################################################### @package dicts
Some extensions to python dictionary ThreadDict: Thread safe dictionary with redefinable read/write methods and a backgroud thread for hardware update. defaultdict_fromkey: Creates a dictionary with a default_factory function that creates new elements using key as argument. CaselessDict: caseless dictionary CaselessDefaultDict: a join venture between caseless and default dict from key
@deprecated @note see in tau.core.utils.containers
by Sergi Rubio, srubio@cells.es, 2008
Classes¶
Enumeration¶
- class fandango.dicts.Enumeration(name, enumList)[source]¶
@DEPRECATED: Use python Enum type instead!
Enumeration class intended to provide the ‘enum’ feature present in many programming languages. Usage: car = ThingWithType(fruit.Lemon) print whatkind(fruit.type, Lemon) bug = ThingWithType(Insect.BEETLE) print whatkind(bug.type, Insect)
Notice that car’s and bug’s attributes don’t include any of the enum machinery, because that machinery is all CLASS attributes and not INSTANCE attributes. So you can generate thousands of cars and bugs with reckless abandon, never worrying that time or memory will be wasted on redundant copies of the enum stuff.
print car.__dict__ print bug.__dict__ pprint.pprint(Cars.__dict__) pprint.pprint(Insect.__dict__)
SortedDict¶
defaultdict_fromkey¶
- class fandango.dicts.defaultdict_fromkey[source]¶
Creates a dictionary with a default_factory function that creates new elements using key as argument. Usage : new_dict = defaultdict_fromkey(method); where method like (lambda key: return new_obj(key)) Each time that new_dict[key] is called with a key that doesn’t exist, method(key) is used to create the value Copied from PyAlarm device server @deprecated now in tau.core.utils.containers
ReversibleDict¶
- class fandango.dicts.ReversibleDict(table=None, subset=None, index=None, level=0, sorted=False, trace=False)[source]¶
- Dictionary that searches in both directions within a list of tuples acting like a nested dictionary.
- Negative indexes and levels are used to reverse the direction of key,values retrieval (i>=0: left-to-right; i<0: rigth-to-left)
- Both directions are not exclusive!!! ... search direction is managed by the tuple index sign (+/-)
- The _level is always positive, and could relate to both sides of the tuple [+level,-(level-1)][i<0]
- When no sublevel is entered, keys() returns both sides of each tuple
- Due to this behaviour may be: len(self) < len(self.items())
- It inherits from dictionary just to be recognized by isinstance!
- keys()[x] != values()[x] ... use .items() or iteritems() when you need to match it
- There’s no check for duplicate keys, if there’s a duplicate key then the first match is returned
:TODO : A nice search algorithm in sorted tables would speed up retrieving a lot
- dict methods implemeted:
- __contains__,__delitem__,__getitem__,__iter__,__len__,__repr__,__setitem__,__str__ get,has_key,items,iteritems,iterkeys,itervalues,keys,pop,values
NotImplemented: clear,copy,fromkeys,setdefault,update,popitem
- iterlines()[source]¶
Instead of returning key,value pairs it returns a tuple with self.depth() values
- keysets(key=None)[source]¶
It returns a dictionary of {key:[index set]} at actual level. The sign +/- of the index refers to left-to-right/right-to-left order The level/direcition is not initialized until a key is found by this method.
- line(i)[source]¶
It returns an arranged tuple slice of the selected index of the table :param i: it must be the RAW (positive or negative) index of the line
DefaultThreadDict¶
- class fandango.dicts.DefaultThreadDict(other=None, default_factory=None, read_method=None, write_method=None, timewait=0.1, threaded=True)[source]¶
- a join venture between thread and default dict
- This class merges the two previous ones.
@deprecated now in tau.core.utils.containers @todo This two classes are not yet well integrated ... the way a new key is added to the dict must be rewritten explicitly.
ThreadDict¶
- class fandango.dicts.ThreadDict(other=None, read_method=None, write_method=None, timewait=0.1, threaded=True, trace=False)[source]¶
Thread safe dictionary with redefinable read/write methods and a backgroud thread for hardware update. All methods are thread-safe using @self_lock decorator.
NOTE: any method decorated in this way CANNOT call other decorated methods!All values of the dictionary will be automatically updated in a separate Thread using read_method provided. Any value overwritten in the dict should launch the write_method.
- Briefing:
- a[2] equals to a[2]=read_method(2) a[2]=1 equals to a[2]=write_method(2,1)
- threadkeys():
- returns the list of keys that are being automatically updated in a background thread
- append(key,value=None,period=3000):
- adds a new key to the dictionary and to the list of keys to be updated
- __setitem__(key,value):tdict[key]=value
- will also add a new key to the dictionary, but this key value will not be automatically updated.
timewait is a pause inserted between readings
- If read_method defined:
- If threaded: the Thread inserts data in the dictionary, __getitem__ retrieves this data else: __getitem__ directly executes read_method
else: __getitem__ as a normal thread-safe dictionary If write_method defined:
any __setitem__ call executes the write_method (dictionary is no affected)else: __setitem__ as a normal thread-safe dictionary
@deprecated now in tau.core.utils.containers
CaselessSortedDict¶
raw autodoc¶
- class fandango.dicts.CaselessDefaultDict[source]
Bases: fandango.dicts.defaultdict_fromkey, fandango.dicts.CaselessDict
a join venture between caseless and defaultdict_fromkey This class merges the two previous ones. This declaration equals to:
CaselessDefaultDict = type(‘CaselessDefaultType’,(CaselessDict,defaultdict_fromkey),{})
- class fandango.dicts.CaselessDict(other=None)[source]
Bases: dict
Dictionary with caseless key resolution Copied from tau.core.utils.CaselessDict @deprecated now in tau.core.utils.containers
- class fandango.dicts.CaselessList[source]
Bases: list
Python list with caseless index,contains,remove methods
- class fandango.dicts.CaselessSortedDict(other=None)[source]
Bases: fandango.dicts.SortedDict, fandango.dicts.CaselessDict
This class implements a dictionary that returns keys in the same order they were inserted.
- pop(k, d=None)[source]
Removes key and returns its (self[key] or d or None)
- sort(key)[source]
This method modifies the sorting of the dictionary overriding the existing sort key. :param key: it can be a sequence containing all the keys already existing in the dictionary
or a callable providing a sorting key algorithm.
- class fandango.dicts.DefaultThreadDict(other=None, default_factory=None, read_method=None, write_method=None, timewait=0.1, threaded=True)[source]
Bases: fandango.dicts.defaultdict_fromkey, fandango.dicts.ThreadDict
- a join venture between thread and default dict
- This class merges the two previous ones.
@deprecated now in tau.core.utils.containers @todo This two classes are not yet well integrated ... the way a new key is added to the dict must be rewritten explicitly.
- exception fandango.dicts.EnumException[source]
Bases: exceptions.Exception
- class fandango.dicts.Enumeration(name, enumList)[source]
@DEPRECATED: Use python Enum type instead!
Enumeration class intended to provide the ‘enum’ feature present in many programming languages. Usage: car = ThingWithType(fruit.Lemon) print whatkind(fruit.type, Lemon) bug = ThingWithType(Insect.BEETLE) print whatkind(bug.type, Insect)
Notice that car’s and bug’s attributes don’t include any of the enum machinery, because that machinery is all CLASS attributes and not INSTANCE attributes. So you can generate thousands of cars and bugs with reckless abandon, never worrying that time or memory will be wasted on redundant copies of the enum stuff.
print car.__dict__ print bug.__dict__ pprint.pprint(Cars.__dict__) pprint.pprint(Insect.__dict__)
- class fandango.dicts.ReversibleDict(table=None, subset=None, index=None, level=0, sorted=False, trace=False)[source]
Bases: object
- Dictionary that searches in both directions within a list of tuples acting like a nested dictionary.
- Negative indexes and levels are used to reverse the direction of key,values retrieval (i>=0: left-to-right; i<0: rigth-to-left)
- Both directions are not exclusive!!! ... search direction is managed by the tuple index sign (+/-)
- The _level is always positive, and could relate to both sides of the tuple [+level,-(level-1)][i<0]
- When no sublevel is entered, keys() returns both sides of each tuple
- Due to this behaviour may be: len(self) < len(self.items())
- It inherits from dictionary just to be recognized by isinstance!
- keys()[x] != values()[x] ... use .items() or iteritems() when you need to match it
- There’s no check for duplicate keys, if there’s a duplicate key then the first match is returned
:TODO : A nice search algorithm in sorted tables would speed up retrieving a lot
- dict methods implemeted:
- __contains__,__delitem__,__getitem__,__iter__,__len__,__repr__,__setitem__,__str__ get,has_key,items,iteritems,iterkeys,itervalues,keys,pop,values
NotImplemented: clear,copy,fromkeys,setdefault,update,popitem
- DEFAULT = None¶
- SORTKEY(s)¶
- get(*keys)[source]
Arguments are keys separated by commas, it is a recursive call to __getitem__
- has_key(key)[source]
Implemented separately of __getitem__ to be more efficient.
- iteritems()[source]
returns key,value pairs at self.level()
- iterlines()[source]
Instead of returning key,value pairs it returns a tuple with self.depth() values
- itervalues()[source]
It returns values for actual keys
- keysets(key=None)[source]
It returns a dictionary of {key:[index set]} at actual level. The sign +/- of the index refers to left-to-right/right-to-left order The level/direcition is not initialized until a key is found by this method.
- level(i=None)[source]
The direction depends on the sign of the tuple index
- line(i)[source]
It returns an arranged tuple slice of the selected index of the table :param i: it must be the RAW (positive or negative) index of the line
- lines()[source]
Instead of returning key,value pairs it returns a tuple with self.depth() values
- prune(filters=[])[source]
This method should do a cleanup of all repeated key-chains in both directions (or delete anything matched by filters)
- range(full=True)[source]
And appropiated iterator to check all lines in the table
- set(*keys)[source]
Arguments are values separated by commas, it is a recursive call to __setitem__
- sort(update=False)[source]
creates indexes of the keys at each level using the self.SORTKEY method for each level there’s a dictionary {SORTKEY(s):[lines where SORTKEY(key)==SORTKEY(s)]} SORTKEY is used instead of key to improve retrieve times.
- class fandango.dicts.SortedDict(other=None)[source]
Bases: dict
This class implements a dictionary that returns keys in the same order they were inserted.
- pop(k, d=None)[source]
Removes key and returns its (self[key] or d or None)
- popitem()[source]
Removes and returns last key,value pair
- sort(key)[source]
This method modifies the sorting of the dictionary overriding the existing sort key. :param key: it can be a sequence containing all the keys already existing in the dictionary
or a callable providing a sorting key algorithm.
- class fandango.dicts.ThreadDict(other=None, read_method=None, write_method=None, timewait=0.1, threaded=True, trace=False)[source]
Bases: dict
Thread safe dictionary with redefinable read/write methods and a backgroud thread for hardware update. All methods are thread-safe using @self_lock decorator.
NOTE: any method decorated in this way CANNOT call other decorated methods!All values of the dictionary will be automatically updated in a separate Thread using read_method provided. Any value overwritten in the dict should launch the write_method.
- Briefing:
- a[2] equals to a[2]=read_method(2) a[2]=1 equals to a[2]=write_method(2,1)
- threadkeys():
- returns the list of keys that are being automatically updated in a background thread
- append(key,value=None,period=3000):
- adds a new key to the dictionary and to the list of keys to be updated
- __setitem__(key,value):tdict[key]=value
- will also add a new key to the dictionary, but this key value will not be automatically updated.
timewait is a pause inserted between readings
- If read_method defined:
- If threaded: the Thread inserts data in the dictionary, __getitem__ retrieves this data else: __getitem__ directly executes read_method
else: __getitem__ as a normal thread-safe dictionary If write_method defined:
any __setitem__ call executes the write_method (dictionary is no affected)else: __setitem__ as a normal thread-safe dictionary
@deprecated now in tau.core.utils.containers
- class fandango.dicts.defaultdict_fromkey[source]
Bases: collections.defaultdict
Creates a dictionary with a default_factory function that creates new elements using key as argument. Usage : new_dict = defaultdict_fromkey(method); where method like (lambda key: return new_obj(key)) Each time that new_dict[key] is called with a key that doesn’t exist, method(key) is used to create the value Copied from PyAlarm device server @deprecated now in tau.core.utils.containers
- class fandango.dicts.fuzzyDict[source]
Bases: dict
- fandango.dicts.reversedict(dct, key=None, default=None)[source]