My class looks like this
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
'''
divide_it = text_log.split(' - ')
tmp_string, _, product_string, price_string = divide_it
timestamp = delorean.parse(tmp_string.strip('[]'))
product_id = int(product_string.split(':')[-1])
price = Decimal(price_string.split('$')[-1])
return cls(timestamp=timestamp, product_id=product_id, price=price)
And this is what it suppose to be when I call the parse
function
>>> log = '[2018-05-05T12:58:59.998903] - SALE - PRODUCT: 897 - PRICE:
$17.99'
>>> PriceLog.parse(log)
<PriceLog (Delorean(datetime=datetime.datetime(2018, 5, 5, 12, 58, 59,
998903), timezone='UTC'), 897, 17.99)>
But I can't even call the function. I tried the following in the terminal:
gangzhao@Gangs-MacBook-Pro yashirq % source .venv/bin/activate
(.venv) gangzhao@Gangs-MacBook-Pro yashirq % python -c 'import PriceLog; parse('[2018-05-05T12:58:59.998903] - SALE - PRODUCT: 897 - PRICE: $17.99')'
zsh: no matches found: import PriceLog; parse([2018-05-05T12:58:59.998903]
(.venv) gangzhao@Gangs-MacBook-Pro yashirq % python
Python 3.8.2 (default, Nov 4 2020, 21:23:28)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PriceLog
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'PriceLog'
question from:
https://stackoverflow.com/questions/65660332/how-to-call-python-function-in-virtual-environment 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…