File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments