2020-03-27 20:14:22 +04:00
|
|
|
"""Transaction class implementation."""
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
from __future__ import annotations
|
2020-06-04 03:12:31 +04:00
|
|
|
from operator import add, sub, mul
|
2019-12-25 23:08:20 +04:00
|
|
|
from time import strftime, strptime, gmtime
|
2020-06-04 03:12:31 +04:00
|
|
|
from typing import Optional, List, Union
|
2019-12-25 23:08:20 +04:00
|
|
|
|
2020-07-07 02:28:33 +04:00
|
|
|
from piggybank import CURRENCIES
|
2019-12-25 23:08:20 +04:00
|
|
|
|
2020-06-07 20:41:17 +04:00
|
|
|
__all__ = ["Transaction", "sum_transactions",
|
2020-06-04 03:12:31 +04:00
|
|
|
"TYPE_INCOME", "TYPE_OUTCOME", "TIME_FORMAT"]
|
|
|
|
|
2020-06-05 03:51:29 +04:00
|
|
|
|
2020-06-04 03:12:31 +04:00
|
|
|
TYPE_INCOME = "i"
|
|
|
|
TYPE_OUTCOME = "o"
|
2019-12-25 23:08:20 +04:00
|
|
|
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
|
|
|
|
|
|
|
|
|
2020-06-04 03:12:31 +04:00
|
|
|
def sum_transactions(transactions: List[Transaction]) -> List[int]:
|
|
|
|
"""Sums all the coins and returns resulting list. All transactions must have
|
|
|
|
same length of coins' list."""
|
|
|
|
coins = [0] * len(transactions[0].coins)
|
|
|
|
for transaction in transactions:
|
|
|
|
if transaction.direction == TYPE_INCOME:
|
2020-06-05 03:51:29 +04:00
|
|
|
coins = list(map(add, coins, transaction.coins))
|
2020-06-04 03:12:31 +04:00
|
|
|
else:
|
2020-06-05 03:51:29 +04:00
|
|
|
coins = list(map(sub, coins, transaction.coins))
|
2020-06-04 03:12:31 +04:00
|
|
|
return coins
|
|
|
|
|
|
|
|
|
2019-12-25 23:08:20 +04:00
|
|
|
class Transaction:
|
2020-06-04 03:12:31 +04:00
|
|
|
"""An object that holds a single transaction.
|
|
|
|
|
2020-05-19 02:36:00 +04:00
|
|
|
Arguments:
|
2020-06-04 03:12:31 +04:00
|
|
|
- coins -- a list of numbers represent count for each face value;
|
2020-05-19 02:36:00 +04:00
|
|
|
- direction -- is this income or outcome. Takes TYPE_INCOME
|
|
|
|
or TYPE_OUTCOME;
|
|
|
|
- timestamp -- date and time formated accordingly to TIME_FORMAT."""
|
|
|
|
def __init__(self, coins: List[int], direction: str = TYPE_INCOME,
|
2019-12-25 23:08:20 +04:00
|
|
|
timestamp: Optional[str] = None) -> None:
|
|
|
|
self.coins = coins
|
2020-06-04 03:12:31 +04:00
|
|
|
self.direction = direction
|
|
|
|
self.timestamp = timestamp
|
|
|
|
|
|
|
|
@property
|
|
|
|
def coins(self) -> List[int]:
|
|
|
|
return self._coins
|
|
|
|
|
|
|
|
@coins.setter
|
|
|
|
def coins(self, coins: List[int]) -> None:
|
2020-06-05 03:51:29 +04:00
|
|
|
if type(coins) is list:
|
2020-06-04 03:12:31 +04:00
|
|
|
self._coins = coins
|
|
|
|
else:
|
|
|
|
raise TypeError("Coins must be of type 'list'.")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def direction(self) -> str:
|
|
|
|
return self._direction
|
|
|
|
|
|
|
|
@direction.setter
|
|
|
|
def direction(self, direction: str = TYPE_INCOME) -> None:
|
2019-12-25 23:08:20 +04:00
|
|
|
if direction in [TYPE_INCOME, TYPE_OUTCOME]:
|
2020-06-04 03:12:31 +04:00
|
|
|
self._direction = direction
|
2019-12-25 23:08:20 +04:00
|
|
|
else:
|
2020-06-04 03:12:31 +04:00
|
|
|
raise ValueError("Direction may only be of TYPE_INCOME(\"i\") " \
|
|
|
|
"or TYPE_OUTCOME(\"o\").")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def timestamp(self) -> str:
|
|
|
|
return self._timestamp
|
|
|
|
|
|
|
|
@timestamp.setter
|
|
|
|
def timestamp(self, timestamp: str = None) -> None:
|
2019-12-25 23:08:20 +04:00
|
|
|
if timestamp is None:
|
2020-06-04 03:12:31 +04:00
|
|
|
self._timestamp = strftime(TIME_FORMAT, gmtime())
|
2019-12-25 23:08:20 +04:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
strptime(timestamp, TIME_FORMAT)
|
|
|
|
except ValueError:
|
2020-06-04 03:12:31 +04:00
|
|
|
raise ValueError(f"Timestamp {timestamp} has wrong format. " \
|
|
|
|
f"The right one is \"{TIME_FORMAT}\".")
|
|
|
|
self._timestamp = timestamp
|
2019-12-25 23:08:20 +04:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_string(transaction: str) -> Transaction:
|
|
|
|
"""Makes a Transaction object from its string output."""
|
|
|
|
timestamp, direction, coins = transaction.split()
|
2020-05-19 02:36:00 +04:00
|
|
|
coins = list(map(int, coins.split(",")))
|
2019-12-25 23:08:20 +04:00
|
|
|
return Transaction(coins, direction, timestamp)
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return f"{self.timestamp} {self.direction} " \
|
2020-06-04 03:12:31 +04:00
|
|
|
f"{','.join(map(str, self.coins))}"
|