[Tutor] Problems with pytz module
Mats Wichmann
mats at wichmann.us
Wed Jul 19 19:40:00 EDT 2017
> On 19/07/17 20:58, Daniel Bosah wrote:
don't see a lot of benefit to using pytz here... just my opinion. the
question does give a chance to annotate the example with some thoughts,
though; take them for what they're worth.
import datetime
import pytz
class Account:
"""Simple account class with balance"""
# 1. if you set up balance param with a default value, you can skip
# the argument if account is created with no balance
#def __init__(self, name, balance):
def __init__(self, name, balance=0):
self.name = name
self.balance = balance
self.transaction_list = []
# 2. seems to make sense to report initial balance
#print "Account created for " + self.name
print "Account created for " + self.name + ", initial balance" +
balance
def deposit(self,amount):
# 3. to avoid cascading indents, bail on "not applicable",
# rather than indenting a whole function behind "applicable"
#if amount > 0:
if amount <= 0:
# 4. do you want to indicate this call was a problem somehow?
return
self.balance += amount
# 5. it is not very interesting to just dump a balance.
# Either add indication it's a deposit/withdrawal and amount,
# or just omit, leaving caller to print balance if they want.
#self.show_balance()
# 6. the pytz part is just adding confusion. In the log, store the
# utc time.
# 7. suggest put the simpler/more important value first in the
log entry
#self.transaction_list.append((pytz.utc.localize(datetime.datetime.utcnow()),
amount))
# appends traction details to list
self.transaction_list.append((amount, datetime.datetime.utcnow()))
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()
# 8. record a transaction on withdraw as well. Make it negative
# as show_transactions method triggers on that.
self.transaction_list.append((-amount, datetime.datetime.utcnow()))
def show_balance(self):
print "Balance is {}".format(self.balance)
def show_transactions(self):
# 9. Presumably translog print is rare, let's say what we're doing
print "Transaction log:"
# after note #7, unpack in other order
#for date, amount in self.transaction_list:
for amount, date in self.transaction_list:
if amount > 0:
tran_type = "deposited"
else:
tran_type = "withdrawn"
amount *= -1 # to show negative number
# 10. Assuming you wanted to print in either case, change indent
# print "{:6} {} on {} (local time was
{})".format(amount, tran_type, date, date.astimezone())
print "{:6} {} on {} UTC".format(amount, tran_type, date)
# can add timezone-relative display back in if you wish
if __name__ == '__main__':
# after note #1, can leave out a zero initial balance
#tim = Account("Tim", 0)
tim = Account("Tim")
tim.show_balance()
tim.deposit(1000)
tim.show_balance()
tim.withdrawl(500)
tim.show_transactions()
tim.show_balance()
More information about the Tutor
mailing list