第1章 1.7 使用第三方工具——parse
 
>>> from parse import parse
>>> LOG = '[2018-05-05T12:58:00.714611] - SALE - PRODUCT: 1345 - PRICE: $09.99'
>>> FORMAT = '[{date}] - SALE - PRODUCT: {product} - PRICE: ${price}'
>>> result = parse(FORMAT, LOG)
>>> result
<Result () {'date': '2018-05-05T12:58:00.714611', 'product': '1345', 'price': '09.99'}>
>>> result['date']
'2018-05-05T12:58:00.714611'
>>> result['product']
'1345'
>>> result['price']
'09.99'
>>> FORMAT = '[{date:ti}] - SALE - PRODUCT: {product:d} - PRICE: ${price:05.2f}'
>>> result = parse(FORMAT, LOG)
>>> result
<Result () {'date': datetime.datetime(2018, 5, 5, 12, 58, 0, 714611), 'product': 1345, 'price': 9.99}>
>>> result['date']
datetime.datetime(2018, 5, 5, 12, 58, 0, 714611)
>>> result['product']
1345
>>> result['price']
9.99
>>> from decimal import Decimal
>>> def price(string):
... return Decimal(string)
...
>>> FORMAT = '[{date:ti}] - SALE - PRODUCT: {product:d} - PRICE: ${price:price}'
>>> parse(FORMAT, LOG, {'price':price})
<Result () {'date': datetime.datetime(2018, 5, 5, 12, 58, 0, 714611), 'product': 1345, 'price': Decimal('9.99')}>
import parse
from decimal import Decimal
import delorean
class PriceLog(object):
    def __init__(self, timestamp, product_id, price):
        self.timestamp = timestamp
        self.product_id = product_id
        self.price = price
    def __repr__(self):
        return '<PriceLog ({}, {}, {})>'.format(self.timestamp,
                                                self.product_id,
                                                self.price)
    @classmethod
    def parse(cls, text_log):
        '''
        Parse from a text log with the format
        [<Timestamp>] - SALE - PRODUCT: <product id> - PRICE: $<price>
        to a PriceLog object
        '''
        def price(string):
            return Decimal(string)
        def isodate(string):
            return delorean.parse(string)
        FORMAT = ('[{timestamp:isodate}] - SALE - PRODUCT: {product:d} - '
                  'PRICE: ${price:price}')
        formats = {'price': price, 'isodate': isodate}
        result = parse.parse(FORMAT, text_log, formats)
        return cls(timestamp=result['timestamp'],
                   product_id=result['product'],
                   price=result['price'])
执行结果如下:
>>> log = '[2018-05-05T11:07:12.267897] - SALE - PRODUCT: 1345 - PRICE: $09.99'
>>> PriceLog.parse(log)
<PriceLog (Delorean(datetime=datetime.datetime(2018, 5, 5, 11, 7, 12, 267897), timezone='UTC'), 1345, 9.99)>
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号