Created
December 7, 2011 19:53
-
-
Save tlmaloney/1444333 to your computer and use it in GitHub Desktop.
Payment class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on Nov 25, 2011 | |
@author: tlmaloney | |
''' | |
class Payment(object): | |
''' | |
A payment is the transfer of ownership and possession of an amount of asset from Agent A to | |
Agent B on a specific date. | |
''' | |
def __init__(self, date, asset, amount, receiver, payer): | |
''' | |
Constructor | |
Keyword arguments: | |
date -- the payment date, SerialDate instance | |
asset -- the asset being used as payment, Asset instance | |
amount -- amount of the asset being transferred | |
receiver -- the receiving Agent, Agent instance | |
payer -- the paying Agent, Agent instance | |
''' | |
self.date = date | |
self.asset = asset | |
self.amount = amount | |
self.receiver = receiver | |
self.payer = payer | |
class CurrencyPayment(Payment): | |
''' | |
A CurrencyPayment is the transfer of amount of Currency from Agent A | |
to Agent B on a specific date. | |
''' | |
def __init__(self, date, currency, amount, receiver, payer): | |
''' | |
Constructor | |
Keyword arguments: | |
date -- the payment date, SerialDate instance | |
currency -- the Asset being paid, Currency instance | |
amount -- the number of units of currency, non-negative integer | |
receiver -- the receiving Agent, Agent instance | |
payer -- the paying Agent, Agent instance | |
''' | |
self.date = date | |
self.currency = currency | |
self.amount = amount | |
self.receiver = receiver | |
self.payer = payer | |
def __eq__(self, payment): | |
""" | |
Equal should be based on the amount and asset, and date | |
Keyword arguments: | |
payment -- Payment instance | |
""" | |
try: | |
return self.amount == payment.amount and self.date == payment.date and self.asset == payment.asset | |
except Exception: | |
return False | |
def __ne__(self, payment): | |
return not self.__eq__(payment) | |
def __repr__(self): | |
"""Return a string representation of this class. | |
""" | |
return self.__class__.__name__ + "(" + repr(self.date) + ", " + repr(self.asset) + ", " + repr(self.amount) + ", " + repr(self.receiver) + ", " + repr(self.payer) + ")" | |
def __str__(self): | |
"""Return a human-friendly string for this class. | |
""" | |
return "The currency payment is " + str(self.amount) + " of " + str(self.asset) + " as of " + str(self.date) + " from " + str(self.payer) + " and to " + str(self.receiver) | |
class Annuity(object): | |
''' | |
An annuity is a collection of CurrencyPayment objects. Let the collection | |
be a dictionary keyed by date. Assume all payments have the same payer and | |
receiver, respectively. | |
''' | |
def __init__(self, currency_payments): | |
''' | |
Constructor | |
Keyword arguments: | |
currency_payments -- a dictionary of CurrencyPayment objects | |
''' | |
self.currency_payments = currency_payments | |
def set_payment(self, payment): | |
''' | |
Add/changes a payment | |
Keyword arguments: | |
payment -- Currency payment instance | |
''' | |
self.currency_payments[payment.date] = payment | |
def get_payment(self, date): | |
''' | |
Returns a payment on a specific date | |
Keyword arguments: | |
date -- SerialDate instance | |
''' | |
return self.currency_payments[date] | |
def make_payment(date, asset, amount, receiver, payer): | |
''' | |
Makes a new Payment instance | |
Keyword arguments: | |
date -- the payment date, SerialDate instance | |
asset -- the asset being used as payment, Asset instance | |
amount -- the amount of asset | |
receiver -- the receiving Agent, Agent instance | |
payer -- the paying Agent, Agent instance | |
''' | |
return Payment(date, asset, amount, receiver, payer) | |
def make_currency_payment(date, currency, amount, receiver=None, payer=None): | |
''' | |
Makes a new CurrencyPayment instance | |
Keyword arguments: | |
date -- the payment date, SerialDate instance | |
currency -- the currency being used as payment, Currency instance | |
amount -- the number of units of currency, non-negative integer | |
receiver -- the receiving Agent, Agent instance | |
payer -- the paying Agent, Agent instance | |
''' | |
return CurrencyPayment(date, currency, amount, receiver, payer) | |
def make_annuity(currency_payments={}): | |
''' | |
Makes a new Annuity instance | |
Keyword arguments: | |
currency_payments -- a dictionary of CurrencyPayment objects | |
''' | |
return Annuity(currency_payments) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Created on Nov 28, 2011 | |
@author: tlmaloney | |
''' | |
import unittest | |
import Payment | |
import fimero.time.SerialDate as SD | |
import Agent | |
import Asset | |
date = SD.make(20110101) | |
asset = Asset.make('Asset1') | |
amount = 2.0 | |
receiver = Agent.make(2, 'agent2') | |
payer = Agent.make(1 , 'agent1') | |
payment = Payment.make_payment(date, asset, amount, receiver, payer) | |
currency = Asset.make('Currency') | |
currency_payment = Payment.make_currency_payment(date, currency, amount, receiver, payer) | |
currency_payments = { date : currency_payment } | |
annuity = Payment.make_annuity(currency_payments) | |
class TestPayment(unittest.TestCase): | |
def test_payment(self): | |
self.assertEqual(payment.asset, asset) | |
self.assertEqual(payment.date, date) | |
self.assertEqual(payment.amount, amount) | |
self.assertEqual(payment.receiver, receiver) | |
self.assertEqual(payment.payer, payer) | |
def test_currency(self): | |
self.assertEqual(currency_payment.currency, currency) | |
self.assertEqual(currency_payment.date, date) | |
self.assertEqual(currency_payment.amount, amount) | |
self.assertEqual(currency_payment.receiver, receiver) | |
self.assertEqual(currency_payment.payer, payer) | |
def test_annuity_payment(self): | |
self.assertEqual(annuity.currency_payments, currency_payments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment