Back to index

python-biopython  1.60
Public Member Functions | Private Attributes
Bio.SeqRecord._RestrictedDict Class Reference

List of all members.

Public Member Functions

def __init__
def __setitem__
def update

Private Attributes


Detailed Description

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

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

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

Definition at line 16 of file

Constructor & Destructor Documentation

def Bio.SeqRecord._RestrictedDict.__init__ (   self,
Create an EMPTY restricted dictionary.

Definition at line 67 of file

00068     def __init__(self, length):
00069         """Create an EMPTY restricted dictionary."""
00070         dict.__init__(self)
00071         self._length = int(length)

Member Function Documentation

def Bio.SeqRecord._RestrictedDict.__setitem__ (   self,

Definition at line 72 of file

00073     def __setitem__(self, key, value):
00074         #The check hasattr(self, "_length") is to cope with pickle protocol 2
00075         #I couldn't seem to avoid this with __getstate__ and __setstate__
00076         if not hasattr(value,"__len__") or not hasattr(value,"__getitem__") \
00077         or (hasattr(self, "_length") and len(value) != self._length):
00078             raise TypeError("We only allow python sequences (lists, tuples or "
00079                             "strings) of length %i." % self._length)
00080         dict.__setitem__(self, key, value)

def Bio.SeqRecord._RestrictedDict.update (   self,

Definition at line 81 of file

00082     def update(self, new_dict):
00083         #Force this to go via our strict __setitem__ method
00084         for (key, value) in new_dict.iteritems():
00085             self[key] = value

Here is the caller graph for this function:

Member Data Documentation

Definition at line 70 of file

The documentation for this class was generated from the following file: