Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
philsv committed Aug 25, 2024
1 parent 47b3dec commit 58f96b7
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 32 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-yaml

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pycot

[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.1.1&x2=0)](https://badge.fury.io/py/pycot-reports)
[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.1.2&x2=0)](https://badge.fury.io/py/pycot-reports)
[![License: MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://github.com/philsv/pycot/blob/main/LICENSE)
[![Weekly Downloads](https://static.pepy.tech/personalized-badge/pycot-reports?period=week&units=international_system&left_color=grey&right_color=blue&left_text=downloads/week)](https://pepy.tech/project/pycot-reports)
[![Monthly Downloads](https://static.pepy.tech/personalized-badge/pycot-reports?period=month&units=international_system&left_color=grey&right_color=blue&left_text=downloads/month)](https://pepy.tech/project/pycot-reports)
Expand Down
19 changes: 6 additions & 13 deletions pycot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@


def open_json(path: Path) -> dict:
"""
Opens a json file.
Returns:
The content as a dictionary.
"""
"""Opens a json file."""
with open(path, "r") as f:
data = json.load(f)
return data


class Settings(BaseSettings):
"""
Settings for the pycot package.
"""
"""Settings for the pycot package."""

BASE_PATH: Path = Path(__file__).parent.parent
FORMAT_COLUMNS_PATH: Path = BASE_PATH / "pycot" / "store" / "format_columns.json"
COT_REPORTS_DATA_PATH: Path = BASE_PATH / "pycot" / "store" / "cot_reports_data.json"
COT_REPORTS_DATA_PATH: Path = (
BASE_PATH / "pycot" / "store" / "cot_reports_data.json"
)
CONTRACT_NAMES_PATH: Path = BASE_PATH / "pycot" / "store" / "contract_names.json"

FORMAT_COLUMNS: dict = open_json(FORMAT_COLUMNS_PATH)
Expand All @@ -34,9 +29,7 @@ class Settings(BaseSettings):

@lru_cache()
def get_settings() -> Settings:
"""
Cache the settings object to avoid reading the environment variables
"""
"""Cache the settings object to avoid reading the environment variables."""
return Settings()


Expand Down
2 changes: 0 additions & 2 deletions pycot/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
class InvalidReportType(Exception):
"""Raised when the report type is not valid"""

pass
21 changes: 7 additions & 14 deletions pycot/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ def format_dataframe(
names: Union[list, tuple],
columns: Union[list, tuple],
) -> pd.DataFrame:
"""
Helper function to format the data retrieved from the CFTC website.
"""
df = data.reindex(
columns=columns
) # Limit the columns to the ones we need (please adjust to your liking in format_columns.json)
"""Helper function to format the data retrieved from the CFTC website."""
# Limit the columns to the ones we need (can be adjusted in format_columns.json)
df = data.reindex(columns=columns)
df = df.rename(columns=dict(zip(columns, names)))
df["Date"] = pd.to_datetime(df["Date"])
df = df.set_index(["Date", "Contract Name"])
Expand All @@ -43,9 +40,7 @@ def get_contract(
df: pd.DataFrame,
contract_name: Union[str, tuple],
) -> pd.DataFrame:
"""
Retrieves the Commitment of Traders Reports data for a specific contract or list of contracts.
"""
"""Retrieves the Commitment of Traders Reports data for a specific contract or list of contracts."""
if isinstance(contract_name, str):
return df[df["Contract Name"] == contract_name]

Expand Down Expand Up @@ -97,9 +92,7 @@ def extract_text_file_to_dataframe(
response: requests.models.Response,
text_file: str,
) -> pd.DataFrame:
"""
Unzips the text file from an archive and returns a pandas DataFrame.
"""
"""Unzips the text file from an archive and returns a pandas DataFrame."""
with tempfile.TemporaryDirectory() as tmpdirname:
zip_path = os.path.join(tmpdirname, "archive.zip")

Expand Down Expand Up @@ -361,7 +354,7 @@ def financial_report(
df["Leveraged Money Short"] = df["Leveraged Money Short"] * -1
df = df.sort_index()[::-1]
return df

def list_available_contracts(
self,
) -> np.ndarray:
Expand Down Expand Up @@ -396,7 +389,7 @@ def list_available_contracts(
"traders_in_financial_futures_futopt",
]:
df = self.financial_report

available_contracts = df["Contract Name"].unique()
return np.sort(available_contracts)

Expand Down
2 changes: 1 addition & 1 deletion pycot/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.1"
__version__ = "0.1.2"
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
license="MIT",
author="philsv",
author_email="[email protected]",
description="Python Library for interacting with the CFTC Commitment of Traders reports.",
long_description=long_description,
long_description_content_type="text/markdown",
keywords=["commitment of traders", "cot data", "cftc", "python"],
Expand Down
3 changes: 3 additions & 0 deletions tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
],
)
def test_individual_reports(report_type, contract_name):
"""Test the individual reports."""
cot = CommitmentsOfTraders(report_type)
df = cot.report(contract_name)
assert isinstance(df, pd.DataFrame)
Expand All @@ -49,6 +50,7 @@ def test_individual_reports(report_type, contract_name):
],
)
def test_list_available_contracts(report_type):
"""Test the list_available_contracts function."""
cot = CommitmentsOfTraders(report_type)
contracts = cot.list_available_contracts()
assert isinstance(contracts, np.ndarray)
Expand All @@ -72,6 +74,7 @@ def test_list_available_contracts(report_type):
],
)
def test_cot_report(report_type, contract_1, contract_2):
"""Test the cot report function to retrieve data from the same report multiple times."""
start_time_1 = time.perf_counter()
cot = CommitmentsOfTraders(report_type)
df_1 = cot.report(contract_1)
Expand Down

0 comments on commit 58f96b7

Please sign in to comment.