Skip to content

Commit 1cb22dd

Browse files
committed
Refactoring. Using black code style
1 parent 4a3bb09 commit 1cb22dd

5 files changed

Lines changed: 40 additions & 40 deletions

File tree

XML/xml.etree.ElementTree__examples/create_xml.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://docs.python.org/3/library/xml.etree.elementtree.html
@@ -12,28 +12,28 @@
1212
from pretty_print import indent
1313

1414

15-
root = ET.Element('data')
15+
root = ET.Element("data")
1616

17-
country = ET.Element('country', name="Liechtenstein")
17+
country = ET.Element("country", name="Liechtenstein")
1818

19-
rank = ET.Element('rank', updated="yes")
20-
rank.text = '2'
19+
rank = ET.Element("rank", updated="yes")
20+
rank.text = "2"
2121
country.append(rank)
2222

23-
year = ET.Element('year')
24-
year.text = '2008'
23+
year = ET.Element("year")
24+
year.text = "2008"
2525
country.append(year)
2626

27-
gdppc = ET.Element('gdppc')
28-
gdppc.text = '141100'
27+
gdppc = ET.Element("gdppc")
28+
gdppc.text = "141100"
2929
country.append(gdppc)
3030

31-
country.append(ET.Element('neighbor', name="Austria", direction="E"))
32-
country.append(ET.Element('neighbor', name="Switzerland", direction="W"))
31+
country.append(ET.Element("neighbor", name="Austria", direction="E"))
32+
country.append(ET.Element("neighbor", name="Switzerland", direction="W"))
3333

3434
root.append(country)
3535

36-
country = ET.Element('country', name="Singapore")
36+
country = ET.Element("country", name="Singapore")
3737
root.append(country)
3838

3939
...
@@ -55,7 +55,7 @@
5555

5656
etree = ET.ElementTree(root)
5757
f = io.BytesIO()
58-
etree.write(f, encoding='utf-8', xml_declaration=True)
58+
etree.write(f, encoding="utf-8", xml_declaration=True)
5959
print(f.getvalue().decode(encoding="utf-8"))
6060
# <?xml version='1.0' encoding='utf-8'?>
6161
# <data>

XML/xml.etree.ElementTree__examples/dict_to_xml.etree.ElementTree.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import xml.etree.ElementTree as ET
@@ -12,16 +12,16 @@
1212
{"first_name": "Sergey", "last_name": "Sidorov", "city": "Sochi"},
1313
]
1414

15-
root = ET.Element('root')
15+
root = ET.Element("root")
1616

1717
for i, item in enumerate(items, 1):
18-
person = ET.SubElement(root, f'person{i}')
18+
person = ET.SubElement(root, f"person{i}")
1919

2020
for k, v in item.items():
2121
ET.SubElement(person, k).text = v
2222

2323
tree = ET.ElementTree(root)
24-
tree.write('person.xml')
24+
tree.write("person.xml")
2525
# <root>
2626
# <person1>
2727
# <first_name>Ivan</first_name>

XML/xml.etree.ElementTree__examples/pretty_print.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://docs.python.org/3/library/xml.etree.elementtree.html
@@ -11,27 +11,27 @@
1111

1212
# SOURCE: http://effbot.org/zone/element-lib.htm#prettyprint
1313
def indent(elem, level=0):
14-
i = "\n" + level*" "
14+
i = "\n" + level * " "
1515
if len(elem):
1616
if not elem.text or not elem.text.strip():
1717
elem.text = i + " "
1818
if not elem.tail or not elem.tail.strip():
1919
elem.tail = i
2020
for elem in elem:
21-
indent(elem, level+1)
21+
indent(elem, level + 1)
2222
if not elem.tail or not elem.tail.strip():
2323
elem.tail = i
2424
else:
2525
if level and (not elem.tail or not elem.tail.strip()):
2626
elem.tail = i
2727

2828

29-
if __name__ == '__main__':
30-
root = ET.Element('data')
29+
if __name__ == "__main__":
30+
root = ET.Element("data")
3131

32-
country = ET.Element('country', name="Liechtenstein")
33-
rank = ET.Element('rank', updated="yes")
34-
rank.text = '2'
32+
country = ET.Element("country", name="Liechtenstein")
33+
rank = ET.Element("rank", updated="yes")
34+
rank.text = "2"
3535
country.append(rank)
3636

3737
root.append(country)

XML/xml.etree.ElementTree__examples/print_element_with_max_date.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7+
import xml.etree.ElementTree as ET
78
from datetime import datetime
89

10+
911
# SOURCE: https://ru.stackoverflow.com/questions/905565/
1012
text = """\
1113
<Response>
@@ -25,18 +27,16 @@
2527

2628

2729
def to_date(date_str):
28-
return datetime.strptime(date_str, '%Y-%m-%d')
30+
return datetime.strptime(date_str, "%Y-%m-%d")
2931

3032

31-
# Из стандартной библиотеки
32-
import xml.etree.ElementTree as ET
3333
root = ET.fromstring(text)
3434

35-
items = root.iter('Leader')
35+
items = root.iter("Leader")
3636
# OR:
3737
# items = root.findall('.//Leader')
38-
leader = max(items, key=lambda x: to_date(x.attrib['ActualDate']))
38+
leader = max(items, key=lambda x: to_date(x.attrib["ActualDate"]))
3939

40-
print(leader.attrib['FIO']) # Шxxxxxxx Аxxxxx Шxxxxxx
41-
print(leader.attrib['ActualDate']) # 2009-12-01
42-
print(leader.attrib['Position']) # генеральный директор
40+
print(leader.attrib["FIO"]) # Шxxxxxxx Аxxxxx Шxxxxxx
41+
print(leader.attrib["ActualDate"]) # 2009-12-01
42+
print(leader.attrib["Position"]) # генеральный директор

XML/xml.etree.ElementTree__examples/xml_declaration.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://docs.python.org/3/library/xml.etree.elementtree.html
@@ -12,12 +12,12 @@
1212
from pretty_print import indent
1313

1414

15-
root = ET.Element('data')
15+
root = ET.Element("data")
1616

17-
country = ET.Element('country', name="Liechtenstein")
17+
country = ET.Element("country", name="Liechtenstein")
1818

19-
rank = ET.Element('rank', updated="yes")
20-
rank.text = '2'
19+
rank = ET.Element("rank", updated="yes")
20+
rank.text = "2"
2121
country.append(rank)
2222

2323
root.append(country)
@@ -34,7 +34,7 @@
3434

3535
etree = ET.ElementTree(root)
3636
f = io.BytesIO()
37-
etree.write(f, encoding='utf-8', xml_declaration=True)
37+
etree.write(f, encoding="utf-8", xml_declaration=True)
3838
print(f.getvalue().decode(encoding="utf-8"))
3939
# <?xml version='1.0' encoding='utf-8'?>
4040
# <data>

0 commit comments

Comments
 (0)