Package Bio :: Module SeqRecord :: Class _RestrictedDict
[hide private]
[frames] | no frames]

Class _RestrictedDict

source code

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
Instance Methods [hide private]
new empty dictionary

__init__(self, length)
Create an EMPTY restricted dictionary.
source code
 
__setitem__(self, key, value)
x[i]=y
source code
None

update(self, new_dict)
Update D from dict/iterable E and F.
source code

Inherited from dict: __cmp__, __contains__, __delitem__, __eq__, __ge__, __getattribute__, __getitem__, __gt__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __repr__, __sizeof__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, values

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __str__, __subclasshook__

Class Variables [hide private]

Inherited from dict: __hash__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, length)
(Constructor)

source code 
Create an EMPTY restricted dictionary.
Returns:
new empty dictionary

Overrides: object.__init__

__setitem__(self, key, value)
(Index assignment operator)

source code 
x[i]=y

Overrides: dict.__setitem__
(inherited documentation)

update(self, new_dict)

source code 
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]

Returns:
None

Overrides: dict.update
(inherited documentation)