-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from ilcardella/refactor-model
Refactor model
- Loading branch information
Showing
18 changed files
with
1,975 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import os | ||
import sys | ||
import inspect | ||
import logging | ||
|
||
currentdir = os.path.dirname(os.path.abspath( | ||
inspect.getfile(inspect.currentframe()))) | ||
parentdir = os.path.dirname(currentdir) | ||
sys.path.insert(0, parentdir) | ||
|
||
from Utils.Utils import Utils | ||
from Utils.Trade import Trade | ||
|
||
class DatabaseHandler(): | ||
""" | ||
Handles the IO operation with the database to handle persistent data | ||
""" | ||
def __init__(self, config): | ||
""" | ||
Initialise | ||
""" | ||
# By default use the configured filepath | ||
self.db_filepath = config.get_trading_database_path() | ||
# Create an empty list to store trades from database | ||
self.trading_history = [] | ||
|
||
def read_data(self, filepath=None): | ||
""" | ||
Read the trade history from the json database and return the list of trades | ||
- **filepath**: optional, if not set the configured path will be used | ||
""" | ||
try: | ||
path = filepath if filepath is not None else self.db_filepath | ||
self.db_filepath = path | ||
json_obj = Utils.load_json_file(path) | ||
|
||
# Create a list of all the trades in the json file | ||
for item in json_obj['trades']: | ||
trade = Trade.from_dict(item) | ||
# Store the list internally | ||
self.trading_history.append(trade) | ||
except Exception as e: | ||
logging.error(e) | ||
raise RuntimeError('Unable to read data from the database') | ||
|
||
|
||
def write_data(self, filepath=None): | ||
""" | ||
Write the trade history to the database | ||
""" | ||
try: | ||
path = filepath if filepath is not None else self.db_filepath | ||
# Create a json object and store the trade history into it | ||
json_obj = { | ||
'trades': [] | ||
} | ||
for t in self.trading_history: | ||
json_obj['trades'].append(t.to_dict()) | ||
# Write to file | ||
return Utils.write_json_file(path, json_obj) | ||
except Exception as e: | ||
logging.error(e) | ||
raise RuntimeError('Unable to write data to the database') | ||
|
||
def get_db_filepath(self): | ||
""" | ||
Return the database filepath | ||
""" | ||
return self.db_filepath | ||
|
||
def get_trades_list(self): | ||
""" | ||
Return the list of trades stored in the db | ||
""" | ||
return self.trading_history | ||
|
||
def add_trade(self, trade): | ||
""" | ||
Add a trade to the database | ||
""" | ||
try: | ||
self.trading_history.append(trade) | ||
except Exception as e: | ||
logging.error(e) | ||
raise RuntimeError('Unable to add trade to the database') | ||
|
||
def remove_last_trade(self): | ||
""" | ||
Remove the last trade from the trade history | ||
""" | ||
try: | ||
del self.trading_history[-1] | ||
except Exception as e: | ||
logging.error(e) | ||
raise RuntimeError('Unable to delete last trade') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.