[Tutor] Problems with pytz module
Peter Otten
__peter__ at web.de
Wed Jul 19 17:51:59 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()
Looks like you don't add anything 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())
Do you want to print only when money is withdrawn?
>
> 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?
Read the error message and traceback carefully (and remember to provide it
in your post). It usually has important clues that help you (or us) fix what
went wrong.
Now I recommend that you forget about the Account class for the moment, fire
up the interactive interpreter and play around with datetime and pytz
a bit. Once you have working building blocks you can go back and easily
apply the lessons you learned from your experiments to the original problem.
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 actually specify the timezone you
want.
More information about the Tutor
mailing list