Skip to content

Instantly share code, notes, and snippets.

@dirkjot
Created November 22, 2017 19:08
Show Gist options
  • Save dirkjot/e4794a40af55e381721271fb7b5a4f88 to your computer and use it in GitHub Desktop.
Save dirkjot/e4794a40af55e381721271fb7b5a4f88 to your computer and use it in GitHub Desktop.
Python workshop part 2: Objects and list comprehension
This code is not complete: One of the tests currently fails and the Transaction class is not used. Can you fix it?
"""
Session 2:
Object oriented programming and Test Driven Development with Python
Dirk P. Janssen - Fall 2017
This code is working but not functionally complete: The 'largewithdrawals'
method does not work, as is shown by one failing test. You can use the
Transaction class that we already wrote, but there are other ways to solve
this.
Bonus points: For each new method or attribute, try to think hard
where it fits best. Which class has the most natural respoonsibility for this
method or attribute?
"""
class Account(object):
"""
Simple bank account supporting withdrawal and deposit
"""
def __init__(self):
self.balance = 0
def withdraw(self, amount):
newbalance = self.balance - amount
if newbalance >= 0:
self.balance = newbalance
else:
raise ValueError("Cannot withdraw this much, balance is %f" % self.balance)
def deposit(self, amount):
self.balance += amount
class Transaction():
"""
This class holds a single transaction
IT IS CURRENTLY NOT USED
"""
action = ""
amount = 0
def __init__(self, action, amount):
self.action = action
self.amount = amount
def __str__(self):
return "%s %.2f" % (self.action, self.amount)
class HistoryAccount(Account):
def __init__(self):
super().__init__()
self.historyList = []
def history(self):
return self.historyList
def deposit(self, amount):
super().deposit(amount)
self.historyList.append("deposit %.2f" % amount)
def withdraw(self, amount):
super().withdraw(amount)
self.historyList.append("withdraw %.2f" % amount)
def largewithdrawals(self):
"return all withdrawals over 10 dollars"
# this is called a list comprehension in python:
return [ x for x in self.historyList if "withdraw" in x ]
"""
Test for account.py.
There are several ways to run tests in python, running 'nose2' from
the command line is one of the easiest methods.
"""
import unittest
from account import Account, HistoryAccount, Transaction
class AccountTest(unittest.TestCase):
def test_withdrawal(self):
a = Account()
a.balance = 100
a.withdraw(40)
self.assertEqual(a.balance, 60)
class TransactionTest(unittest.TestCase):
def test_str(self):
t = Transaction("give", 12.3)
self.assertEqual(str(t), "give 12.30")
class HistoryTest(unittest.TestCase):
def test_hasBalance(self):
a = HistoryAccount()
self.assertEqual(a.balance, 0)
def test_hasEmptyHistory(self):
a = HistoryAccount()
history = a.history()
self.assertEqual(len(history), 0)
def test_hasHistory(self):
a = HistoryAccount()
a.deposit(100)
a.withdraw(40)
history = a.history()
self.assertEqual(len(history), 2)
self.assertEqual("deposit 100.00", history[0])
self.assertEqual("withdraw 40.00", history[1])
def test_hasLargeWithdrawals(self):
""""
TODO
this test does not work right now, it will return
2 transactions (both withdrawals) but does not filter out
the withdrawal of 4 dollars"""
a = HistoryAccount()
a.deposit(100)
a.withdraw(40)
a.withdraw(4)
largewithdrawals = a.largewithdrawals()
self.assertEqual(len(largewithdrawals), 1)
self.assertEqual("withdraw 40.00", largewithdrawals[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment