Mercurial > babel > mirror
comparison babel/plural.py @ 579:99d51589c822 trunk
use decorators (as we require Python 2.4+ anyway)
author | fschwarz |
---|---|
date | Tue, 31 Jul 2012 08:46:19 +0000 |
parents | ca203b2af83c |
children |
comparison
equal
deleted
inserted
replaced
578:b167d06df1d6 | 579:99d51589c822 |
---|---|
71 type(self).__name__, | 71 type(self).__name__, |
72 ', '.join(['%s: %s' % (tag, rules[tag]) for tag in _plural_tags | 72 ', '.join(['%s: %s' % (tag, rules[tag]) for tag in _plural_tags |
73 if tag in rules]) | 73 if tag in rules]) |
74 ) | 74 ) |
75 | 75 |
76 @classmethod | |
76 def parse(cls, rules): | 77 def parse(cls, rules): |
77 """Create a `PluralRule` instance for the given rules. If the rules | 78 """Create a `PluralRule` instance for the given rules. If the rules |
78 are a `PluralRule` object, that object is returned. | 79 are a `PluralRule` object, that object is returned. |
79 | 80 |
80 :param rules: the rules as list or dict, or a `PluralRule` object | 81 :param rules: the rules as list or dict, or a `PluralRule` object |
82 :raise Ruleerror: if the expression is malformed | 83 :raise Ruleerror: if the expression is malformed |
83 """ | 84 """ |
84 if isinstance(rules, cls): | 85 if isinstance(rules, cls): |
85 return rules | 86 return rules |
86 return cls(rules) | 87 return cls(rules) |
87 parse = classmethod(parse) | 88 |
88 | 89 @property |
89 def rules(self): | 90 def rules(self): |
90 """The `PluralRule` as a dict of unicode plural rules. | 91 """The `PluralRule` as a dict of unicode plural rules. |
91 | 92 |
92 >>> rule = PluralRule({'one': 'n is 1'}) | 93 >>> rule = PluralRule({'one': 'n is 1'}) |
93 >>> rule.rules | 94 >>> rule.rules |
94 {'one': 'n is 1'} | 95 {'one': 'n is 1'} |
95 """ | 96 """ |
96 _compile = _UnicodeCompiler().compile | 97 _compile = _UnicodeCompiler().compile |
97 return dict([(tag, _compile(ast)) for tag, ast in self.abstract]) | 98 return dict([(tag, _compile(ast)) for tag, ast in self.abstract]) |
98 rules = property(rules, doc=rules.__doc__) | |
99 | 99 |
100 tags = property(lambda x: frozenset([i[0] for i in x.abstract]), doc=""" | 100 tags = property(lambda x: frozenset([i[0] for i in x.abstract]), doc=""" |
101 A set of explicitly defined tags in this rule. The implicit default | 101 A set of explicitly defined tags in this rule. The implicit default |
102 ``'other'`` rules is not part of this set unless there is an explicit | 102 ``'other'`` rules is not part of this set unless there is an explicit |
103 rule for it.""") | 103 rule for it.""") |