Skip to content

Commit

Permalink
Fix codestyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Aug 27, 2022
1 parent f7a09b2 commit 64aa9ad
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 60 deletions.
68 changes: 46 additions & 22 deletions pytse_client/financial_index/financial_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def __init__(
):
self._index: str = index or symbols_data.get_financial_index(symbol)
self.symbol: str = symbol if index is None else self._index
self._intraday_url: str = (
tse_settings.TSE_FINANCIAL_INDEX_EXPORT_INTRADAY_DATA_ADDRESS.format(
self._intraday_url = (
tse_settings.FINANCIAL_INDEX_EXPORT_INTRADAY_URL.format(
self._index
)
)
Expand All @@ -30,37 +30,43 @@ def __init__(
def last_update(self):
# زمان انتشار
soup = self._financial_index_page_soup
before_update_time_td: BeautifulSoup = soup.find_all("td", text="زمان انتشار")[
0
]
update_time_td: BeautifulSoup = before_update_time_td.find_next_siblings("td")[
0
]
before_update_time_td: BeautifulSoup = soup.find_all(
"td", text="زمان انتشار"
)[0]
update_time_td: BeautifulSoup = (
before_update_time_td.find_next_siblings("td")[0]
)
return pd.to_datetime(update_time_td.get_text()).strftime("%H:%M")

@property
def last_value(self):
# آخرین مقدار شاخص
soup = self._financial_index_page_soup
before_lastval_td: BeautifulSoup = soup.find_all("td", text="آخرین مقدار شاخص")[
before_lastval_td: BeautifulSoup = soup.find_all(
"td", text="آخرین مقدار شاخص"
)[0]
lastval_td: BeautifulSoup = before_lastval_td.find_next_siblings("td")[
0
]
lastval_td: BeautifulSoup = before_lastval_td.find_next_siblings("td")[0]
return float(lastval_td.get_text().replace(",", ""))

@property
def high(self):
# بیشترین مقدار روز
soup = self._financial_index_page_soup
before_high_td: BeautifulSoup = soup.find_all("td", text="بیشترین مقدار روز")[0]
before_high_td: BeautifulSoup = soup.find_all(
"td", text="بیشترین مقدار روز"
)[0]
high_td: BeautifulSoup = before_high_td.find_next_siblings("td")[0]
return high_td.get_text()

@property
def low(self):
# کمترین مقدار روز
soup = self._financial_index_page_soup
before_low_td: BeautifulSoup = soup.find_all("td", text="کمترین مقدار روز")[0]
before_low_td: BeautifulSoup = soup.find_all(
"td", text="کمترین مقدار روز"
)[0]
low_td: BeautifulSoup = before_low_td.find_next_siblings("td")[0]
return low_td.get_text()

Expand All @@ -80,11 +86,18 @@ def _get_contributing_symbols(self):
before_contr_symbols: BeautifulSoup = soup.find_all(
"div", text="شرکت های موجود در شاخص"
)[0]
contr_symbols: BeautifulSoup = before_contr_symbols.find_next_siblings("div")[0]
contr_symbols: BeautifulSoup = before_contr_symbols.find_next_siblings(
"div"
)[0]
_index_symbols = list(
map(lambda x: x.a["href"].split("i=")[1], contr_symbols.find_all("td")[::9])
map(
lambda x: x.a["href"].split("i=")[1],
contr_symbols.find_all("td")[::9],
)
)
_symbols = list(
map(lambda x: x.get_text(), contr_symbols.find_all("td")[::9])
)
_symbols = list(map(lambda x: x.get_text(), contr_symbols.find_all("td")[::9]))
_contr_symbols = list(zip(_index_symbols, _symbols))
return [
{"symbol": _contr_symbol[1], "index": _contr_symbol[0]}
Expand All @@ -103,9 +116,9 @@ def intraday_price(self) -> pd.DataFrame:
"div", text="سابقه شاخص روز جاری"
)[0]

intraday_price: BeautifulSoup = before_intraday_price.find_next_siblings("div")[
0
]
intraday_price: BeautifulSoup = (
before_intraday_price.find_next_siblings("div")[0]
)
intraday_price_ls = list(
map(lambda bs: bs.get_text(), intraday_price.find_all("td"))
)
Expand All @@ -114,7 +127,12 @@ def intraday_price(self) -> pd.DataFrame:
rows = self._get_rows(intraday_price_ls, len(columns))
df = pd.DataFrame(rows, columns=columns)
df = df.astype(
{"value": float, "change_percentage": float, "low": float, "high": float},
{
"value": float,
"change_percentage": float,
"low": float,
"high": float,
},
errors="raise",
)

Expand Down Expand Up @@ -144,9 +162,15 @@ def _get_rows(self, intraday_price_ls, col_len):
"""

# Remove separators in numbers: 3,000 -> 3000
intraday_price_ls = list(map(lambda x: x.replace(",", ""), intraday_price_ls))
intraday_price_ls = list(map(lambda x: x.replace("(", "-"), intraday_price_ls))
intraday_price_ls = list(map(lambda x: x.replace(")", ""), intraday_price_ls))
intraday_price_ls = list(
map(lambda x: x.replace(",", ""), intraday_price_ls)
)
intraday_price_ls = list(
map(lambda x: x.replace("(", "-"), intraday_price_ls)
)
intraday_price_ls = list(
map(lambda x: x.replace(")", ""), intraday_price_ls)
)
rows = [
intraday_price_ls[i : i + col_len]
for i in range(0, len(intraday_price_ls), col_len)
Expand Down
Loading

0 comments on commit 64aa9ad

Please sign in to comment.