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 102 aaronmk
# Function names must be lowercase because name_of() returns name lowercased
49
# Functions take arguments (doc, node)
50 113 aaronmk
funcs = {'_alt': alt, '_range': range_, '_avg': avg, '_date': date,
51
    '_name': name, '_namepart': namePart}
52 86 aaronmk
53
def process(doc, node=None):
54
    if node == None: node = doc.documentElement
55
    name = xml_dom.name_of(node)
56 115 aaronmk
    if name.startswith('_') and name in funcs: xml_dom.replace_with_text(doc,
57
        node, funcs[name](xml_dom.NodeTextEntryIter(node)))
58 86 aaronmk
    else:
59
        for child in xml_dom.NodeElemIter(node): process(doc, child)