Project

General

Profile

1 86 aaronmk
# XML "function" nodes that evaluate their contents to text
2
3 111 aaronmk
import datetime
4
5 86 aaronmk
import xml_dom
6
7
def map_items(func, items):
8
    return [(name, func(value)) for name, value in items]
9
10 113 aaronmk
def alt(items):
11
    items = list(items)
12
    items.sort()
13
    return items[0][1] # value of lowest-numbered item
14
15 86 aaronmk
def range_(items):
16
    items = dict(map_items(float, items))
17
    return str(items['to'] - items['from'])
18
19
def avg(items):
20
    count = 0
21
    sum_ = 0.
22
    for name, value in map_items(float, items):
23
        count += 1
24
        sum_ += value
25
    return str(sum_/count)
26
27
def date(items):
28
    items = dict(items)
29 111 aaronmk
    year = float(items['year'])
30
    date = datetime.date(int(year), int(items.get('month', '1')),
31
        int(items.get('day', '1'))) + datetime.timedelta(round((year % 1.)*365))
32
    return date.strftime('%Y-%m-%d')
33 86 aaronmk
34 89 aaronmk
def name(items):
35
    items = dict(items)
36
    return ' '.join([items['first'], items['last']])
37
38 102 aaronmk
def namePart(items):
39
    items = dict(items)
40
    def to_parts(name): return items[name].split(' ')
41
    parts = []
42
    if 'first' in items: parts += to_parts('first')[:1]
43
    if 'last' in items: parts += to_parts('last')[-1:]
44
    if 'middle' in items: parts += to_parts('middle')[1:-1]
45
    return ' '.join(parts)
46
47 86 aaronmk
# Function names must start with _ to avoid collisions with real tags
48 144 aaronmk
# Functions take arguments (items)
49 113 aaronmk
funcs = {'_alt': alt, '_range': range_, '_avg': avg, '_date': date,
50 138 aaronmk
    '_name': name, '_namePart': namePart}
51 86 aaronmk
52 142 aaronmk
def process(node):
53 139 aaronmk
    name = node.tagName
54 142 aaronmk
    if name.startswith('_') and name in funcs: xml_dom.replace_with_text(node,
55
        funcs[name](xml_dom.NodeTextEntryIter(node)))
56 86 aaronmk
    else:
57 142 aaronmk
        for child in xml_dom.NodeElemIter(node): process(child)