Skip to content

Commit

Permalink
Improving class definition
Browse files Browse the repository at this point in the history
  • Loading branch information
lcsrodriguez committed Dec 24, 2023
1 parent dfad355 commit 837f6a3
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions ecocal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,26 @@


class Calendar:
__slots__ = (
"startHorizon",
"endHorizon",
"ecocal",
"details",
"detailed_ecocal",
"URL",
"hasCollectedTable",
"hasCollectedDetails",
"SOURCE_URL",
"withProgressBar",
"nbThreads"
)
__class__: str = "Calendar"
__slots__: dict = ("startHorizon", "endHorizon", "calendar", "details", "detailedCalendar", "URL",
"hasCollectedCalendar", "hasCollectedDetailedCalendar", "SOURCE_URL", "withProgressBar",
"nbThreads")

def __init__(self,
startHorizon: Union[datetime.datetime, str] = None,
endHorizon: Union[datetime.datetime, str] = None,
preBuildCalendar: bool = True,
withDetails: bool = False,
withProgressBar: bool = True,
nbThreads: int = 20) -> None:
nbThreads: int = DEFAULT_THREADS) -> None:

if isinstance(startHorizon, (datetime.datetime, datetime.date)):
startHorizon = startHorizon.strftime("%Y-%m-%d")
if isinstance(endHorizon, (datetime.datetime, datetime.date)):
endHorizon = endHorizon.strftime("%Y-%m-%d")

self.startHorizon: str = startHorizon if startHorizon is not None else "2023-10-08"
self.endHorizon: str = endHorizon if startHorizon is not None else "2023-10-10"
self.startHorizon: str = startHorizon if startHorizon is not None else "2020-01-01" # Default values
self.endHorizon: str = endHorizon if startHorizon is not None else "2023-12-31"

self.hasCollectedTable: bool = False
self.hasCollectedDetails: bool = False
Expand All @@ -41,6 +32,7 @@ def __init__(self,
self.detailed_ecocal: pd.DataFrame = None

self.nbThreads: int = nbThreads
assert self.nbThreads > 0

self.SOURCE_URL: str = API_SOURCE_URL
if preBuildCalendar:
Expand All @@ -50,7 +42,6 @@ def __init__(self,
raise Exception(f"An error occured.")
except Exception as e:
raise Exception(f"An error occured ({e})")

if withDetails:
self._mergeTableDetails()

Expand All @@ -59,11 +50,10 @@ def __str__(self):
f"(Collected ?: {self.hasCollectedTable}) " \
f"(Details ?: {self.hasCollectedDetails}) "

def __repr__(self):
return self.__str__()
def __repr__(self) -> None:
print(self.__str__())

def _buildCalendar(self) -> bool:

self.URL = f"{self.SOURCE_URL}/{self.startHorizon}T00:00:00Z/{self.endHorizon}T23:59:59Z" \
f"?&volatilities=NONE" \
f"&volatilities=LOW" \
Expand All @@ -90,13 +80,12 @@ def _buildCalendar(self) -> bool:
headers={
"Accept": "text/csv",
"Content-Type": "text/csv",
"Referer": "https://www.fxstreet.com/",
"Referer": BASE_URL,
"Connection": "keep-alive",
"User-Agent": "EcoCal script",
"User-Agent": DEFAULT_USER_AGENT,
})
end_clock = time.time()
dur_clock = end_clock - start_clock
print(f"Duration: {dur_clock}")
except Exception as e:
raise Exception(f"An error just occurred (Error: {e})")
if r.status_code != 200:
Expand Down Expand Up @@ -182,11 +171,11 @@ def _requestDetails(self, resource_id: str = "", output: dict = {}) -> Union[dic
headers={
"Accept": "application/json",
"Content-Type": "application/json",
"Referer": "https://www.fxstreet.com/",
"Referer": BASE_URL,
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36",
"User-Agent": DEFAULT_USER_AGENT,
})
if r.status_code == 200:
if r.status_code // 100 == 2:
output[resource_id] = r.json()
return output[resource_id]
return None
Expand Down

0 comments on commit 837f6a3

Please sign in to comment.