Finalize WL Management and fix merge func
parent
49b760673e
commit
918133f265
10
piker/cli.py
10
piker/cli.py
|
@ -121,7 +121,7 @@ def watch(loglevel, broker, rate, name):
|
||||||
log = get_console_log(loglevel) # activate console logging
|
log = get_console_log(loglevel) # activate console logging
|
||||||
brokermod = get_brokermod(broker)
|
brokermod = get_brokermod(broker)
|
||||||
|
|
||||||
watchlists = json.dumps({
|
watchlists_base = {
|
||||||
'cannabis': [
|
'cannabis': [
|
||||||
'EMH.VN', 'LEAF.TO', 'HVT.VN', 'HMMJ.TO', 'APH.TO',
|
'EMH.VN', 'LEAF.TO', 'HVT.VN', 'HMMJ.TO', 'APH.TO',
|
||||||
'CBW.VN', 'TRST.CN', 'VFF.TO', 'ACB.TO', 'ABCN.VN',
|
'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'],
|
'dad': ['GM', 'TSLA', 'DOL.TO', 'CIM', 'SPY', 'SHOP.TO'],
|
||||||
'pharma': ['ATE.VN'],
|
'pharma': ['ATE.VN'],
|
||||||
'indexes': ['SPY', 'DAX', 'QQQ', 'DIA'],
|
'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(
|
# broker_conf_path = os.path.join(
|
||||||
# click.get_app_dir('piker'), 'watchlists.json')
|
# click.get_app_dir('piker'), 'watchlists.json')
|
||||||
# from piker.testing import _quote_streamer as brokermod
|
# from piker.testing import _quote_streamer as brokermod
|
||||||
|
@ -144,11 +146,9 @@ def watch(loglevel, broker, rate, name):
|
||||||
rate = broker_limit
|
rate = broker_limit
|
||||||
log.warn(f"Limiting {brokermod.__name__} query rate to {rate}/sec")
|
log.warn(f"Limiting {brokermod.__name__} query rate to {rate}/sec")
|
||||||
trio.run(_async_main, name, watchlists[name], brokermod, rate)
|
trio.run(_async_main, name, watchlists[name], brokermod, rate)
|
||||||
|
|
||||||
# broker_conf_path = os.path.join(
|
# broker_conf_path = os.path.join(
|
||||||
# click.get_app_dir('piker'), 'watchlists.json')
|
# click.get_app_dir('piker'), 'watchlists.json')
|
||||||
# from piker.testing import _quote_streamer as brokermod
|
# from piker.testing import _quote_streamer as brokermod
|
||||||
trio.run(_async_main, name, watchlists[name], brokermod)
|
|
||||||
|
|
||||||
|
|
||||||
@cli.group()
|
@cli.group()
|
||||||
|
@ -216,7 +216,7 @@ def delete(ctx, name):
|
||||||
@click.argument('watchlist_to_merge', nargs=1, required=True)
|
@click.argument('watchlist_to_merge', nargs=1, required=True)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def merge(ctx, watchlist_to_merge):
|
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'])
|
ctx.obj['watchlist'])
|
||||||
wl.write_sorted_json(merged_watchlist, ctx.obj['path'])
|
wl.write_sorted_json(merged_watchlist, ctx.obj['path'])
|
||||||
|
|
||||||
|
|
|
@ -40,14 +40,12 @@ def remove_ticker(name, ticker_name, watchlist):
|
||||||
|
|
||||||
|
|
||||||
def delete_group(name, watchlist):
|
def delete_group(name, watchlist):
|
||||||
if name in watchlist:
|
watchlist.pop(name, None)
|
||||||
del watchlist[name]
|
|
||||||
return watchlist
|
return watchlist
|
||||||
|
|
||||||
|
|
||||||
def merge_watchlist(watchlist_to_merge, watchlist):
|
def merge_watchlist(watchlist_to_merge, watchlist):
|
||||||
merged_watchlist = defaultdict(list)
|
merged_watchlist = defaultdict(list)
|
||||||
watchlist_to_merge = json.loads(watchlist_to_merge)
|
|
||||||
for d in (watchlist, watchlist_to_merge):
|
for d in (watchlist, watchlist_to_merge):
|
||||||
for key, value in d.items():
|
for key, value in d.items():
|
||||||
merged_watchlist[key].extend(value)
|
merged_watchlist[key].extend(value)
|
||||||
|
|
|
@ -175,32 +175,30 @@ def test_group_deleted_from_watchlists(capfd, piker_dir):
|
||||||
|
|
||||||
|
|
||||||
def test_watchlists_loaded(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 = {
|
expected_out = {
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
||||||
'pharma': ['ATE.VN'],
|
'pharma': ['ATE.VN'],
|
||||||
}
|
}
|
||||||
|
expected_out_text = json.dumps(expected_out)
|
||||||
run(f"piker watchlists -d {piker_dir} load", expected_out_text)
|
run(f"piker watchlists -d {piker_dir} load", expected_out_text)
|
||||||
out = wl.ensure_watchlists(piker_dir)
|
out = wl.ensure_watchlists(piker_dir)
|
||||||
assert out == expected_out
|
assert out == expected_out
|
||||||
|
|
||||||
|
|
||||||
def test_watchlists_is_merge(capfd, piker_dir):
|
def test_watchlists_are_merged(capfd, piker_dir):
|
||||||
orig_watchlist = {
|
orig_watchlist = {
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
||||||
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
||||||
'pharma': ['ATE.VN'],
|
'pharma': ['ATE.VN'],
|
||||||
}
|
}
|
||||||
list_to_merge = json.dumps({
|
list_to_merge = json.dumps({
|
||||||
'drugs': ['CRACK']
|
'drugs': ['CRACK'],
|
||||||
|
'pharma': ['ATE.VN', 'MALI', 'PERCOCET']
|
||||||
})
|
})
|
||||||
expected_out = {
|
expected_out = {
|
||||||
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
'dad': ['CIM', 'DOL.TO', 'GM', 'SHOP.TO', 'SPY', 'TSLA'],
|
||||||
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
'indexes': ['DAX', 'DIA', 'QQQ', 'SPY'],
|
||||||
'pharma': ['ATE.VN'],
|
'pharma': ['ATE.VN', 'MALI', 'PERCOCET'],
|
||||||
'drugs': ['CRACK']
|
'drugs': ['CRACK']
|
||||||
}
|
}
|
||||||
wl.write_sorted_json(orig_watchlist, piker_dir)
|
wl.write_sorted_json(orig_watchlist, piker_dir)
|
||||||
|
|
|
@ -96,6 +96,6 @@ def test_watchlist_is_merged():
|
||||||
"""Ensure that watchlist is merged.
|
"""Ensure that watchlist is merged.
|
||||||
"""
|
"""
|
||||||
wl_temp = {'test': ['TEST.CN']}
|
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)
|
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']}
|
||||||
|
|
Loading…
Reference in New Issue