Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/rev/c37a2c56deb7
Дата изменения: Unknown
Дата индексирования: Tue Oct 2 00:29:59 2012
Кодировка:
allpy: c37a2c56deb7

allpy

changeset 427:c37a2c56deb7

improve markup module: * correct docstrings with PEP-257 (see #11) * add convenient methods .items() and .keys() * make .from_string() method static
author boris <bnagaev@gmail.com>
date Mon, 14 Feb 2011 21:04:47 +0300
parents 43bca78ece0c
children 3566d253c994
files allpy/markup.py
diffstat 1 files changed, 23 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/allpy/markup.py	Mon Feb 14 19:32:51 2011 +0300
     1.2 +++ b/allpy/markup.py	Mon Feb 14 21:04:47 2011 +0300
     1.3 @@ -31,7 +31,7 @@
     1.4      """
     1.5  
     1.6      def __init__(self, name, container, map_out={}):
     1.7 -        """ Note: adds convenient defaults to maps """
     1.8 +        """ Note: add convenient defaults to maps """
     1.9          self.name = name
    1.10          self.container = container
    1.11          self.set_map_out(map_out)
    1.12 @@ -53,14 +53,14 @@
    1.13          self.map_out[default] = default
    1.14  
    1.15      def __getitem__(self, item):
    1.16 -        """ returns internal value for given item
    1.17 +        """ return internal value for given item
    1.18  
    1.19          markup[monomer] <==> monomer.name, where name is markup.name
    1.20          """
    1.21          return getattr(item, self.name)
    1.22  
    1.23      def __contains__(self, item):
    1.24 -        """ returns if intarnal value determined for given item
    1.25 +        """ return item has attribute
    1.26  
    1.27          usage: if item in markup
    1.28          """
    1.29 @@ -81,14 +81,31 @@
    1.30                  internal_values.append(default)
    1.31          return ''.join(self.map_out[i] for i in internal_values)
    1.32  
    1.33 -    def from_string(self, string):
    1.34 -        """ adds (or replaces) attributes to items """
    1.35 -        assert len(string) == len(self.container)
    1.36 +    @classmethod
    1.37 +    def from_string(cls, string, name, container, map_out={}):
    1.38 +        """ return new markup
    1.39 +
    1.40 +        add (or replaces) attributes to items
    1.41 +        """
    1.42 +        assert len(string) == len(container)
    1.43 +        self = cls(name, container, map_out)
    1.44          for char, item in zip(list(string), self.container):
    1.45              if char in self.map_in:
    1.46                  internal = self.map_in[char]
    1.47              else:
    1.48                  internal = self.map_in[default]
    1.49              setattr(item, self.name, internal)
    1.50 +        return self
    1.51  
    1.52 +    def items(self):
    1.53 +        """ return list of tuples (item, self[item]) for items with attribute """
    1.54 +        for item in self.container:
    1.55 +            if item in self:
    1.56 +                raise (item, self[item])
    1.57  
    1.58 +    def keys(self):
    1.59 +        """ return list of items for with attribute is determined """
    1.60 +        for item in self.container:
    1.61 +            if item in self:
    1.62 +                raise item
    1.63 +