fancytools.fcollections package¶
Submodules¶
fancytools.fcollections.FIFObuffer module¶
-
class
fancytools.fcollections.FIFObuffer.FIFObuffer(shape, dtype=<type 'numpy.float32'>, filled=False)[source]¶ Bases:
objectA circular FIFO buffer implemented on top of numpy.
>>> a = FIFObuffer(shape=3) >>> a.add(2) >>> a.add(33) >>> a.add(124) >>> print a [ 2. 33. 124.] >>> a.add(21) >>> a.add(2456) >>> print a [ 124. 21. 2456.]
-
position¶
-
fancytools.fcollections.MultiList module¶
-
class
fancytools.fcollections.MultiList.MultiList(*names)[source]¶ Bases:
objectcreate a list of named lists. can be used as like a normal list but allows to get entries from direct from sublists
>>> l = MultiList('spam','eggs') >>> l.extend( ([1,2],[3,4]) ) >>> print l.spam [1, 2] >>> print l.eggs [3, 4] >>> print l[1] [2, 4]
fancytools.fcollections.NestedOrderedDict module¶
-
class
fancytools.fcollections.NestedOrderedDict.NestedOrderedDict(*args, **kwargs)[source]¶ Bases:
collections.OrderedDictan OrderedDict allowing the access of nested items through a .path attribute
every item of the NestedOrderedDict that is also a NestedOrderedDict inherits the path information to the uppermost NestedOrderedDict
lets’ build a nested structure:
>>> parent = NestedOrderedDict() >>> ch1 = 'foo' >>> ch2 = NestedOrderedDict() >>> ch2_1 = NestedOrderedDict() >>> ch2_1_1 = NestedOrderedDict([['hello','world']])
set items:
>>> parent['2'] = ch1 >>> parent['3'] = ch2 >>> ch2['4'] = ch2_1 >>> ch2_1['5'] = ch2_1_1
get the path from the most nested item:
>>> p = ch2_1_1.path >>> print p 3, 4, 5
access this item from the parent dict:
>>> print parent[p] NestedOrderedDict({hello: world})
fancytools.fcollections.ProxyList module¶
-
class
fancytools.fcollections.ProxyList.ProxyList[source]¶ Bases:
listforwards an attribute/method given to this instance to its list-entries
create a list >>> a = ProxyList((‘aa’,11,’cc’))
find ‘a’ in the list because ‘find’ is not a method of this instance all list-entries will be given this method
>>> print a.find('a') [0, None, -1]
... this returns a list saying that... [0,... –> ‘a’ is in the first position of the first entry ..., None –> there is no fitting method for the second entry ... ,-1 ] –> ‘a’ is not found in the last entry
show indices of list entries that are of type string
>>> print a.where('__class__',str) [0, 2]
...using _TestObject which has one one extra method ‘foo’
>>> p = ProxyList([_TestObject(),_TestObject(),_TestObject()]) >>> print p.foo[0].__name__ foo
each time ‘foo’ is called it returns (‘bar’, 99) therefore for each member of the ProxyList the output would be:
>>> print p.foo('bar') [('bar', 99), ('bar', 99), ('bar', 99)]
Also this method sets an attribute ‘a’ to ‘bar’->
>>> print p.a ['bar', 'bar', 'bar']
- / etc can also be forwarded to the members of a list:
>>> p = ProxyList([ 1, -20, 3 ]) >>> p += 10 >>> print p [11, -10, 13]
>>> print p.__abs__() [11, 10, 13]
If the members are mutable their id remains the same:
>>> p = ProxyList([ [1], [2], [3] ]) >>> oldID = id(p[0]) >>> p *= 4 >>> newID = id(p[0]) >>> print oldID == newID True
A ProxyList can be used for a mathematical operation with another ProxyList:
>>> p1 = ProxyList([ 1, 2, 3 ]) >>> p2 = ProxyList([ 1, 2, 3 ]) >>> p1-p2 [0, 0, 0]
fancytools.fcollections.TwoDArraySliceIterator module¶
-
class
fancytools.fcollections.TwoDArraySliceIterator.TwoDArraySliceIterator(main_size, slice_size)[source]¶ Bases:
objecta simple iterator that build small slices from a big array
>>> import numpy as np
>>> arr = np.random.rand(100,100) >>> n_pieces = (2,4) >>> i = TwoDArraySliceIterator( arr.shape, n_pieces )
than iterate through your array as follows:
- for s1,s2 in i:
- print s1,s2, arr[s1,s2] ...
fancytools.fcollections.WeakList module¶
-
class
fancytools.fcollections.WeakList.WeakList(l=())[source]¶ Bases:
lista list that removes its entries, if they are originally removed
>>> import numpy as np >>> arr = np.ones(100) >>> l = WeakList([arr,arr]) >>> l.append(arr) >>> assert len(l) == 3 >>> l[0] is arr True >>> arr in l True >>> del arr >>> assert len(l) == 0
fancytools.fcollections.naturalSorting module¶
-
fancytools.fcollections.naturalSorting.naturalSorting(l)[source]¶ sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html (See Toothy’s implementation in the comments)
>>> alist = [ "something1", "something12", "something17", "something2", "something25", "something29"]
>>> print naturalSorting(alist) ['something1', 'something2', 'something12', 'something17', 'something25', 'something29']