-
Notifications
You must be signed in to change notification settings - Fork 1
/
ppp.py
executable file
·94 lines (73 loc) · 2.33 KB
/
ppp.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env python3
import argparse
import os
import csv
import errno
import sys
sys.tracebacklimit=0
from datetime import datetime
def args():
parser = argparse.ArgumentParser()
parser.add_argument(
"amount",
type=int,
help="amount to adjust to Purchasing Power Parity",
)
parser.add_argument(
"country",
type=str,
help="country to use for adjustment",
)
parser.add_argument(
"year",
type=str,
nargs="?",
default=datetime.now().year,
help="year of reference for a stipend amounts table",
)
parser.add_argument(
"--reference", "-r",
type=str,
nargs="?",
help="country to use as reference",
default="Netherlands",
)
parser.add_argument(
"--ignore", "-i",
type=str,
help="do not adjust the amount if the country is listed in this file"
)
args = parser.parse_args()
return args
def compute_ratio(reference, country, data):
"""
compute PPP ratio against a default source for given country
"""
source_amount = int(next(x[1] for x in data if x[0] == reference))
target_amount = int(next(x[1] for x in data if x[0] == country))
return target_amount/source_amount
def main():
input = args()
base = os.getcwd()
file = f"{base}/{input.year}.csv"
if not os.path.isfile(file):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), f"'{input.year}.csv'. Please download a .csv file containing the stipend table amounts. Run `update.py` or see `README.md` for further instructions")
with open(file, "r") as table:
my_csv = csv.reader(table)
data = [row for row in my_csv]
countries = [row[0] for row in data[1:]]
if (not input.country in countries) or (not input.reference in countries):
print(f"'{input.country}' not in list. List of available countries:")
print(*countries, sep="\n")
return
if input.ignore:
with open(input.ignore, 'r') as file:
ignored = file.read().splitlines()
if input.country in ignored:
print(input.amount)
return
ratio = compute_ratio(input.reference, input.country, data)
output = ratio * input.amount
print(round(output))
if __name__ == "__main__":
main()