Trees | Indices | Help |
---|
|
object --+ | dict --+ | _RestrictedDict
Dict which only allows sequences of given length as values (PRIVATE).
This simple subclass of the Python dictionary is used in the SeqRecord object for holding per-letter-annotations. This class is intended to prevent simple errors by only allowing python sequences (e.g. lists, strings and tuples) to be stored, and only if their length matches that expected (the length of the SeqRecord's seq object). It cannot however prevent the entries being edited in situ (for example appending entries to a list).
>>> x = _RestrictedDict(5) >>> x["test"] = "hello" >>> x {'test': 'hello'}
Adding entries which don't have the expected length are blocked:
>>> x["test"] = "hello world" Traceback (most recent call last): ... TypeError: We only allow python sequences (lists, tuples or strings) of length 5.
The expected length is stored as a private attribute,
>>> x._length 5
In order that the SeqRecord (and other objects using this class) can be pickled, for example for use in the multiprocessing library, we need to be able to pickle the restricted dictionary objects.
Using the default protocol, which is 0 on Python 2.x,
>>> import pickle >>> y = pickle.loads(pickle.dumps(x)) >>> y {'test': 'hello'} >>> y._length 5
Using the highest protocol, which is 2 on Python 2.x,
>>> import pickle >>> z = pickle.loads(pickle.dumps(x, pickle.HIGHEST_PROTOCOL)) >>> z {'test': 'hello'} >>> z._length 5
|
|||
new empty dictionary |
|
||
|
|||
None |
|
||
Inherited from Inherited from |
|
|||
Inherited from |
|
|||
Inherited from |
|
Create an EMPTY restricted dictionary.
|
x[i]=y
|
Update D from dict/iterable E and F. If E has a .keys() method, does: for k in E: D[k] = E[k] If E lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
|
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Sat Aug 20 10:38:58 2011 | http://epydoc.sourceforge.net |