-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinflation.py
More file actions
executable file
·64 lines (49 loc) · 1.65 KB
/
inflation.py
File metadata and controls
executable file
·64 lines (49 loc) · 1.65 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
60
61
62
63
64
#!/usr/bin/env python3
"""
@file inflation.py
@brief Calculates US inflation.
@author Evan Elias Young
@date 2017-09-06
@date 2022-02-04
@copyright Copyright 2022 Evan Elias Young. All rights reserved.
"""
import sys
from datetime import date
import requests as req
from bs4 import BeautifulSoup as bs
from dateutil.relativedelta import relativedelta as datedelta
def ask_for_past() -> str:
"""Will ask for a past date.
Returns:
string: The date in url format YYYY-MM.
"""
year: int = int(input("Year (after 1912): "))
if year <= 1912 or year >= date.today().year:
sys.exit()
month: int = 1
return f"{year}{month:0>2}"
def get_inflation(amount: float, past: str) -> float:
"""Will calculate the inflation between now and then.
Args:
amount (float): The amount of money to inflate.
past (string): The past date.
Returns:
float: The money calculated with inflation.
"""
cur_date: date = date.today() - datedelta(months=2)
now: str = f"{cur_date.year}{cur_date.month:0>2}"
res = req.get(
f"https://data.bls.gov/cgi-bin/cpicalc.pl?cost1={amount}&year1={past}&year2={now}"
)
soup = bs(res.text, "html.parser")
ans: str = soup.find(id="answer").string
return float(ans[1:].replace(",", ""))
if __name__ == "__main__":
from random import randint
print("Hello Console!")
amt: float = randint(0, 1000) + (randint(0, 99) / 100)
cur_year: int = date.today().year
yr: int = randint(1913, cur_year - 1)
then: str = f"{yr}01"
print(f"{yr} -> {cur_year}")
print(f"${amt:0.2f} -> ${get_inflation(amt, then):0.2f}")