-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
59 lines (46 loc) · 1.84 KB
/
database.py
File metadata and controls
59 lines (46 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
"""
Author: Nick Russo
Purpose: A simple Flask web app that demonstrates the Model View Controller
(MVC) pattern in a meaningful and somewhat realistic way.
"""
class Database:
"""
Represent the interface to the data (model). Can read from a
simple file such as JSON, YAML, or XML. Uses JSON by default.
"""
def __init__(self, path):
"""
Constructor to initialize the data attribute as
a dictionary where the account number is the key and
the value is another dictionary with keys "paid" and "due".
"""
# Open the specified database file for reading and perform loading
with open(path, "r") as handle:
import json
self.data = json.load(handle)
# ALTERNATIVE IMPLEMENTATIONS: Using YAML or XML to load data
# import yaml
# self.data = yaml.safe_load(handle)
# import xmltodict
# self.data = xmltodict.parse(handle.read())["root"]
# print(self.data)
def balance(self, acct_id):
"""
Determines the customer balance by finding the difference between
what has been paid and what is still owed on the account, The "model"
can provide methods to help interface with the data; it is not
limited to only storing data. A positive number means the customer
owes us money and a negative number means they overpaid and have
a credit with us.
"""
acct = self.data.get(acct_id)
if acct:
bal = float(acct["due"]) - float(acct["paid"])
# Style added in module 4
return f"{bal:.2f} USD"
# Style added in module 3
# return f"$ {bal:.2f}"
# Original style in module 2
# return int(acct["due"]) - int(acct["paid"])
return None