Skip to content

Commit

Permalink
Restructure: Add linting and testing
Browse files Browse the repository at this point in the history
Added black, isort, and lint to developer workflow. This in turn forced touches in almost all the code
Added testing to immediately verify at least some parts of the code were not broken, but much work is still needed on this front.
  • Loading branch information
nico-corthorn committed Nov 6, 2024
1 parent 591cb3c commit 8c12af0
Show file tree
Hide file tree
Showing 39 changed files with 1,340 additions and 1,943 deletions.
49 changes: 49 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# File: .pylintrc
[MASTER]
# Python files to analyze
ignore=CVS
persistent=yes

# Use multiple processes to speed up Pylint (use "0" for auto)
jobs=0

[MESSAGES CONTROL]
# Disable specific warnings
disable=
C0111, # missing-docstring
C0103, # invalid-name
C0303, # trailing-whitespace
W0621, # redefined-outer-name
W0703, # broad-except
R0903, # too-few-public-methods
R0913, # too-many-arguments
R0914, # too-many-locals
W0511 # fixme

[FORMAT]
# Maximum number of characters on a single line
max-line-length=100

# Expected format of line ending
expected-line-ending-format=LF

[BASIC]
# Good variable names
good-names=i,j,k,ex,Run,_,fd,fp

[MISCELLANEOUS]
# List of note tags to take into consideration
notes=FIXME,TODO,XXX,HACK

[SIMILARITIES]
# Minimum lines number of a similarity
min-similarity-lines=20

# Ignore comments when computing similarities
ignore-comments=yes

# Ignore docstrings when computing similarities
ignore-docstrings=yes

# Ignore imports when computing similarities
ignore-imports=yes
6 changes: 6 additions & 0 deletions .samignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*/__pycache__
**/*.pyc
**/.pytest_cache
**/.ipynb_checkpoints
tests/
esgtools/db/notebooks
18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
install:
pip install --upgrade pip &&\
python -m pip install --upgrade pip &&\
pip install -r esgtools/requirements.txt &&\
pip install -r tests/test_requirements.txt
pip install -r tests/test_requirements.txt &&\
pip install -r requirements-dev.txt

test:
python -m pytest -v

format:
black *.py
black esgtools tests
isort esgtools tests

lint:
pylint --disable=R,C hello.py
pylint esgtools tests --rcfile=.pylintrc
black --check esgtools tests
isort --check-only esgtools tests

all: install lint test
deploy:
sam build &&\
sam deploy --config-file samconfig.toml

all: install format lint test
3 changes: 1 addition & 2 deletions esgtools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

__version__ = "0.0.1"
__version__ = "0.0.1"
20 changes: 7 additions & 13 deletions esgtools/alpha/api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@

import os
import requests
import time
import pandas as pd
from datetime import datetime

import requests

URL_BASE = 'https://www.alphavantage.co/query?function='

URL_BASE = "https://www.alphavantage.co/query?function="

class AlphaScraper():

class AlphaScraper:
def __init__(self, api_key=None, wait=True, max_api_requests_per_min=75):
if api_key is None:
self.api_key = os.environ.get('ALPHAVANTAGE_API_KEY')
self.api_key = os.environ.get("ALPHAVANTAGE_API_KEY")
else:
self.api_key = api_key
self.wait = wait
Expand All @@ -22,30 +19,27 @@ def __init__(self, api_key=None, wait=True, max_api_requests_per_min=75):
self.last_increase_time = datetime.today()

def wait_to_hit_api(self):

# Remaining api requests
if self.remaining_api_requests > 0:
self.remaining_api_requests -= 1
print(f"Remaining api requests: {self.remaining_api_requests}")
return

# Reached maximum
delay = (datetime.today() - self.last_increase_time).total_seconds()
if delay < 60:
print(f"Maximum limit reached. Sleeping for {60 - delay} seconds")
time.sleep(60 - delay)

print("API limit refreshed after 1 minute")
self.remaining_api_requests = self.max_api_requests_per_min - 1
self.last_increase_time = datetime.today()


def hit_api(self, url, **kwargs):

kwargs["URL_BASE"] = URL_BASE
kwargs["api_key"] = self.api_key
url = url.format(**kwargs)

if self.wait:
self.wait_to_hit_api()

Expand Down
Loading

0 comments on commit 8c12af0

Please sign in to comment.