Skip to content

Commit d652e2f

Browse files
authored
Add files via upload
1 parent 3644ba6 commit d652e2f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

my_delorean.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import delorean
2+
import parse
3+
from decimal import Decimal
4+
class PriceLog(object):
5+
def __init__(self, timestamp, product_id, price):
6+
self.timestamp = timestamp
7+
self.product_id = product_id
8+
self.price = price
9+
def __repr__(self):
10+
return '<PriceLog ({}, {}, {})>'.format(self.timestamp,
11+
self.product_id,
12+
self.price)
13+
@classmethod
14+
def parse(cls, text_log):
15+
'''
16+
Parse from a text log with the format
17+
[<Timestamp>] - SALE - PRODUCT: <product id> - PRICE: $<price>
18+
to a PriceLog object
19+
'''
20+
divide_it = text_log.split(' - ')
21+
tmp_string, _, product_string, price_string = divide_it
22+
timestamp = delorean.parse(tmp_string.strip('[]'))
23+
product_id = int(product_string.split(':')[-1])
24+
price = Decimal(price_string.split('$')[-1])
25+
return cls(timestamp=timestamp, product_id=product_id, price=price)
26+
27+
# test code
28+
log = '[2018-05-05T11:07:12.267897] - SALE - PRODUCT: 1345 - PRICE: $09.99'
29+
PriceLog.parse(log)

0 commit comments

Comments
 (0)