coilpy.sortedDict#
- class coilpy.sortedDict.SortedDict(*args, **kwargs)[source]#
Bases:
dict
A dictionary that keeps its keys in the order in which they’re inserted
- Parameters:
data – A dict object or list of (key,value) tuples from which to initialize the new SortedDict object
**kw – Optional keyword arguments given below
- kw:
- param caseInsensitive:
(bool) If True, allows self[‘UPPER’] to yield self[‘upper’].
- param sorted:
(bool) If True, keep keys sorted alphabetically, instead of by insertion order.
- param limit:
(int) keep only the latest limit number of entries (useful for data cashes)
- param dynaLoad:
(bool) Not sure what this does
- across(what='', sort=False, returnKeys=False)[source]#
Aggregate objects across the tree
- Parameters:
what – string with the regular expression to be cut across
sort – sorting of results alphabetically
returnKeys – return keys of elements in addition to objects
- Returns:
list of objects or tuple with with objects and keys
>> OMFIT[‘test’]=OMFITtree() >> for k in range(5): >> OMFIT[‘test’][‘aaa’+str(k)]=OMFITtree() >> OMFIT[‘test’][‘aaa’+str(k)][‘var’]=k >> OMFIT[‘test’][‘bbb’+str(k)]=-1 >> print(OMFIT[‘test’].across(“[‘aaa*’][‘var’]”))
- changeKeysCase(case=None, recursive=False)[source]#
Change all the keys in the dictionary to be upper/lower case
- Parameters:
case – ‘upper’ or ‘lower’
recursive – apply this recursively
- Returns:
None
- check_location(location, value=[])[source]#
check if location exist and equals value (if value is specified)
- Parameters:
location – location as string
value – value for which to return equal
- Returns:
True/False
>> root[‘SETTINGS’].check_location(“[‘EXPERIMENT’][‘shot’]”, 133221)
- diff(other, ignoreComments=False, ignoreContent=False, skipClasses=(), noloadClasses=(), precision=0.0, quiet=True)[source]#
- Parameters:
other – other dictionary to compare to
ignoreComments – ignore keys that start and end with “__” (e.g. “__comment__”)
ignoreContent – ignore content of the objects
skipClasses – list of class of objects to ignore
noloadClasses – list of class of objects to not load
precision – relative precision to which the comparison is carried out
quiet – verbosity of the comparison
- Returns:
comparison dictionary
- diffKeys(other)[source]#
- Parameters:
other – other dictionary to compare to
- Returns:
floating point to indicate the ratio of keys that are similar
- flatten()[source]#
The hierarchical structure of the dictionaries is flattened
- Returns:
SortedDict populated with the flattened content of the dictionary
- keys(filter=None, matching=False)[source]#
returns the sorted list of keys in the dictionary
- Parameters:
filter – regular expression for filtering keys
matching – boolean to indicate whether to return the keys that match (or not)
- Returns:
list of keys
- moveDown(index)[source]#
Shift down in key list the item at a given index
- Parameters:
index – index to be shifted
- Returns:
None
- moveUp(index)[source]#
Shift up in key list the item at a given index
- Parameters:
index – index to be shifted
- Returns:
None
- pop(k[, d]) v, remove specified key and return the corresponding value. [source]#
If key is not found, d is returned if given, otherwise KeyError is raised
- popitem()[source]#
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setFlat(key, value)[source]#
recursively searches dictionary for key in order to set a value raises KeyError if key could not be found, so this method cannot be used to set new entries in the dictionary
- Parameters:
key – key to be set
value – value to set
- setdefault(key, default)[source]#
The method setdefault() is similar to get(), but will set dict[key]=default if key is not already in dict
- Parameters:
key – key to be accessed
default – default value if key does not exist
- Returns:
value
- sort(key=None, **kw)[source]#
- Parameters:
key – function that returns a string that is used for sorting or dictionary key whose content is used for sordting
>> tmp=SortedDict() >> for k in range(5): >> tmp[k]={} >> tmp[k][‘a’]=4-k >> # by dictionary key >> tmp.sort(key=’a’) >> # or equivalently >> tmp.sort(key=lambda x:tmp[x][‘a’])
- Parameters:
**kw – additional keywords passed to the underlying list sort command
- Returns:
sorted keys
- sort_class(class_order=[<class 'dict'>])[source]#
sort items based on their class
- Parameters:
class_order – list containing order of class
- Returns:
sorted keys
- traverse(string='', level=100, onlyDict=False, onlyLeaf=False, skipDynaLoad=False)[source]#
Equivalent to the tree command in UNIX
- Parameters:
string – prepend this string
level – depth
onlyDict – only subtrees and not the leafs
- Returns:
list of strings containing the dictionary path to each object
- coilpy.sortedDict.buildLocation(inv)[source]#
Assemble list of keys into dictionary path string
- Parameters:
inv – list of dictionary keys including rootname
- Returns:
string representation of the dictionary path
- coilpy.sortedDict.dynaLoad(f)[source]#
Decorator which calls _dynaLoad method
- Parameters:
f – function to decorate
- Returns:
decorated function
- coilpy.sortedDict.dynaLoadKey(f)[source]#
Decorator which calls _dynaLoad method only if key is not found
- Parameters:
f – function to decorate
- Returns:
decorated function
- coilpy.sortedDict.dynaSave(f)[source]#
Decorator which calls _dynaSave method
- Parameters:
f – function to decorate
- Returns:
decorated function
- coilpy.sortedDict.get_bases(clss, tp=[])[source]#
Returns a list of strings describing the dependencies of a class
- coilpy.sortedDict.parseBuildLocation(inv)[source]#
DEPRECATED: use parseLocation and buildLocation functions instead
Function to handle locations in the OMFIT tree (i.e. python dictionaries)
- Parameters:
inv – input location
- Returns:
if inv is a string, then the dictionary path is split and a list is returned (Note that this function strips the root name)
if it’s a list, then the dictionary path is assembled and a string is returned (Note that this function assumes that the root name is missing)
- coilpy.sortedDict.parseLocation(inv)[source]#
Parse string representation of the dictionary path and return list including root name This function can parse things like: OMFIT[‘asd’][u’aiy’ ][”[ ‘bla’][‘asa’]”][3][1:5]
- Parameters:
inv – string representation of the dictionary path
- Returns:
list of dictionary keys including rootname
- coilpy.sortedDict.pretty_diff(d, ptrn={})[source]#
generate “human readable” dictionary output from SortedDict.diff()
- coilpy.sortedDict.prune_mask(what, ptrn)[source]#
prune dictionary structure based on mask The mask can be in the form of of a pretty_diff dictionary or a list of traverse strings
- coilpy.sortedDict.recursiveUpdate(A, B, overWrite=True)[source]#
Recursive update of dictionary A based on data from dictionary B
- Parameters:
A – dictionary A
B – dictionary B
overWrite – force overwrite of duplicates
- Returns:
- coilpy.sortedDict.size_tree_objects(location)[source]#
Returns file sizes of objects in the dictionary based on the size of their filename attribute
- Parameters:
location – string of the tree location to be analyzed
- Returns:
dictionary with locations sorted by size
- coilpy.sortedDict.traverse(self, string='', level=100, split=True, onlyDict=False, onlyLeaf=False, skipDynaLoad=False, noSubmodules=False, traverse_classes=(<class 'dict'>, ))[source]#
Returns a string or list of strings describing the path of every entry/subentries in the dictionary
- Parameters:
string – string to be appended in front of all entries
level – maximum depth
split – split the output string into a list of strings
onlyDict – return only dictionary entries (can be a tuple of classes)
onlyLeaf – return only non-dictionary entries (can be a tuple of classes)
skipDynaLoad – skip entries that have .dynaLoad==True
noSubmodules – controls whether to traverse submodules or not
traverse_classes – tuple of classes to traverse
- Returns:
string or list of string