Types, reflexion, generic object — selkie.cld.seal.misc
- selkie.cld.seal.misc.string_to_module(s)
Given a string representing a qualified module name, to get the module:
>>> from selkie.cld.seal.misc import string_to_module >>> m = string_to_module('foo.bar')
(This actually just calls
importlib.load_module.)
- selkie.cld.seal.misc.string_to_object(s)
Given a string representing a function, class, etc. within a qualified module name, to get the object:
>>> from selkie.cld.seal.misc import string_to_object >>> f = string_to_object('foo.bar.my_function') >>> cls = string_to_object('foo.bar.MyClass')
- class selkie.cld.seal.misc.Object
Essentially, a dict that permits keys to be accessed and set using dot syntax as well as square-bracket syntax:
>>> from selkie.cld.seal.misc import Object >>> x = Object() >>> x['hi'] = 10 >>> x.bye = 20 >>> x.hi 10 >>> x['bye'] 20
- selkie.cld.seal.misc.matches(x, desc)
X is an object and desc is a dict, interpreted as a description, in which the keys are attributes and the values are required values. The return value is
TrueorFalse, indicating whether the object matches the description.>>> from selkie.cld.seal.misc import matches >>> class Point (object): ... def __init__ (self, x, y): ... self.x = x ... self.y = y ... def L1_norm (self): ... return abs(self.x) + abs(self.y) ... >>> p = Point(2, -4) >>> matches(p, {'y': -4, 'x': 2}) True >>> matches(p, {'y': 0}) False >>> matches(p, {'foo': 'bar'}) False
If a value specification is a list, then the actual value can be any member of the list.
>>> matches(p, {'x': [0,1,2]}) True
If the named attribute is a method, then it is called to get the value that is compared to the description’s value.
>>> matches(p, {'L1_norm': 6}) True
A
Nonein the description functions as a wildcard. It matches any object.>>> matches(p, {'foo': None}) True
- class selkie.cld.seal.misc.FunctionInfo(fnc)
A FunctionInfo object provides easy-to-use reflexion for functions.
- args
The names of the positional arguments (a list of strings).
- kwargs
A list containing pairs of optional/keyword argument and its default value. Technically, these are optional arguments rather than true keyword arguments (which follow a
*argument).
- doc
The lines of the doc string, eliminating any indentation (a list of strings).
- selkie.cld.seal.misc.MethodInfo(method)
Returns a FunctionInfo object. The
selfargument is not included among the args.
- class selkie.cld.seal.misc.ListProxy
A mix-in class. The main class must have a method
__list__that returns an underlying list. ListProxy implements the following methods by dispatching to the underlying list:__iter__,__contains__,__getitem__,__len__,__repr__.
- class selkie.cld.seal.misc.MapProxy
A mix-in class. The main class must have a method
__map__that returns an underlying dict. MapProxy implements the following methods by dispatching to the underlying dict:__iter__,__len__,__contains__,__getitem__,get,keys,values,items,__repr__.
- class selkie.cld.seal.misc.LazyList(ListProxy)
A delayed version of ListProxy. The initializer is called with a function that iterates over the members of the underlying list. They are then cached, and
__list__()returns the cached list, to which the ListProxy methods dispatch.- __init__ (self, iterf):
Iterf should be a callable that returns an iteration over the underlying list. N.b.: if one calls
__iter__()and then any method that dispatches to__list__(), the iterf will be called multiple times. It should return a fresh iteration each time.
- __iter__()
If
__list__()has not previously been called, this calls the iteration function directly. Otherwise, it iterates over the cached list.
- __repr__()
Displays as
[...]if the underlying list has not been cached. Dispatches to the cached list, otherwise.
- class selkie.cld.seal.misc.Index
A dict that associates multiple values (a list) with each key. For example:
>>> from selkie.nlp.map import Index >>> index = Index() >>> index['hi'] [] >>> index.add('hi', 10) >>> index['hi'] [10] >>> index.add('hi', 42) >>> index['hi'] [10, 42]
- count(key)
Returns the number of items for a given key:
>>> index.count('hi') 2
- values()
Returns the concatenation of all the lists.
>>> index.add('bye', 20) >>> sorted(index.values()) [10, 20, 42]
- itervalues()
Iterates over all values.
- delete(key, value)
Deletes a value out of the list of values.
>>> index.delete('hi', 10) >>> index['hi'] [42]