Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/e83572fff43f/allpy/markup.py
Дата изменения: Unknown
Дата индексирования: Mon Feb 4 03:48:58 2013
Кодировка:
allpy: e83572fff43f allpy/markup.py

allpy

view allpy/markup.py @ 746:e83572fff43f

Roll-back a bug introduces by dirty hand-merge in [723]. (closes #74) (see #76) Boris! Please do not do dirty hand merges! If you did hg fetch here, this bug would not appear! Please, be extremely careful when you do hand merges and double-check your changes. Do a diff with each parent and see what you remove related to the parent! If someone else's code is involved in the merge (which is almost always the case), do that diff twice just to make sure you have not missed anything!
author Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru>
date Mon, 11 Jul 2011 14:29:54 +0400
parents 3566d253c994
children
line source
1 import base
3 default = object()
4 """ object for use as key for default value """
6 class Markup(object):
7 """ Markup for Sequence or Alignment
9 container is iterable collection of items.
10 item is object getting markup data (new attribute).
12 container: Alignment, item: Column
13 container: Sequence, item: Monomer
15 Markup stored in attributes of item is internal value.
16 When export or import, it is mapped to or from external value.
17 Internal value could be of any type.
18 External value should be single character.
20 Data:
21 * name -- name (str) of attribute of object
22 * container
23 * map_out -- dict, mapping internal values to external
24 * map_in -- dict, mapping external values to internal
26 map_in and map_out can have key markup.default, which used when
27 input key is not in map. To drop this defaults, drop this key
28 from map_out and map_in dicts. In this case to_string() and
29 from_string() methods can raise KeyError
30 """
32 def __init__(self, name, container, map_out={}):
33 """ Note: add convenient defaults to maps """
34 self.name = name
35 self.container = container
36 self.set_map_out(map_out)
37 self.set_internal_default()
38 self.set_external_default()
40 def set_map_out(self, map_out):
41 self.map_out = map_out
42 self.map_in = dict([(v, k) for (k, v) in self.map_out.items()])
44 def set_map_in(self, map_in):
45 self.map_in = map_in
46 self.map_out = dict([(v, k) for (k, v) in self.map_in.items()])
48 def set_internal_default(self, default_value=None):
49 self.map_in[default] = default_value
51 def set_external_default(self, default_value='-'):
52 self.map_out[default] = default_value
54 def __getitem__(self, item):
55 """ return internal value for given item
57 markup[monomer] <==> monomer.name, where name is markup.name
58 """
59 return getattr(item, self.name)
61 def __setitem__(self, item, value):
62 """ set attribute of item to value """
63 setattr(item, self.name, value)
65 def __contains__(self, item):
66 """ return item has attribute
68 usage: if item in markup
69 """
70 return hasattr(item, self.name)
72 def clear(self):
73 """ delete all attributes from items """
74 for item in self.keys():
75 delattr(item, self.name)
77 def to_string(self):
78 """ return string representing markup """
79 internal_values = []
80 for item in self.container:
81 if item in self and self[item] in self.map_out:
82 internal_values.append(self[item])
83 else:
84 internal_values.append(default)
85 return ''.join(self.map_out[i] for i in internal_values)
87 @classmethod
88 def from_string(cls, string, name, container, map_out={}):
89 """ return new markup
91 add (or replaces) attributes to items
92 """
93 assert len(string) == len(container)
94 self = cls(name, container, map_out)
95 for char, item in zip(list(string), self.container):
96 if char in self.map_in:
97 internal = self.map_in[char]
98 else:
99 internal = self.map_in[default]
100 setattr(item, self.name, internal)
101 return self
103 def items(self):
104 """ return list of tuples (item, self[item]) for items with attribute """
105 for item in self.container:
106 if item in self:
107 yield (item, self[item])
109 def keys(self):
110 """ return list of items for with attribute is determined """
111 for item in self.container:
112 if item in self:
113 yield item