|
| 1 | +import sqlite3 |
| 2 | +import datetime |
| 3 | +import pytz |
| 4 | +import pickle |
| 5 | + |
| 6 | +db = sqlite3.connect("accounts.sqlite", detect_types=sqlite3.PARSE_DECLTYPES) |
| 7 | +db.execute("CREATE TABLE IF NOT EXISTS accounts (name TEXT PRIMARY KEY NOT NULL, balance INTEGER NOT NULL)") |
| 8 | +db.execute("CREATE TABLE IF NOT EXISTS history (time TIMESTAMP NOT NULL," |
| 9 | + " account TEXT NOT NULL, amount INTEGER NOT NULL," |
| 10 | + " zone INTEGER NOT NULL, PRIMARY KEY (time, account))") |
| 11 | +db.execute("CREATE VIEW IF NOT EXISTS localhistory AS" |
| 12 | + " SELECT strftime('%Y-%m-%d %H:%M:%f', history.time, 'localtime') as localtime, history.account," |
| 13 | + " history.amount FROM history ORDER BY history.time") |
| 14 | + |
| 15 | + |
| 16 | +class Account(object): |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def _current_time(): |
| 20 | + # return pytz.utc.localize(datetime.datetime.utcnow()) |
| 21 | + # local_time = pytz.utc.localize(datetime.datetime.utcnow()) |
| 22 | + # return local_time.astimezone() |
| 23 | + utc_time = pytz.utc.localize(datetime.datetime.utcnow()) |
| 24 | + local_time = utc_time.astimezone() |
| 25 | + zone = local_time.tzinfo |
| 26 | + return utc_time, zone |
| 27 | + |
| 28 | + def __init__(self, name: str, opening_balance: int = 0): |
| 29 | + cursor = db.execute("SELECT name, balance FROM accounts WHERE (name = ?)", (name,)) |
| 30 | + row = cursor.fetchone() |
| 31 | + |
| 32 | + if row: |
| 33 | + self.name, self._balance = row |
| 34 | + print(f"Retrieved record for {self.name}", end='') |
| 35 | + else: |
| 36 | + self.name = name |
| 37 | + self._balance = opening_balance |
| 38 | + cursor.execute("INSERT INTO accounts VALUES(?, ?)", (name, opening_balance)) |
| 39 | + cursor.connection.commit() |
| 40 | + print(f"Account created for {name}", end='') |
| 41 | + self.show_balance() |
| 42 | + |
| 43 | + def _save_update(self, amount): |
| 44 | + new_balance = self._balance + amount |
| 45 | + deposit_time, zone = Account._current_time() # <-- unpacking the return tuple |
| 46 | + picked_zone = pickle.dumps(zone) |
| 47 | + |
| 48 | + db.execute("UPDATE accounts SET balance = ? WHERE (name = ?)", (new_balance, self.name)) |
| 49 | + db.execute("INSERT INTO history VALUES(?, ?, ?, ?)", (deposit_time, self.name, amount, picked_zone)) |
| 50 | + db.commit() |
| 51 | + self._balance = new_balance |
| 52 | + |
| 53 | + def deposit(self, amount: int) -> float: |
| 54 | + if amount > 0.0: |
| 55 | + # new_balance = self._balance + amount |
| 56 | + # deposit_time = Account._current_time() |
| 57 | + # db.execute("UPDATE accounts SET balance = ? WHERE (name = ?)", (new_balance, self.name)) |
| 58 | + # db.execute("INSERT INTO history VALUES(?, ?, ?)", (deposit_time, self.name, amount)) |
| 59 | + # db.commit() |
| 60 | + # self._balance = new_balance |
| 61 | + self._save_update(amount) |
| 62 | + print(f"{amount / 100:.2f} deposited") |
| 63 | + return self._balance / 100 |
| 64 | + |
| 65 | + def withdraw(self, amount: int) -> float: |
| 66 | + if 0 < amount <= self._balance: |
| 67 | + # new_balance = self._balance - amount |
| 68 | + # withdrawal_time = Account._current_time() |
| 69 | + # db.execute("UPDATE accounts SET balance = ? WHERE (name = ?)", (new_balance, self.name)) |
| 70 | + # db.execute("INSERT INTO history VALUES(?, ?, ?)", (withdrawal_time, self.name, -amount)) |
| 71 | + # db.commit() |
| 72 | + # self._balance = new_balance |
| 73 | + self._save_update(-amount) |
| 74 | + print(f"{amount / 100:.2f} withdrawn") |
| 75 | + return amount / 100 |
| 76 | + else: |
| 77 | + print("The amount must be greater than zero and no more than your account balance") |
| 78 | + return 0.0 |
| 79 | + |
| 80 | + def show_balance(self): |
| 81 | + print(f"Balance on account {self.name} is {self._balance / 100:.2f}") |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + john = Account("John") |
| 86 | + john.deposit(1010) |
| 87 | + john.deposit(10) |
| 88 | + john.deposit(10) |
| 89 | + john.withdraw(30) |
| 90 | + john.withdraw(0) |
| 91 | + john.show_balance() |
| 92 | + |
| 93 | + terry = Account("TerryJ") |
| 94 | + graham = Account("Graham", 9000) |
| 95 | + eric = Account("Eric", 7000) |
| 96 | + michael = Account("Michael") |
| 97 | + terryG = Account("TerryG") |
| 98 | + # db.execute("DROP TABLE accounts") |
| 99 | + # db.execute("DROP TABLE history") |
| 100 | + db.close() |
0 commit comments