浏览代码

Refactor into actions

Foppe Hemminga 6 年之前
父节点
当前提交
5b117f6007
共有 3 个文件被更改,包括 43 次插入28 次删除
  1. 32 17
      main.py
  2. 10 10
      stocks.py
  3. 1 1
      timestamp.txt

+ 32 - 17
main.py

@@ -9,29 +9,44 @@ import database
     This is the controller file
 """
 
+
+def process_drop(this_stock):
+    """
+    Processes call when the action is "DROP"
+    :param this_stock:
+    :return:
+    """
+    global db
+    global timestamp_previous
+    global timestamp_latest
+    stock_id = this_stock[1]
+    data_previous = model.get_data(stock_id, timestamp_previous, db)
+    data_latest = model.get_data(stock_id, timestamp_latest, db)
+    threshold = this_stock[3]
+    max_price = this_stock[2]
+    is_drop = model.process_data(data_previous, data_latest, max_price, threshold)
+    current_price = float(data_latest.current_price)
+    quantity = data_latest.available_shares - data_previous.available_shares
+    stock_name = this_stock[0]
+    message = view.create_message(
+        stock_name, timestamp_latest, current_price, quantity)
+    print(message)
+    if is_drop:
+        view.broadcast(message)
+
+
 if __name__ == '__main__':
     db = database.db
-    timestamp_latest = model.get_timestamp_latest(db)
-    timestamp_stored = model.get_timestamp_stored()
+    timestamp_latest = model.get_timestamp_latest(db)  # global
+    timestamp_stored = model.get_timestamp_stored()  # global
     if timestamp_latest == timestamp_stored:
         """Nothing to do"""
         sys.exit()
     # Update timestamp_stored
     model.put_timestamp_stored(timestamp_latest)
-    timestamp_previous = model.get_timestamp_previous(db)
+    timestamp_previous = model.get_timestamp_previous(db)  # global
     for stock in stocks.stocks:
-        stock_id = stock[1]
-        data_previous = model.get_data(stock_id, timestamp_previous, db)
-        data_latest = model.get_data(stock_id, timestamp_latest, db)
-        threshold = stock[3]
-        max_price = stock[2]
-        is_drop = model.process_data(data_previous, data_latest, max_price, threshold)
-        current_price = float(data_latest.current_price)
-        quantity = data_latest.available_shares - data_previous.available_shares
-        stock_name = stock[0]
-        message = view.create_message(
-            stock_name, timestamp_latest, current_price, quantity)
-        print(message)
-        if is_drop:
-            view.broadcast(message)
+        action = stock[-1]
+        if action == "DROP":
+            process_drop(stock)
     db.close()

+ 10 - 10
stocks.py

@@ -1,15 +1,15 @@
 # stock name, stock number, max price, min(price * drop) in B
 # So this means: stock name, stock # 15 (FHC), max price $335  and a > $10B drop
-fhg = ("FHG", 15, 335, 10)
-yaz = ("YAZ", 8, 60, 10)
-cnc = ("CNC", 10, 480, 10)
-wlt = ("WLT", 30, 550, 10)
-hrg = ("HRG", 22, 335, 10)
-sym = ("SYM", 16, 480, 10)
-slag = ("SLAG", 4, 190, 10)
-grn = ("GRN", 6, 230, 10)
-tcp = ("TCP", 13, 240, 10)
-tcb = ("TCB", 2, 420, 10)
+fhg = ("FHG", 15, 335, 10, "DROP")
+yaz = ("YAZ", 8, 60, 10, "DROP")
+cnc = ("CNC", 10, 480, 10, "DROP")
+wlt = ("WLT", 30, 550, 10, "DROP")
+hrg = ("HRG", 22, 335, 10, "DROP")
+sym = ("SYM", 16, 480, 10, "DROP")
+slag = ("SLAG", 4, 190, 10, "DROP")
+grn = ("GRN", 6, 230, 10, "DROP")
+tcp = ("TCP", 13, 240, 10, "DROP")
+tcb = ("TCB", 2, 420, 10, "DROP")
 
 # Put all stocks in this set
 stocks = (fhg, yaz, cnc, wlt, hrg, sym, slag, grn, tcp, tcb)

+ 1 - 1
timestamp.txt

@@ -1 +1 @@
-1559654400
+1560160200