Skip to content

Instantly share code, notes, and snippets.

@blinks
Created September 19, 2019 16:39
Show Gist options
  • Save blinks/80979ba6817cef3888696d4066e5e8af to your computer and use it in GitHub Desktop.
Save blinks/80979ba6817cef3888696d4066e5e8af to your computer and use it in GitHub Desktop.
A simple currency board tracker for For-Ex.
#!/usr/bin/env python3
# A simple currency board tracker for For-Ex.
# Needs `pip3 install colorama` and normal Python3.
from collections import defaultdict
from colorama import Fore, Style
import os
currency = {
'GBP': Fore.RED,
'EUR': Fore.BLUE,
'USD': Fore.GREEN,
'CHF': Fore.MAGENTA,
'JPY': Fore.WHITE,
'CAD': Style.DIM + Fore.YELLOW,
'CNY': Style.NORMAL + Fore.YELLOW,
}
# Initial exchange rates, as counters on the table.
init = [
('GBP', 1, 'EUR'),
('GBP', 1, 'CHF'),
('GBP', 1, 'JPY'),
('GBP', 2, 'CAD'),
('GBP', 6, 'CNY'),
('EUR', 1, 'JPY'), ('EUR', 1, 'CAD'), ('EUR', 5, 'CNY'),
('USD', 1, 'JPY'), ('USD', 1, 'CAD'), ('USD', 5, 'CNY'),
('CHF', 1, 'JPY'), ('CHF', 1, 'CAD'), ('CHF', 5, 'CNY'),
('JPY', 1, 'CAD'), ('JPY', 4, 'CNY'),
('CAD', 3, 'CNY'),
]
def main(args):
exchange = defaultdict(lambda: defaultdict(int))
for (a, x, b) in init:
exchange[a][b] = x
exchange[b][a] = -x
while True:
os.system('cls||clear')
board(exchange)
print()
print('Strengthen(+) // Weaken(-) + Currency (+USD // -GBP)')
arg = input('> ').strip()
d, self = arg[0], arg[1:].strip().upper()
if d not in '-+' or self not in currency: continue
d = 1 if d == '+' else -1
for other in currency:
val = exchange[self][other]
val += d
# Clamp to the extremes.
val = max(-10, min(10, val))
# Skip zero -> 1:1 has two spaces.
if val == 0: val = d
# Denormalize to both exchange rates.
exchange[self][other] = val
exchange[other][self] = -val
def board(exchange):
cell = '{:^5}'
fmt = lambda name: currency[name] + cell.format(name) + Style.RESET_ALL
table = '{:^5} : {:^5} | {:^5} | {:^5} | {:^5} | {:^5} | {:^5} | {:^5} | {:^5}'
print(table.format('$', 1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 6, 8))
for self in currency:
print('-' * 70)
# Fill out the row of weaker currencies.
row = [[], [], [], [], [], [], [], []]
for other in currency:
if self is other: continue
i = exchange[self][other] - 1
if i < 0: continue
row[i].append(other)
# Print rows one at a time.
empty = True
for i in range(len(currency)):
inner = [fmt(col[i]) if i < len(col) else ' ' for col in row]
line = ' | '.join([fmt(self)] + inner)
if ''.join(inner).strip() == '':
if empty: print(line)
break # no more rows to print
print(line)
empty = False
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment