-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBite38 ElementTree XML.py
45 lines (34 loc) · 1.88 KB
/
Bite38 ElementTree XML.py
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
import xml.etree.ElementTree as ET
# from OMDB
xmlstring = '''<?xml version="1.0" encoding="UTF-8"?>
<root response="True">
<movie title="The Prestige" year="2006" rated="PG-13" released="20 Oct 2006" runtime="130 min" genre="Drama, Mystery, Sci-Fi" director="Christopher Nolan" />
<movie title="The Dark Knight" year="2008" rated="PG-13" released="18 Jul 2008" runtime="152 min" genre="Action, Crime, Drama" director="Christopher Nolan" />
<movie title="The Dark Knight Rises" year="2012" rated="PG-13" released="20 Jul 2012" runtime="164 min" genre="Action, Thriller" director="Christopher Nolan" />
<movie title="Dunkirk" year="2017" rated="PG-13" released="21 Jul 2017" runtime="106 min" genre="Action, Drama, History" director="Christopher Nolan" />
<movie title="Interstellar" year="2014" rated="PG-13" released="07 Nov 2014" runtime="169 min" genre="Adventure, Drama, Sci-Fi" director="Christopher Nolan"/>
</root>''' # noqa E501
def get_tree():
tree = ET.fromstring(xmlstring)
"""You probably want to use ET.fromstring"""
return tree
#print (get_tree())
def get_movies():
titles_list = []
for movies in get_tree().findall('movie'):
title = movies.get('title')
titles_list.append(title)
return (titles_list)
"""Call get_tree and retrieve all movie titles, return a list or generator"""
#print(get_movies())
def get_movie_longest_runtime():
"""Call get_tree again and return the movie title for the movie with the longest
runtime in minutes, for latter consider adding a _get_runtime helper"""
max_runtime = 0
for movies in get_tree().findall('movie'):
runtime = int(movies.get('runtime').split()[0]) # Extract runtime value and convert to integer
if runtime > max_runtime:
max_runtime = runtime
longest_movie = movies.get('title')
return longest_movie
#print(get_movie_longest_runtime())