Project

General

Profile

« Previous | Next » 

Revision 1471

units.py: Restructured to use a Quantity object for the units-tagged value and conversion functions quantity2str() and str2quantity() to convert between that and a raw string. Added convert() with basic support for removing units and passing through matching units. xml_func.py: _units: Added "to" attr. VegBIEN mappings: Remove units using new _units "to" attr instead of temporary workaround in _units.

View differences:

xml_func.py
133 133

  
134 134
def _map(items):
135 135
    items = conv_items(str, items) # get *once* from iter and check types
136
    try: value = items.pop()[1] # value is last entry's value
136
    try: value = items.pop()[1] # last entry contains value
137 137
    except IndexError, e: raise SyntaxException(e)
138 138
    map_ = dict(items)
139 139
    closed = bool(map_.pop('_closed', False))
......
159 159
#### Quantities
160 160

  
161 161
def _units(items):
162
    items = dict(conv_items(str, items))
163
    try: value = items['value']
164
    except KeyError: return None # input is empty
165
    default_units = items.get('default', None)
166
    # DB unit conversion isn't ready yet, so just return number
167
    try: return units.cleanup_units(value, default_units).split(' ')[0]
162
    items = conv_items(str, items) # get *once* from iter and check types
163
    try: last = items.pop() # last entry contains value
164
    except IndexError: return None # input is empty and no actions
165
    if last[0] != 'value': return None # input is empty
166
    str_ = last[1]
167
    
168
    quantity = units.str2quantity(str_)
169
    try:
170
        for action, units_ in items:
171
            units_ = util.none_if(units_, u'')
172
            if action == 'default': units.set_default_units(quantity, units_)
173
            elif action == 'to': quantity = units.convert(quantity, units_)
174
            else: raise SyntaxException(ValueError('Invalid action: '+action))
168 175
    except units.MissingUnitsException, e: raise SyntaxException(e)
176
    return units.quantity2str(quantity)
169 177
funcs['_units'] = _units
170 178

  
171 179
def _range(items):

Also available in: Unified diff