aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-23 23:33:36 +0100
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-03-24 10:39:52 +0100
commit35667b31ddf1ce47a56ccbf4db9896dbc165ad0a (patch)
tree758fa5203849f284bd8379d8928b22dfbbbc699e
parent88e486a61fd563b3d30d34e1962269eae5a931bf (diff)
downloadTrader-35667b31ddf1ce47a56ccbf4db9896dbc165ad0a.tar.gz
Trader-35667b31ddf1ce47a56ccbf4db9896dbc165ad0a.tar.zst
Trader-35667b31ddf1ce47a56ccbf4db9896dbc165ad0a.zip
Cleanup market from_config
-rw-r--r--main.py20
-rw-r--r--market.py17
-rw-r--r--test.py10
3 files changed, 21 insertions, 26 deletions
diff --git a/main.py b/main.py
index 3e98289..4462192 100644
--- a/main.py
+++ b/main.py
@@ -62,9 +62,11 @@ def make_order(market, value, currency, action="acquire",
62 62
63def get_user_market(config_path, user_id, debug=False): 63def get_user_market(config_path, user_id, debug=False):
64 pg_config, report_path = parse_config(config_path) 64 pg_config, report_path = parse_config(config_path)
65 market_config = list(fetch_markets(pg_config, str(user_id)))[0][1] 65 market_id, market_config, user_id = list(fetch_markets(pg_config, str(user_id)))[0]
66 args = type('Args', (object,), { "debug": debug, "quiet": False })() 66 args = type('Args', (object,), { "debug": debug, "quiet": False })()
67 return market.Market.from_config(market_config, args, user_id=user_id, report_path=report_path) 67 return market.Market.from_config(market_config, args,
68 pg_config=pg_config, market_id=market_id,
69 user_id=user_id, report_path=report_path)
68 70
69def fetch_markets(pg_config, user): 71def fetch_markets(pg_config, user):
70 connection = psycopg2.connect(**pg_config) 72 connection = psycopg2.connect(**pg_config)
@@ -132,7 +134,7 @@ def parse_args(argv):
132 134
133 return args 135 return args
134 136
135def process(market_id, market_config, user_id, report_path, args, pg_config): 137def process(market_config, market_id, user_id, args, report_path, pg_config):
136 try: 138 try:
137 market.Market\ 139 market.Market\
138 .from_config(market_config, args, 140 .from_config(market_config, args,
@@ -151,13 +153,13 @@ def main(argv):
151 import threading 153 import threading
152 market.Portfolio.start_worker() 154 market.Portfolio.start_worker()
153 155
154 for row in fetch_markets(pg_config, args.user): 156 def process_(*args):
155 threading.Thread(target=process, args=[ 157 threading.Thread(target=process, args=args).start()
156 *row, report_path, args, pg_config
157 ]).start()
158 else: 158 else:
159 for row in fetch_markets(pg_config, args.user): 159 process_ = process
160 process(*row, report_path, args, pg_config) 160
161 for market_id, market_config, user_id in fetch_markets(pg_config, args.user):
162 process_(market_config, market_id, user_id, args, report_path, pg_config)
161 163
162if __name__ == '__main__': # pragma: no cover 164if __name__ == '__main__': # pragma: no cover
163 main(sys.argv[1:]) 165 main(sys.argv[1:])
diff --git a/market.py b/market.py
index 78ced1a..496ec45 100644
--- a/market.py
+++ b/market.py
@@ -14,9 +14,7 @@ class Market:
14 trades = None 14 trades = None
15 balances = None 15 balances = None
16 16
17 def __init__(self, ccxt_instance, args, 17 def __init__(self, ccxt_instance, args, **kwargs):
18 user_id=None, market_id=None,
19 report_path=None, pg_config=None):
20 self.args = args 18 self.args = args
21 self.debug = args.debug 19 self.debug = args.debug
22 self.ccxt = ccxt_instance 20 self.ccxt = ccxt_instance
@@ -26,14 +24,11 @@ class Market:
26 self.balances = BalanceStore(self) 24 self.balances = BalanceStore(self)
27 self.processor = Processor(self) 25 self.processor = Processor(self)
28 26
29 self.user_id = user_id 27 for key in ["user_id", "market_id", "report_path", "pg_config"]:
30 self.market_id = market_id 28 setattr(self, key, kwargs.get(key, None))
31 self.report_path = report_path
32 self.pg_config = pg_config
33 29
34 @classmethod 30 @classmethod
35 def from_config(cls, config, args, 31 def from_config(cls, config, args, **kwargs):
36 user_id=None, market_id=None, report_path=None, pg_config=None):
37 config["apiKey"] = config.pop("key", None) 32 config["apiKey"] = config.pop("key", None)
38 33
39 ccxt_instance = ccxt.poloniexE(config) 34 ccxt_instance = ccxt.poloniexE(config)
@@ -50,9 +45,7 @@ class Market:
50 ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session, 45 ccxt_instance.session.request = request_wrap.__get__(ccxt_instance.session,
51 ccxt_instance.session.__class__) 46 ccxt_instance.session.__class__)
52 47
53 return cls(ccxt_instance, args, 48 return cls(ccxt_instance, args, **kwargs)
54 user_id=user_id, market_id=market_id,
55 pg_config=pg_config, report_path=report_path)
56 49
57 def store_report(self): 50 def store_report(self):
58 self.report.merge(Portfolio.report) 51 self.report.merge(Portfolio.report)
diff --git a/test.py b/test.py
index 5b9c56c..637a305 100644
--- a/test.py
+++ b/test.py
@@ -3682,7 +3682,7 @@ class MainTest(WebMockTestCase):
3682 args_mock.after = "after" 3682 args_mock.after = "after"
3683 self.assertEqual("", stdout_mock.getvalue()) 3683 self.assertEqual("", stdout_mock.getvalue())
3684 3684
3685 main.process(3, "config", 1, "report_path", args_mock, "pg_config") 3685 main.process("config", 3, 1, args_mock, "report_path", "pg_config")
3686 3686
3687 market_mock.from_config.assert_has_calls([ 3687 market_mock.from_config.assert_has_calls([
3688 mock.call("config", args_mock, pg_config="pg_config", market_id=3, user_id=1, report_path="report_path"), 3688 mock.call("config", args_mock, pg_config="pg_config", market_id=3, user_id=1, report_path="report_path"),
@@ -3719,8 +3719,8 @@ class MainTest(WebMockTestCase):
3719 3719
3720 self.assertEqual(2, process.call_count) 3720 self.assertEqual(2, process.call_count)
3721 process.assert_has_calls([ 3721 process.assert_has_calls([
3722 mock.call(3, "config1", 1, "report_path", args_mock, "pg_config"), 3722 mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"),
3723 mock.call(1, "config2", 2, "report_path", args_mock, "pg_config"), 3723 mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"),
3724 ]) 3724 ])
3725 with self.subTest(parallel=True): 3725 with self.subTest(parallel=True):
3726 with mock.patch("main.parse_args") as parse_args,\ 3726 with mock.patch("main.parse_args") as parse_args,\
@@ -3749,9 +3749,9 @@ class MainTest(WebMockTestCase):
3749 self.assertEqual(2, process.call_count) 3749 self.assertEqual(2, process.call_count)
3750 process.assert_has_calls([ 3750 process.assert_has_calls([
3751 mock.call.__bool__(), 3751 mock.call.__bool__(),
3752 mock.call(3, "config1", 1, "report_path", args_mock, "pg_config"), 3752 mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"),
3753 mock.call.__bool__(), 3753 mock.call.__bool__(),
3754 mock.call(1, "config2", 2, "report_path", args_mock, "pg_config"), 3754 mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"),
3755 ]) 3755 ])
3756 3756
3757 @mock.patch.object(main.sys, "exit") 3757 @mock.patch.object(main.sys, "exit")