Browse Source

chore(stock-market): run formatter

pull/2562/head
nprimo 5 months ago committed by Niccolò Primo
parent
commit
8316a86b43
  1. 9
      subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py
  2. 4
      subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py

9
subjects/mobile-dev/stock-market/resources/mock-stock-data-server/app.py

@ -2,7 +2,7 @@ from flask import Flask, jsonify
from flask_cors import CORS from flask_cors import CORS
import time import time
from utils import load_historical_data from utils import load_historical_data
app = Flask(__name__) app = Flask(__name__)
CORS(app) CORS(app)
@ -11,21 +11,24 @@ start_time = time.time()
historical_data = load_historical_data() historical_data = load_historical_data()
@app.route('/exchange_rate/<symbol>') @app.route('/exchange_rate/<symbol>')
def get_stock_data(symbol): def get_stock_data(symbol):
if symbol not in list(historical_data.keys()): if symbol not in list(historical_data.keys()):
return jsonify("Invalid symbol") return jsonify("Invalid symbol")
current_time = time.time() current_time = time.time()
step = (int(current_time * 10) - int(start_time * 10)) % len(historical_data[symbol]) step = (int(current_time * 10) - int(start_time * 10)
) % len(historical_data[symbol])
try: try:
return jsonify({ return jsonify({
'symbol': 'USD', 'symbol': 'USD',
'rate': float(historical_data[symbol][step]), 'rate': float(historical_data[symbol][step]),
'timestamp': current_time 'timestamp': current_time
}) })
except: except:
return "Server Error" return "Server Error"
@app.route('/stocks_list') @app.route('/stocks_list')
def list_symbols(): def list_symbols():
return jsonify(list(historical_data.keys())) return jsonify(list(historical_data.keys()))

4
subjects/mobile-dev/stock-market/resources/mock-stock-data-server/utils.py

@ -5,7 +5,8 @@ import csv
def load_historical_data(directory_path='./sample-stocks/'): def load_historical_data(directory_path='./sample-stocks/'):
historical_data = {} historical_data = {}
file_list = [filename for filename in os.listdir(directory_path) if filename.endswith(".csv")] file_list = [filename for filename in os.listdir(
directory_path) if filename.endswith(".csv")]
for filename in file_list: for filename in file_list:
symbol = filename.replace(".csv", "") symbol = filename.replace(".csv", "")
@ -21,4 +22,3 @@ def load_historical_data(directory_path='./sample-stocks/'):
if __name__ == "__main__": if __name__ == "__main__":
result = load_historical_data() result = load_historical_data()
print(f'keys: {result.keys()}') print(f'keys: {result.keys()}')

Loading…
Cancel
Save