-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
209 additions
and
18 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
Binary file not shown.
Binary file not shown.
Empty file.
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,55 @@ | ||
import sys | ||
|
||
from jdatetime import datetime as jdatetime | ||
|
||
from tsetmc_api.data_file import SymbolDataFile | ||
from tsetmc_api.types import SymbolId | ||
|
||
|
||
def generate(symbol_id: SymbolId, start_time: jdatetime, end_time: jdatetime): | ||
file_name = f'{symbol_id}_{start_time.strftime("%Y%m%d")}_{end_time.strftime("%Y%m%d")}.zip' | ||
print(f'Generating Data File {file_name} ...') | ||
SymbolDataFile.generate_data_file(symbol_id=symbol_id, | ||
start_time=start_time, | ||
end_time=end_time, | ||
file_location=file_name) | ||
|
||
|
||
def install(file_location: str): | ||
SymbolDataFile.install_data_file(file_location) | ||
|
||
|
||
def main(): | ||
if len(sys.argv) < 2: | ||
print('not enough arguments') | ||
sys.exit(1) | ||
|
||
action = sys.argv[1] | ||
if action == 'generate': | ||
if len(sys.argv) <= 2: | ||
print('symbol id was not provided') | ||
sys.exit(1) | ||
|
||
symbol_id = sys.argv[2] | ||
|
||
if len(sys.argv) > 3: | ||
start_time = jdatetime.strptime(sys.argv[3], '%Y-%m-%d') | ||
else: | ||
start_time = jdatetime(1396, 1, 1) | ||
|
||
if len(sys.argv) > 4: | ||
end_time = jdatetime.strptime(sys.argv[4], '%Y-%m-%d') | ||
else: | ||
end_time = jdatetime.now() | ||
|
||
generate(symbol_id=symbol_id, start_time=start_time, end_time=end_time) | ||
elif action == 'install': | ||
if len(sys.argv) <= 2: | ||
print('file location was not provided') | ||
sys.exit(1) | ||
|
||
file_location = sys.argv[2] | ||
install(file_location=file_location) | ||
else: | ||
print('invalid action') | ||
sys.exit(1) |
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 @@ | ||
from .symbol_data_file import SymbolDataFile |
Binary file not shown.
Binary file not shown.
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,82 @@ | ||
from __future__ import annotations | ||
|
||
import pickle | ||
import zipfile | ||
from dataclasses import dataclass | ||
|
||
from jdatetime import datetime as jdatetime | ||
|
||
from tsetmc_api.cache import PersistentCache | ||
from tsetmc_api.day_details.day_details import SymbolDayDetails | ||
from tsetmc_api.day_details.exceptions import NoDataError | ||
from tsetmc_api.symbol import Symbol | ||
from tsetmc_api.types import SymbolId | ||
from tsetmc_api.utils import jalali_daterange | ||
|
||
|
||
@dataclass | ||
class SymbolDataFileInformation: | ||
symbol_id: SymbolId | ||
start_time: jdatetime | ||
end_time: jdatetime | ||
|
||
|
||
class SymbolDataFile: | ||
@staticmethod | ||
def get_data_file_information(file_location: str) -> SymbolDataFileInformation: | ||
with zipfile.ZipFile(file_location, 'r') as zp: | ||
with zp.open('information.pickle', 'r') as infop: | ||
return pickle.load(infop) | ||
|
||
@staticmethod | ||
def install_data_file(file_location: str): | ||
file_information = SymbolDataFile.get_data_file_information(file_location=file_location) | ||
with zipfile.ZipFile(file_location, 'r') as zp: | ||
for jdate in jalali_daterange(start_time=file_information.start_time, end_time=file_information.end_time): | ||
print(f'Installing {jdate.year}/{jdate.month}/{jdate.day}') | ||
file_name = f'{jdate.year}-{jdate.month}-{jdate.day}.pickle' | ||
try: | ||
with zp.open(file_name, 'r') as pfp: | ||
day_details = pickle.load(pfp) | ||
if type(day_details) == SymbolDayDetails: | ||
day_details._save_to_cache() | ||
else: | ||
PersistentCache.store( | ||
'symbol_day_details', | ||
f'{file_information.symbol_id}-{jdate.year}-{jdate.month}-{jdate.day}', | ||
{ | ||
'no_data': True, | ||
} | ||
) | ||
except KeyError: | ||
pass | ||
except Exception as ex: | ||
print(type(ex)) | ||
print(ex) | ||
|
||
@staticmethod | ||
def generate_data_file(symbol_id: SymbolId, start_time: jdatetime, end_time: jdatetime, file_location: str): | ||
symbol = Symbol(symbol_id=symbol_id) | ||
with zipfile.ZipFile(file_location, 'w') as zp: | ||
for jdate in jalali_daterange(start_time=start_time, end_time=end_time): | ||
while True: | ||
print(f'Loading {jdate.year}/{jdate.month}/{jdate.day}') | ||
file_name = f'{jdate.year}-{jdate.month}-{jdate.day}.pickle' | ||
try: | ||
day_details = symbol.get_day_details(jyear=jdate.year, jmonth=jdate.month, jday=jdate.day) | ||
with zp.open(file_name, 'w') as pfp: | ||
pickle.dump(day_details, pfp) | ||
break | ||
except NoDataError as ex: | ||
with zp.open(file_name, 'w') as pfp: | ||
pickle.dump(None, pfp) | ||
break | ||
except Exception as ex: | ||
print(ex) | ||
|
||
with zp.open('information.pickle', 'w') as infop: | ||
pickle.dump(SymbolDataFileInformation( | ||
symbol_id=symbol_id, | ||
start_time=start_time, | ||
end_time=end_time, | ||
), infop) |
Binary file modified
BIN
+44 Bytes
(100%)
lib/tsetmc_api/day_details/__pycache__/core.cpython-38.pyc
Binary file not shown.
Binary file modified
BIN
+1.22 KB
(130%)
lib/tsetmc_api/day_details/__pycache__/day_details.cpython-38.pyc
Binary file not shown.
Binary file not shown.
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
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,2 @@ | ||
class NoDataError(Exception): | ||
pass |
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