Project

General

Profile

« Previous | Next » 

Revision 1602

xml_func.py: _map: Factored replacing code out into new function repl(), which can also be used by other XML funcs

View differences:

lib/xml_func.py
143 143
    except ValueError: return value # value not convertible, so can't equal null
144 144
funcs['_nullIf'] = _nullIf
145 145

  
146
def _map(items):
146
def repl(repls, value):
147 147
    '''Raises error if value not in map and no special '*' entry
148
    @param items
149
        <last_entry> Value
150
        <other_entries> name=value Mappings
151
            name "*" means all other input values
152
            value "*" means keep input value the same
153
            value "" means ignore input value
148
    @param repls dict repl:with
149
        repl "*" means all other input values
150
        with "*" means keep input value the same
151
        with "" means ignore input value
154 152
    '''
155
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
156
    value = pop_value(items)
157
    if value == None: return None # input is empty
158
    map_ = dict(items)
159
    
160
    try: new_value = map_[value]
153
    try: new_value = repls[value]
161 154
    except KeyError, e:
162 155
        # Save traceback right away in case another exception raised
163 156
        se = SyntaxException(e) 
164
        try: new_value = map_['*']
157
        try: new_value = repls['*']
165 158
        except KeyError: raise se
166 159
    if new_value == '*': new_value = value # '*' means keep input value the same
167 160
    return util.none_if(new_value, u'') # empty map entry means None
161

  
162
def _map(items):
163
    '''See repl()
164
    @param items
165
        <last_entry> Value
166
        <other_entries> name=value Mappings. Special values: See repl() repls.
167
    '''
168
    items = conv_items(strings.ustr, items) # get *once* from iter, check types
169
    value = pop_value(items)
170
    if value == None: return None # input is empty
171
    return repl(dict(items), value)
168 172
funcs['_map'] = _map
169 173

  
170 174
def _replace(items):

Also available in: Unified diff