]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Store printed logs
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 12 Mar 2018 01:54:13 +0000 (02:54 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 12 Mar 2018 01:54:13 +0000 (02:54 +0100)
market.py
store.py
test.py

index 8672c59699431846ac097173bf5cb9ab076b3910..2ddebfac3795ebc51167676255e16e4113a834a5 100644 (file)
--- a/market.py
+++ b/market.py
@@ -49,9 +49,11 @@ class Market:
         self.report.merge(Portfolio.report)
         try:
             if self.report_path is not None:
-                report_file = "{}/{}_{}.json".format(self.report_path, datetime.now().isoformat(), self.user_id)
-                with open(report_file, "w") as f:
+                report_file = "{}/{}_{}".format(self.report_path, datetime.now().isoformat(), self.user_id)
+                with open(report_file + ".json", "w") as f:
                     f.write(self.report.to_json())
+                with open(report_file + ".log", "w") as f:
+                    f.write("\n".join(map(lambda x: x[1], self.report.print_logs)))
         except Exception as e:
             print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
 
index f655be538b66f3d81d7fb121a0595ee4ff63ddc7..d875a983a77b5d0fa8cbce0f60c923ac5de62832 100644 (file)
--- a/store.py
+++ b/store.py
@@ -15,14 +15,20 @@ class ReportStore:
         self.market = market
         self.verbose_print = verbose_print
 
+        self.print_logs = []
         self.logs = []
 
     def merge(self, other_report):
         self.logs += other_report.logs
         self.logs.sort(key=lambda x: x["date"])
 
+        self.print_logs += other_report.print_logs
+        self.print_logs.sort(key=lambda x: x[0])
+
     def print_log(self, message):
-        message = str(message)
+        now = datetime.now()
+        message = "{:%Y-%m-%d %H:%M:%S}: {}".format(now, str(message))
+        self.print_logs.append([now, message])
         if self.verbose_print:
             print(message)
 
diff --git a/test.py b/test.py
index ac9a6cd55b3ccf81b14f926939b359e14586ca70..13bd332405526ae0e1af0a9b568bf38446506410 100644 (file)
--- a/test.py
+++ b/test.py
@@ -1395,12 +1395,15 @@ class MarketTest(WebMockTestCase):
                 mock.patch.object(market, "datetime") as time_mock:
 
             time_mock.now.return_value = datetime.datetime(2018, 2, 25)
+            report.print_logs = [[time_mock.now(), "Foo"], [time_mock.now(), "Bar"]]
             report.to_json.return_value = "json_content"
 
             m.store_report()
 
             file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
-            file_open().write.assert_called_once_with("json_content")
+            file_open.assert_any_call("present/2018-02-25T00:00:00_1.log", "w")
+            file_open().write.assert_any_call("json_content")
+            file_open().write.assert_any_call("Foo\nBar")
             m.report.to_json.assert_called_once_with()
             report.merge.assert_called_with(store.Portfolio.report)
 
@@ -3016,15 +3019,18 @@ class ReportStoreTest(WebMockTestCase):
 
         self.assertEqual(3, len(report_store1.logs))
         self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs)))
+        self.assertEqual(6, len(report_store1.print_logs))
 
     def test_print_log(self):
         report_store = market.ReportStore(self.m)
         with self.subTest(verbose=True),\
+                mock.patch.object(store, "datetime") as time_mock,\
                 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+            time_mock.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10)
             report_store.set_verbose(True)
             report_store.print_log("Coucou")
             report_store.print_log(portfolio.Amount("BTC", 1))
-            self.assertEqual(stdout_mock.getvalue(), "Coucou\n1.00000000 BTC\n")
+            self.assertEqual(stdout_mock.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n")
 
         with self.subTest(verbose=False),\
                 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: