-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
84 lines (72 loc) · 3.09 KB
/
example.py
File metadata and controls
84 lines (72 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
__author__ = 'martinjr'
from __init__ import ObjectiveDoc
if __name__ == "__main__":
# example data:
data="""
<namespace:root>
<entry division="sales">
<id>1</id>
<name n:attr="value">Martin</name>
<cars>
<car id="1A1 1111" used="true">Hyundai i30</car>
<car id="1A1 1112" used="false">Hyundai ix55</car>
</cars>
<occupation>Manager</occupation>
<occupation>Developer</occupation>
<paid>true</paid>
<age>32</age>
<secure:password>1234</secure:password>
</entry>
<entry division="sales">
<id>2</id>
<name>Standa</name>
<cars>
<car id="1A1 1111" used="true">Skoda Yetti</car>
</cars>
<occupation>Manager</occupation>
<paid>TRUE</paid>
<age>25</age>
<secure:password>0000</secure:password>
</entry>
<entry division="IT">
<id>3</id>
<name>Alice</name>
<occupation>Developer</occupation>
<paid>true</paid>
<age>22</age>
<secure:password>40T1_V7%Ee</secure:password>
</entry>
<entry division="IT">
<id>5</id>
<name>Bob</name>
<cars>
<car id="1A1 2718" used="true">DeLorean</car>
</cars>
<paid>false</paid>
<age>17</age>
<secure:password>hatem$</secure:password>
</entry>
</namespace:root>
"""
doc=ObjectiveDoc(data) # parse document
doc._root.traverse() # print out the structure
for entry in doc.get("namespace:root").entry: # iterate over all entries
print("RESULTS FOR %s (id %d)"%(entry.name(), entry.id())) # basic referencing
if entry.occupation: # check if there is at leas one occupation
for occ in entry.occupation: # for each occupation
print("works in %s as %s"%(entry("division"), # entry division param
occ())) # the occupation
if entry.cars: # check if there are some cars
for car in entry.cars.car:
if car("used"): # bool parsing
print("uses a %s car (id %s)"%(car(), car("id")))
if entry.paid():
print("a paid person uses password '%s'"%(entry.get("secure:password")(raw=True))) # use node.get("sub") when sub uses special characters
else:
print("an unpaid person uses password %s"%(entry.get("secure:password")(raw=True))) # use raw=True to prevent '0000' becoming 0
if entry.age()>23: # int conversion
print("%s is getting a bit old"%entry.name())
print()
for entry in doc.get("namespace:root").get("entry", division="IT"): # filter out entries with division="IT" attribute
print("IT worker: %s"%entry.name()) # (again, check when presence is not sure and
# iterate when multiple are possible)