0

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'

enter image description here

user8314628
  • 1,952
  • 2
  • 22
  • 46
  • You need to include the module PriceLog is in. E.g. if it's in the file `price.py` then you need to `from price import PriceLog` – alkasm Jan 11 '21 at 01:34
  • @alkasm The relative path of the file is `Practice/Python/Automation/02_priceLog.py`. So I tried `from 02_priceLog.py import PriceLog`. It still doesn't work, which gives `SyntaxError: invalid decimal literal` – user8314628 Jan 11 '21 at 02:57
  • Did you try `from 02_priceLog import PriceLog` – Anirban Saha Jan 11 '21 at 03:02
  • 1
    Module names in Python don't start with numbers. See if this post helps https://stackoverflow.com/questions/9090079/in-python-how-to-import-filename-starts-with-a-number – Nerveless_child Jan 11 '21 at 03:18
  • @Anirban It doesn't work. I even rename my file as priceLog.py and tried `from priceLog import PriceLog`, `from priceLog.py import PriceLog` – user8314628 Jan 12 '21 at 01:48

0 Answers0