Finalize WL Management and fix merge func

kivy_mainline_and_py3.8
K0nstantine 2018-04-04 00:07:05 -04:00
parent 49b760673e
commit 918133f265
4 changed files with 13 additions and 17 deletions

View File

@ -121,7 +121,7 @@ def watch(loglevel, broker, rate, name):
log = get_console_log(loglevel) # activate console logging
brokermod = get_brokermod(broker)
watchlists = json.dumps({
watchlists_base = {
'cannabis': [
'EMH.VN', 'LEAF.TO', 'HVT.VN', 'HMMJ.TO', 'APH.TO',
'CBW.VN', 'TRST.CN', 'VFF.TO', 'ACB.TO', 'ABCN.VN',
@ -135,7 +135,9 @@ def watch(loglevel, broker, rate, name):
'dad': ['GM', 'TSLA', 'DOL.TO', 'CIM', 'SPY', 'SHOP.TO'],
'pharma': ['ATE.VN'],
'indexes': ['SPY', 'DAX', 'QQQ', 'DIA'],
})
}
watchlist_from_file = wl.ensure_watchlists(_watchlists_data_path)
watchlists = wl.merge_watchlist(watchlist_from_file, watchlists_base)
# broker_conf_path = os.path.join(
# click.get_app_dir('piker'), 'watchlists.json')
# from piker.testing import _quote_streamer as brokermod
@ -144,11 +146,9 @@ def watch(loglevel, broker, rate, name):
rate = broker_limit
log.warn(f"Limiting {brokermod.__name__} query rate to {rate}/sec")
trio.run(_async_main, name, watchlists[name], brokermod, rate)
# broker_conf_path = os.path.join(
# click.get_app_dir('piker'), 'watchlists.json')
# from piker.testing import _quote_streamer as brokermod
trio.run(_async_main, name, watchlists[name], brokermod)
@cli.group()
@ -216,7 +216,7 @@ def delete(ctx, name):
@click.argument('watchlist_to_merge', nargs=1, required=True)
@click.pass_context
def merge(ctx, watchlist_to_merge):
merged_watchlist = wl.merge_watchlist(watchlist_to_merge,
merged_watchlist = wl.merge_watchlist(json.loads(watchlist_to_merge),
ctx.obj['watchlist'])
wl.write_sorted_json(merged_watchlist, ctx.obj['path'])

View File

@ -40,14 +40,12 @@ def remove_ticker(name, ticker_name, watchlist):
def delete_group(name, watchlist):
if name in watchlist:
del watchlist[name]
watchlist.pop(name, None)
return watchlist
def merge_watchlist(watchlist_to_merge, watchlist):
merged_watchlist = defaultdict(list)
watchlist_to_merge = json.loads(watchlist_to_merge)
for d in (watchlist, watchlist_to_merge):
for key, value in d.items():
merged_watchlist[key].extend(value)

View File

@ -175,32 +175,30 @@ def test_group_deleted_from_watchlists(capfd, piker_dir):
def test_watchlists_loaded(capfd, piker_dir):
expected_out_text = json.dumps({
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
'pharma': ['ATE.VN'],
})
expected_out = {
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
'pharma': ['ATE.VN'],
}
expected_out_text = json.dumps(expected_out)
run(f"piker watchlists -d {piker_dir} load", expected_out_text)
out = wl.ensure_watchlists(piker_dir)
assert out == expected_out
def test_watchlists_is_merge(capfd, piker_dir):
def test_watchlists_are_merged(capfd, piker_dir):
orig_watchlist = {
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
'pharma': ['ATE.VN'],
}
list_to_merge = json.dumps({
'drugs': ['CRACK']
'drugs': ['CRACK'],
'pharma': ['ATE.VN', 'MALI', 'PERCOCET']
})
expected_out = {
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
'pharma': ['ATE.VN'],
'pharma': ['ATE.VN', 'MALI', 'PERCOCET'],
'drugs': ['CRACK']
}
wl.write_sorted_json(orig_watchlist, piker_dir)

View File

@ -96,6 +96,6 @@ def test_watchlist_is_merged():
"""Ensure that watchlist is merged.
"""
wl_temp = {'test': ['TEST.CN']}
wl_temp2 = '{"test2": ["TEST2.CN"]}'
wl_temp2 = {'test': ['TOAST'], "test2": ["TEST2.CN"]}
wl_temp3 = wl.merge_watchlist(wl_temp2, wl_temp)
assert wl_temp3 == {'test': ['TEST.CN'], 'test2': ['TEST2.CN']}
assert wl_temp3 == {'test': ['TEST.CN', 'TOAST'], 'test2': ['TEST2.CN']}