1
|
# XML "function" nodes that evaluate their contents to text
|
2
|
|
3
|
import datetime
|
4
|
|
5
|
import xml_dom
|
6
|
|
7
|
def map_items(func, items):
|
8
|
return [(name, func(value)) for name, value in items]
|
9
|
|
10
|
def alt(items):
|
11
|
items = list(items)
|
12
|
items.sort()
|
13
|
return items[0][1] # value of lowest-numbered item
|
14
|
|
15
|
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
|
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
|
|
34
|
def name(items):
|
35
|
items = dict(items)
|
36
|
return ' '.join([items['first'], items['last']])
|
37
|
|
38
|
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
|
# Function names must start with _ to avoid collisions with real tags
|
48
|
# Functions take arguments (items)
|
49
|
funcs = {'_alt': alt, '_range': range_, '_avg': avg, '_date': date,
|
50
|
'_name': name, '_namePart': namePart}
|
51
|
|
52
|
def process(node):
|
53
|
name = node.tagName
|
54
|
if name.startswith('_') and name in funcs: xml_dom.replace_with_text(node,
|
55
|
funcs[name](xml_dom.NodeTextEntryIter(node)))
|
56
|
else:
|
57
|
for child in xml_dom.NodeElemIter(node): process(child)
|