forked from lolongcovas/python_junior_parsing_test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing_client.py
More file actions
74 lines (54 loc) · 1.84 KB
/
Copy pathparsing_client.py
File metadata and controls
74 lines (54 loc) · 1.84 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
"""Implement one particular client parser."""
import xml.etree.ElementTree as ET
from model import Product, Image
class ClientParser:
"""Client parser abstract class."""
def __len__(self):
"""Get number of products.
Return:
int, number of products
"""
raise NotImplementedError
def __getitem__(self, idx):
"""Get the product of index `idx`.
Iterate over the feed and extract needed information for the product
at `idx` index.
Return:
Product, one product
"""
raise NotImplementedError
class ClientAParser(ClientParser):
"""Client A parser."""
def __init__(self, filename):
"""Client parser constructor."""
if filename.endswith("xml"):
self.tree = ET.parse(filename)
elif filename.endswith(".gz"):
import gzip
self.tree = ET.parse(gzip.open(filename))
else:
raise NotImplementedError
def __len__(self):
#TODO, implement "how to get number of products"
pass
def __getitem__(self, idx):
#TODO, implement the parsing product at `idx`
# For instance, get the following information and others from the XML:
# category = 'Papyon'
# price = 329.00
# gender = "male"
pass
# auto generated doc from super class
__len__.__doc__ = ClientParser.__len__.__doc__
__getitem__.__doc__ = ClientParser.__getitem__.__doc__
if __name__ == '__main__':
import time
xml_filename = 'feed.gz'
parser = ClientAParser(xml_filename)
tic = time.time()
# optimize and speed up the whole xml parsing
# you could use threading, multi processing and etc
for idx in range(len(parser)):
product = parser[idx]
toc = time.time() - tic
print('Elapsed {} secs'.format(toc))