Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/raw-rev/83a40cd34923
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 08:10:49 2012
Кодировка:

# HG changeset patch
# User Daniil Alexeyevsky
# Date 1310112223 -14400
# Node ID 83a40cd3492340133ea2a22f6372c7b233797d7f
# Parent 2c3bd08e1f1d9390a1dc21dbc08fd1688c203d18
Added docstrings to base markup classes

diff -r 2c3bd08e1f1d -r 83a40cd34923 allpy/base.py
--- a/allpy/base.py Fri Jul 08 00:23:51 2011 +0200
+++ b/allpy/base.py Fri Jul 08 12:03:43 2011 +0400
@@ -490,11 +490,27 @@
return block

class Markup(object):
+ """Base class for sequence and alignment markups.
+
+ We shall call either sequence or alignment a container. And we shall call
+ either monomers or columns elements respectively.
+
+ Markup behaves like a dictionary of [element] -> value.
+
+ Every container has a dictionary of [name] -> markup. It is Markup's
+ responsibility to add itself to this dictionary and to avoid collisions
+ while doing it.
+ """

name = None
"""Name of markup elements"""

def _register(self, container, name):
+ """Register self within container.
+
+ Assure the name is not taken before. If name is not given, look in the
+ class. Make sure we have some name at all.
+ """
if name:
self.name = name
assert self.name is not None
@@ -502,16 +518,44 @@
container.markups[self.name] = self

def refresh(self):
+ """Recalculate markup values (if they are generated automatically)."""
pass

@classmethod
- def from_record(cls, alignment, record, name=None):
- return cls(alignment, name)
+ def from_record(cls, container, record, name=None):
+ """Restore markup from `record`. (Used for loading from file).
+
+ `record` is a dict of all metadata and data related to one markup. All
+ keys and values in `record` are strings, markup must parse them itself.
+
+ Markup values should be stored in `record['markup']`, which is a list
+ of items separated with either `record['separator']` or a comma.
+ """
+ return cls(container, name)

def to_record(self):
+ """Save markup to `record`, for saving to file.
+
+ For description of `record` see docstring for `from_record` method.
+ """
return {}

+ def sorted_keys(self):
+ """Return list of elements in the container in proper order."""
+ raise NotImplementedError()
+
+ def sorted_values(self):
+ """Return list of markup values in container."""
+ raise NotImplementedError()
+
class SequenceMarkup(Markup):
+ """Markup for sequence.
+
+ Behaves like a dictionary of [monomer] -> value. Value may be anything
+ or something specific, depending on subclass.
+
+ Actual values are stored in monomers themselves as attributes.
+ """

def __init__(self, sequence, name=None):
self.sequence = sequence
@@ -519,26 +563,37 @@
self.refresh()

def sorted_keys(self):
+ """Return list of monomers."""
return self.sequence

def sorted_values(self):
+ """Return list of markup values, if every monomer is marked up."""
return (self[monomer] for monomer in self.sequence)

def get(self, key, value=None):
+ """Part of Mapping collection interface."""
if key not in self:
return value
return self[key]

def __contains__(self, monomer):
+ """Part of Mapping collection interface."""
return hasattr(monomer, self.name)

def __getitem__(self, monomer):
+ """Part of Mapping collection interface."""
return getattr(monomer, self.name)

def __setitem__(self, monomer, value):
+ """Part of Mapping collection interface."""
return setattr(monomer, self.name, value)

class AlignmentMarkup(dict, Markup):
+ """Markupf for alignment.
+
+ Is a dictionary of [column] -> value. Value may be anything or something
+ specific, depending on subclass.
+ """

def __init__(self, alignment, name=None):
self.alignment = alignment
@@ -546,9 +601,11 @@
self.refresh()

def sorted_keys(self):
+ """Return a list of columns."""
return self.alignment.columns

def sorted_values(self):
+ """Return a list of makrup values, if every column is marked up."""
return (self[column] for column in self.alignment.columns)

# vim: set ts=4 sts=4 sw=4 et: