Skip to content

Commit

Permalink
fix: add data directly
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashGordon committed Nov 17, 2023
1 parent ff9fd25 commit 6d9a1c1
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Run tests
run: |
source .venv/bin/activate
python3 tools/yaml2json.py
python3 tools/yaml2data.py
poetry build
poetry run pytest
- name: Publish Unit Test Results
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Run tests
run: |
source .venv/bin/activate
python3 tools/yaml2json.py
python3 tools/yaml2data.py
poetry build
poetry run pytest
- name: Publish Unit Test Results
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Run tests
run: |
source .venv/bin/activate
python3 tools/yaml2json.py
python3 tools/yaml2data.py
poetry build
poetry run pytest
- name: Publish Unit Test Results
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/symbolscanner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
source .venv/bin/activate
pip3 install pysymbolscanner
pysymbolscanner --input stocks.yaml --output stocks.yaml
python3 tools/yaml2json.py
python3 tools/yaml2data.py
export SKIP_TEST_VALID_COUNTRY_NAME='true'
export SKIP_TEST_UNIQUE_TICKER_SYMBOLS='true'
poetry build
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ version_toml = [
branch = "master"
upload_to_pypi = true
upload_to_release = true
build_command = "source .venv/bin/activate && python3 tools/yaml2json.py && poetry build"
build_command = "source .venv/bin/activate && python3 tools/yaml2data.py && poetry build"

[tool.pytest.ini_options]
addopts = "tests/ --junitxml test-results/test.xml --cov src/pytickersymbols --cov-report term-missing"
Expand Down
24 changes: 3 additions & 21 deletions src/pytickersymbols/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import yaml
from weakref import WeakValueDictionary
import itertools
from pytickersymbols.data import __data__

__version__ = "1.13.1"

Expand Down Expand Up @@ -92,27 +93,8 @@ def __call__(cls, *args, **kwargs):


class PyTickerSymbols(metaclass=Singleton):
def __init__(self, stocks_path=''):
self.__stocks = None
if not stocks_path:
json_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'stocks.json',
)
with open(json_path, errors='replace') as stocks:
self.__stocks = json.load(stocks)
else:
if stocks_path.lower().endswith(
'.yaml'
) or stocks_path.lower().endswith('.yml'):
self.load_yaml(stocks_path)
elif stocks_path.lower().endswith('.json'):
self.load_json(stocks_path)
else:
raise NotImplementedError(
f'File {stocks_path} is not supported.'
'File should be end with yaml, yml or json.'
)
def __init__(self):
self.__stocks = __data__

def load_json(self, path):
"""
Expand Down
1 change: 1 addition & 0 deletions src/pytickersymbols/data.py

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ def tearDown(self):
del p

def test_load_json(self):
with pytest.raises(NotImplementedError):
PyTickerSymbols(stocks_path='test.dat')
with tempfile.NamedTemporaryFile(suffix='.json') as temp:
temp.write(_test_json)
temp.flush()
stocks = PyTickerSymbols(stocks_path=temp.name)
stocks = PyTickerSymbols()
stocks.load_json(temp.name)
indices = stocks.get_all_indices()
self.assertEqual(len(indices), 1)
self.assertIn('test', indices)
Expand All @@ -89,7 +88,8 @@ def test_load_yaml(self):
)
)
temp.flush()
stocks = PyTickerSymbols(stocks_path=temp.name)
stocks = PyTickerSymbols()
stocks.load_yaml(temp.name)
indices = stocks.get_all_indices()
self.assertEqual(len(indices), 1)
self.assertIn('test', indices)
Expand Down
9 changes: 5 additions & 4 deletions tools/yaml2json.py → tools/yaml2data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import json
import os

# convert yaml file to json
# convert yaml and add to init file
input_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'..', 'stocks.yaml')
output_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'..', 'src', 'pytickersymbols', 'stocks.json')
'..', 'src', 'pytickersymbols', 'data.py')

os.makedirs(os.path.dirname(os.path.realpath(output_path)), exist_ok=True)

with open(output_path, 'w') as out_file:
with open(input_path, 'r') as in_file:
Expand All @@ -23,4 +22,6 @@
break
if stop:
break
json.dump(stock, out_file, ensure_ascii=False)

data = json.dumps(stock, ensure_ascii=False)
out_file.write(f'__data__ = {data.replace("null", "None")}\n')

0 comments on commit 6d9a1c1

Please sign in to comment.