Sfoglia il codice sorgente

improve table output

Eren Yilmaz 6 anni fa
parent
commit
223f5e4952
2 ha cambiato i file con 22 aggiunte e 17 eliminazioni
  1. 21 15
      client_controller.py
  2. 1 2
      model.py

+ 21 - 15
client_controller.py

@@ -125,12 +125,18 @@ def help():
     print('  All arguments for all commands are optional!')
     print('  All arguments for all commands are optional!')
 
 
 
 
+def _my_tabulate(data, **params):
+    if data == [] and 'headers' in params:
+        data = [(None for _ in params['headers'])]
+    return tabulate(data, **params)
+
+
 def depot():
 def depot():
     fake_loading_bar('Loading data', duration=1.3)
     fake_loading_bar('Loading data', duration=1.3)
     response = client_request('depot', {"session_id": connection.session_id})
     response = client_request('depot', {"session_id": connection.session_id})
     success = 'data' in response and 'own_wealth' in response
     success = 'data' in response and 'own_wealth' in response
     if success:
     if success:
-        print(tabulate(response['data'], headers=['Object', 'Amount', 'Est. Value'], tablefmt="pipe"))
+        print(_my_tabulate(response['data'], headers=['Object', 'Amount', 'Est. Value'], tablefmt="pipe"))
         print('This corresponds to a wealth of roughly', response['own_wealth'])
         print('This corresponds to a wealth of roughly', response['own_wealth'])
     else:
     else:
         if 'error_message' in response:
         if 'error_message' in response:
@@ -144,7 +150,7 @@ def leaderboard():
     response = client_request('leaderboard', {"session_id": connection.session_id})
     response = client_request('leaderboard', {"session_id": connection.session_id})
     success = 'data' in response
     success = 'data' in response
     if success:
     if success:
-        print(tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
+        print(_my_tabulate(response['data'], headers=['User', 'Wealth'], tablefmt="pipe"))
     else:
     else:
         if 'error_message' in response:
         if 'error_message' in response:
             print('Leaderboard access failed with message:', response['error_message'])
             print('Leaderboard access failed with message:', response['error_message'])
@@ -248,9 +254,9 @@ def orders():
     response = client_request('orders', {"session_id": connection.session_id})
     response = client_request('orders', {"session_id": connection.session_id})
     success = 'data' in response
     success = 'data' in response
     if success:
     if success:
-        print(tabulate(response['data'],
-                       headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
-                       tablefmt="pipe"))
+        print(_my_tabulate(response['data'],
+                           headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Orig. Size', 'Expires', 'No.'],
+                           tablefmt="pipe"))
     else:
     else:
         if 'error_message' in response:
         if 'error_message' in response:
             print('Order access failed with message:', response['error_message'])
             print('Order access failed with message:', response['error_message'])
@@ -265,9 +271,9 @@ def orders_on(object_name=None):
     response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
     response = client_request('orders_on', {"session_id": connection.session_id, "ownable": object_name})
     success = 'data' in response
     success = 'data' in response
     if success:
     if success:
-        print(tabulate(response['data'],
-                       headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
-                       tablefmt="pipe"))
+        print(_my_tabulate(response['data'],
+                           headers=['Buy?', 'Name', 'Amount', 'Limit', 'Stop Loss?', 'Expires', 'No.'],
+                           tablefmt="pipe"))
     else:
     else:
         if 'error_message' in response:
         if 'error_message' in response:
             print('Order access failed with message:', response['error_message'])
             print('Order access failed with message:', response['error_message'])
@@ -287,7 +293,7 @@ def gift(username=None, amount=None, object_name=None):
                               {"session_id": connection.session_id,
                               {"session_id": connection.session_id,
                                "username": username,
                                "username": username,
                                "object_name": object_name,
                                "object_name": object_name,
-                               "amount":amount})
+                               "amount": amount})
     if 'error_message' in response:
     if 'error_message' in response:
         print('Order access failed with message:', response['error_message'])
         print('Order access failed with message:', response['error_message'])
     elif 'message' in response:
     elif 'message' in response:
@@ -299,9 +305,9 @@ def news():
     response = client_request('news', {"session_id": connection.session_id})
     response = client_request('news', {"session_id": connection.session_id})
     success = 'data' in response
     success = 'data' in response
     if success:
     if success:
-        print(tabulate(response['data'],
-                       headers=['Date', 'Title'],
-                       tablefmt="pipe"))
+        print(_my_tabulate(response['data'],
+                           headers=['Date', 'Title'],
+                           tablefmt="pipe"))
     else:
     else:
         if 'error_message' in response:
         if 'error_message' in response:
             print('Order access failed with message:', response['error_message'])
             print('Order access failed with message:', response['error_message'])
@@ -316,9 +322,9 @@ def transactions(object_name=None):
     response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
     response = client_request('transactions', {"session_id": connection.session_id, "ownable": object_name})
     success = 'data' in response
     success = 'data' in response
     if success:
     if success:
-        print(tabulate(response['data'],
-                       headers=['Time', 'Volume', 'Price'],
-                       tablefmt="pipe"))
+        print(_my_tabulate(response['data'],
+                           headers=['Time', 'Volume', 'Price'],
+                           tablefmt="pipe"))
     else:
     else:
         if 'error_message' in response:
         if 'error_message' in response:
             print('Transactions access failed with message:', response['error_message'])
             print('Transactions access failed with message:', response['error_message'])

+ 1 - 2
model.py

@@ -14,8 +14,7 @@ from debug import debug
 # connection: db.Connection = None
 # connection: db.Connection = None
 # cursor: db.Cursor = None
 # cursor: db.Cursor = None
 connection = None  # no type annotations in python 3.5
 connection = None  # no type annotations in python 3.5
-cursor = None
-
+cursor = None  # no type annotations in python 3.5
 db_name = None
 db_name = None