Skip to content

Commit

Permalink
Update cryptocurrency example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Oct 9, 2020
1 parent dc79c3c commit 782bf03
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions examples/cryptocurrency
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from appdirs import user_config_dir
import nestedtext as nt
from voluptuous import Schema, Required, All, Length, Invalid
from inform import display, fatal, is_collection, os_error, render_bar
from voluptuous import Schema, Required, All, Length, Invalid, Coerce
from inform import display, fatal, is_collection, os_error, render_bar, full_stop
import arrow
import requests
from quantiphy import Quantity
Expand All @@ -13,25 +13,14 @@ from pathlib import Path
Quantity.set_prefs(prec=2, ignore_sf = True)
currency_symbols = dict(USD='$', EUR='€', JPY='¥', GBP='£')

# utility functions
def coerce(type):
def f(value):
try:
if is_collection(value):
return [type(each) for each in value]
return type(value)
except ValueError:
raise Invalid(f'expected {type.__name__}, found {v.__class__.__name__}')
return f

try:
# read settings
settings_file = Path(user_config_dir('cc'), 'settings')
settings_schema = Schema({
Required('holdings'): All(coerce(Quantity), Length(min=1)),
Required('holdings'): All([Coerce(Quantity)], Length(min=1)),
'currency': str,
'date format': str,
'screen width': coerce(int)
'screen width': Coerce(int)
})
settings = settings_schema(nt.load(settings_file, top='dict'))
currency = settings.get('currency', 'USD')
Expand Down Expand Up @@ -74,7 +63,7 @@ try:
except nt.NestedTextError as e:
e.terminate()
except Invalid as e:
fatal(e)
fatal(full_stop(e.msg), culprit=e.path)
except OSError as e:
fatal(os_error(e))
except KeyboardInterrupt:
Expand Down

0 comments on commit 782bf03

Please sign in to comment.