From 918133f265e7de283c842da989a1f4f4a0c42618 Mon Sep 17 00:00:00 2001 From: K0nstantine Date: Wed, 4 Apr 2018 00:07:05 -0400 Subject: [PATCH] Finalize WL Management and fix merge func --- piker/cli.py | 10 +++++----- piker/watchlists.py | 4 +--- tests/test_cli.py | 12 +++++------- tests/test_watchlists.py | 4 ++-- 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/piker/cli.py b/piker/cli.py index ad254e57..65377937 100644 --- a/piker/cli.py +++ b/piker/cli.py @@ -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']) diff --git a/piker/watchlists.py b/piker/watchlists.py index b440b5f1..ae777904 100644 --- a/piker/watchlists.py +++ b/piker/watchlists.py @@ -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) diff --git a/tests/test_cli.py b/tests/test_cli.py index 8453e794..10e5668f 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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) diff --git a/tests/test_watchlists.py b/tests/test_watchlists.py index 8e8e7f70..f198860f 100644 --- a/tests/test_watchlists.py +++ b/tests/test_watchlists.py @@ -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']}