diff --git a/BharatFinTrack/__init__.py b/BharatFinTrack/__init__.py index 4fe0e24..5b1c584 100644 --- a/BharatFinTrack/__init__.py +++ b/BharatFinTrack/__init__.py @@ -6,4 +6,4 @@ ] -__version__ = '0.0.2' \ No newline at end of file +__version__ = '0.0.2' diff --git a/BharatFinTrack/nse_track.py b/BharatFinTrack/nse_track.py index 367e358..a0ecf75 100644 --- a/BharatFinTrack/nse_track.py +++ b/BharatFinTrack/nse_track.py @@ -1,29 +1,32 @@ class NSETrack: - + ''' Represents characteristics of NSE finance products. ''' - - + @property - def indices_category(self): - + def indices_category( + self + ) -> list[str]: + ''' Returns a list categories for NSE indices. ''' - + output = [ - 'broad', - 'sectoral', - 'thematic', + 'broad', + 'sectoral', + 'thematic', 'strategy' ] - + return output - - - def get_indices_by_category(self, category): - + + def get_indices_by_category( + self, + category: str + ) -> list[str]: + ''' Returns NSE indices for a specified category. @@ -37,57 +40,60 @@ def get_indices_by_category(self, category): list A list containing the names of indices for the specified category. ''' - + indices = {} - + # broad index indices['broad'] = [ 'NIFTY 500', 'NIFTY 50' ] - + # sectoral index indices['sectoral'] = [ 'NIFTY IT', 'NIFTY BANK' ] - + # thematic index indices['thematic'] = [ 'NIFTY EV & NEW AGE AUTOMOTIVE', 'NIFTY INDIA DEFENCE' ] - + # strategy index indices['strategy'] = [ 'NIFTY ALPHA 50', 'NIFTY MIDCAP150 MOMENTUM 50' ] - + if category in self.indices_category: pass else: raise Exception(f'Invadid category: {category}') - + return indices[category] - - + @property - def downloadable_indices(self): - + def downloadable_indices( + self + ) -> list[str]: + ''' Returns a list of all indices names. ''' - + output = [ - index for category in self.indices_category for index in self.get_indices_by_category(category) + i for c in self.indices_category for i in self.get_indices_by_category(c) ] - + return output - - def is_downloadable_index(self, index): - + def is_downloadable_index( + self, + index: str + ) -> bool: + ''' Checks whether a specified NSE index name is downloadable. @@ -101,22 +107,23 @@ def is_downloadable_index(self, index): bool True if the index name is valid, False. ''' - + return index in self.downloadable_indices - - + @property - def indices_base_date(self): - + def indices_base_date( + self + ) -> dict[str, str]: + ''' - Returns a dictionary where keys are indices + Returns a dictionary where keys are indices and values are their corresponding base dates. ''' - + default_date = '01-Apr-2005' - + start_date = {} - + start_date['01-Jan-1995'] = ['NIFTY 500'] start_date['03-Nov-1995'] = ['NIFTY 50'] start_date['01-Jan-1996'] = ['NIFTY IT'] @@ -128,16 +135,19 @@ def indices_base_date(self): ] date_dict = {v: key for key, value in start_date.items() for v in value} - - output = dict( - map(lambda x: (x, date_dict.get(x, default_date)), self.downloadable_indices) - ) - + + output = { + index: date_dict.get(index, default_date) + for index in self.downloadable_indices + } + return output - - - def get_index_base_date(self, index): - + + def get_index_base_date( + self, + index: str + ) -> str: + ''' Returns the base date for a specified NSE index. @@ -151,17 +161,19 @@ def get_index_base_date(self, index): str The base date of the index in 'DD-MMM-YYYY' format. ''' - + if self.is_downloadable_index(index): pass else: raise Exception(f'Invalid index: {index}') - + return self.indices_base_date[index] - - - def get_index_base_value(self, index): - + + def get_index_base_value( + self, + index: str + ) -> float: + ''' Returns the base value for a specified NSE index. @@ -175,12 +187,12 @@ def get_index_base_value(self, index): float The base value of the index. ''' - + if self.is_downloadable_index(index): pass else: raise Exception(f'Invalid index: {index}') - + base_value = {'NIFTY IT': 100.0} - - return base_value.get(index, 1000.0) \ No newline at end of file + + return base_value.get(index, 1000.0) diff --git a/docs/conf.py b/docs/conf.py index 2f9dc32..4d3560f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,20 +1,11 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# import os import sys import sphinx_rtd_theme -sys.path.insert(0, os.path.abspath('..')) - +if os.path.abspath('..') in sys.path: + pass +else: + sys.path.append(os.path.abspath('..')) +from BharatFinTrack import __version__ # -- Project information ----------------------------------------------------- @@ -23,39 +14,34 @@ author = 'Debasish Pal' # The full version, including alpha/beta/rc tags -from BharatFinTrack import __version__ release = __version__ - # -- General configuration --------------------------------------------------- -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. extensions = [ - 'sphinx.ext.autodoc', + 'sphinx.ext.autodoc', 'sphinx.ext.napoleon', 'sphinx.ext.viewcode' - ] - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +] -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path. -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] +templates_path = [ + '_templates' +] +exclude_patterns = [ + '_build', + 'Thumbs.db', + '.DS_Store' +] # -- Options for HTML output ------------------------------------------------- -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# html_theme = 'sphinx_rtd_theme' -html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] -# Add any paths that contain custom static files (such as style sheets) here, -# relative to this directory. They are copied after the builtin static files, -# so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] \ No newline at end of file +html_theme_path = [ + sphinx_rtd_theme.get_html_theme_path() +] + +html_static_path = [ + '_static' +] diff --git a/pyproject.toml b/pyproject.toml index c0978d9..2a86064 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ authors = [ { name="Debasish Pal", email="bestdebasish@gmail.com" }, ] readme = "README.md" -requires-python = ">=3.12" +requires-python = ">=3.10" classifiers = [ "Development Status :: 1 - Planning", "Programming Language :: Python :: 3", @@ -21,10 +21,9 @@ classifiers = [ "Topic :: Scientific/Engineering :: Information Analysis" ] dynamic = ["version"] - keywords = [ "nse index", - "nse total return index", + "total return index(TRI)", "data download", "data analysis" ] @@ -36,4 +35,40 @@ keywords = [ [tool.setuptools.dynamic] -version = {attr = "BharatFinTrack.__version__"} \ No newline at end of file +version = {attr = "BharatFinTrack.__version__"} + + +[tool.pytest.ini_options] +addopts = "-rA -Werror --cov=BharatFinTrack" +testpaths = [ + "tests" +] + + +[tool.mypy] +files = [ + "BharatFinTrack", + "docs" +] +ignore_missing_imports = true +strict = true + + +[tool.tox] +legacy_tox_ini = """ + [tox] + envlist = py310, py311 + + [testenv] + deps = + pytest + pytest-cov + commands = + pytest --cov=BharatFinTrack + + [testenv:py310] + basepython = C:\\Users\\dpal22\\AppData\\Local\\anaconda3\\envs\\py310\\python.exe + + [testenv:py311] + basepython = C:\\Users\\dpal22\\AppData\\Local\\anaconda3\\envs\\py311\\python.exe +""" \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..0d3a3d7 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,6 @@ +[flake8] +exclude = + .ipynb_checkpoints + __pycache__ + .tox +ignore = E501 \ No newline at end of file diff --git a/tests/test_nse_track.py b/tests/test_nse_track.py index 5d9f817..b90b135 100644 --- a/tests/test_nse_track.py +++ b/tests/test_nse_track.py @@ -1,47 +1,55 @@ import pytest -import BharatFinTrack +from BharatFinTrack import NSETrack + @pytest.fixture(scope='module') def class_instance(): - - return BharatFinTrack.NSETrack() - - -def test_get_indices_by_category(class_instance): - + + yield NSETrack() + + +def test_get_indices_by_category( + class_instance +): + # pass test assert class_instance.get_indices_by_category('broad')[0] == 'NIFTY 500' assert 'NIFTY ALPHA 50' in class_instance.get_indices_by_category('strategy') - + # error test with pytest.raises(Exception) as exc_info: class_instance.get_indices_by_category('non-existence') assert exc_info.value.args[0] == 'Invadid category: non-existence' - - -def test_is_downloadable_index(class_instance): - assert class_instance.is_downloadable_index('NIFTY 50') == True - assert class_instance.is_downloadable_index('non-existence') == False - - -def test_get_index_base_date(class_instance): + +def test_is_downloadable_index( + class_instance +): + + assert class_instance.is_downloadable_index('NIFTY 50') is True + assert class_instance.is_downloadable_index('non-existence') is False + + +def test_get_index_base_date( + class_instance +): assert class_instance.get_index_base_date('NIFTY 50') == '03-Nov-1995' - + # error test with pytest.raises(Exception) as exc_info: class_instance.get_index_base_date('non-existence') assert exc_info.value.args[0] == 'Invalid index: non-existence' - - -def test_get_index_base_value(class_instance): + + +def test_get_index_base_value( + class_instance +): assert class_instance.get_index_base_value('NIFTY 50') == 1000.0 assert class_instance.get_index_base_value('NIFTY IT') == 100.0 - + # error test with pytest.raises(Exception) as exc_info: class_instance.get_index_base_value('non-existence') assert exc_info.value.args[0] == 'Invalid index: non-existence' - \ No newline at end of file