[Tutor] Problems with pytz module

Peter Otten __peter__ at web.de
Wed Jul 19 18:05:32 EDT 2017


Daniel Bosah wrote:

> I'm learning about OOP programming in Python.
> This is my code from my online course.
> 
> import datetime
> import pytz
> class Account:
>     """""""Simple account class with balance"""""""

Why so many "s ? Use either one for single-line strings

"Simple account class with balance"

or three for multiline strings

"""Simple account class with balance.
   yadda yadda...
"""


>     def __init__(self,name,balance):
>         self.name = name
>         self.balance = balance
>         self.transaction_list = []
>         print "Account created for " + self.name
> 
>     def deposit(self,amount):
>         if amount > 0:
>             self.balance += amount
>             self.show_balance()
> 
> 
self.transaction_list.append((pytz.utc.localize(datetime.datetime.utcnow()),amount))
> # appends traction details to list

That's either an indentation error in your code or a cut-and-past mishap.

>     def withdrawl(self,amount):
>          if 0 < amount <= self.balance:
>                 self.balance -= amount
>          else:
>             print "The account must be greater then zero and no more then
> your account balance"
>          self.show_balance()

Nothing is added to the transaction_list for withdrawals.

>     def show_balance(self):
>         print "Balance is {}".format(self.balance)
> 
>     def show_transactions(self):
>         for date, amount in self.transaction_list:
>             if amount > 0:
>                 tran_type = "deposited"
>             else:
>                 tran_type = "withdrawn"
>                 amount *= -1 # to show negative number
>                 print "{:6} {} on {} (local time was {})".format(amount,
> tran_type, date, date.astimezone())

This prints only "withdrawn" transactions.

> 
> if __name__ == '__main__':
>     tim = Account("Tim", 0)
>     tim.show_balance()
> 
> 
>     tim.deposit(1000)
>     tim.show_balance()
>     tim.withdrawl(500)
>     tim.show_transactions()
> 
>     tim.show_balance()
> 
> 
> 
> Im using Ubuntu Linux. My problem is that I cannot get show_transactions
> to print out on the console. I suspect that I cannot use pytz ( as I'm
> using Python 2.7) . Im trying to get the date and time of the transaction
> (as shown on this line -
> 
> print "{:6} {} on {} (local time was {})".format(amount, tran_type, date,
> date.astimezone())
> 
> But it will. Is there a workaround for pytz, or another problem that I am
> missing?

First read the error message and the traceback carefully (and remember to 
always include it in your posts here). They usually contain clues that make 
it much easier for you (or us) to fix what went wrong.

Then I recommend that you forget about the Account class for the moment, 
fire up the interactive interpreter and play around with the datetime and 
pytz module a bit. Once you have that part working on its own you can take 
the building blocks and put them back into your Account class.
Spoiler below:















>>> import datetime
>>> import pytz
>>> t = pytz.utc.localize(datetime.datetime.utcnow())
>>> t
datetime.datetime(2017, 7, 19, 21, 34, 40, 117522, tzinfo=<UTC>)
>>> t.astimezone()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Required argument 'tz' (pos 1) not found
>>> t.astimezone(pytz.timezone("MET"))
datetime.datetime(2017, 7, 19, 23, 34, 40, 117522, tzinfo=<DstTzInfo 'MET' 
MEST+2:00:00 DST>)

See? To convert to a timezone you have to specify which timezone you 
actually want.



More information about the Tutor mailing list