PYield 0.40.4__tar.gz → 0.40.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyield-0.40.4 → pyield-0.40.5}/PKG-INFO +1 -1
- pyield-0.40.5/pyield/__about__.py +1 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/anbima/difusao.py +5 -3
- {pyield-0.40.4 → pyield-0.40.5}/pyield/anbima/ima.py +1 -1
- {pyield-0.40.4 → pyield-0.40.5}/pyield/anbima/tpf.py +15 -30
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/di1.py +2 -1
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/futures/__init__.py +7 -13
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/rates.py +1 -1
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/trades_monthly.py +1 -1
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/vna.py +1 -1
- {pyield-0.40.4 → pyield-0.40.5}/pyield/ipca/projected.py +2 -2
- pyield-0.40.5/pyield/retry.py +62 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/auctions.py +1 -1
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/benchmark.py +3 -3
- pyield-0.40.4/pyield/__about__.py +0 -1
- pyield-0.40.4/pyield/retry.py +0 -98
- {pyield-0.40.4 → pyield-0.40.5}/.gitignore +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/LICENSE +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/README.md +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/anbima/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/anbima/ettj_intraday.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/anbima/ettj_last.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/anbima/imaq.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/di_over.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/futures/common.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/futures/historical.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/futures/intraday.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/b3/futures/xml.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/auction.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/ptax_api.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/repo.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bc/trades_intraday.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bday/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bday/core.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bday/holidays/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bday/holidays/br_holidays_new.txt +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bday/holidays/br_holidays_old.txt +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/bday/holidays/brholidays.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/config.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/conftest.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/converters.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/data_cache.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/fwd.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/interpolator.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/ipca/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/ipca/historical.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/py.typed +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/__init__.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/lft.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/ltn.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/ntnb.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/ntnc.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/ntnf.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/pre.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/tn/tools.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyield/types.py +0 -0
- {pyield-0.40.4 → pyield-0.40.5}/pyproject.toml +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.40.5"
|
|
@@ -118,10 +118,10 @@ def _fetch_url_data(data_referencia: str) -> str | None:
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
with requests.Session() as s:
|
|
121
|
-
s.get(URL_PAGINA_INICIAL, headers=headers)
|
|
121
|
+
s.get(URL_PAGINA_INICIAL, headers=headers, timeout=60)
|
|
122
122
|
try:
|
|
123
123
|
response_consulta = s.post(
|
|
124
|
-
URL_CONSULTA_DADOS, headers=headers, data=payload
|
|
124
|
+
URL_CONSULTA_DADOS, headers=headers, data=payload, timeout=60
|
|
125
125
|
)
|
|
126
126
|
response_consulta.raise_for_status()
|
|
127
127
|
except requests.exceptions.RequestException as e:
|
|
@@ -129,7 +129,9 @@ def _fetch_url_data(data_referencia: str) -> str | None:
|
|
|
129
129
|
return None # 3. Retornar None em caso de falha
|
|
130
130
|
|
|
131
131
|
try:
|
|
132
|
-
response_download = s.post(
|
|
132
|
+
response_download = s.post(
|
|
133
|
+
URL_DOWNLOAD, headers=headers, data=payload, timeout=60
|
|
134
|
+
)
|
|
133
135
|
response_download.raise_for_status()
|
|
134
136
|
if "text/html" in response_download.headers.get("Content-Type", ""):
|
|
135
137
|
logger.error(
|
|
@@ -76,7 +76,7 @@ LAST_IMA_URL = "https://www.anbima.com.br/informacoes/ima/arqs/ima_completo.txt"
|
|
|
76
76
|
|
|
77
77
|
@default_retry
|
|
78
78
|
def _fetch_last_ima_text() -> str:
|
|
79
|
-
r = requests.get(LAST_IMA_URL)
|
|
79
|
+
r = requests.get(LAST_IMA_URL, timeout=3)
|
|
80
80
|
r.raise_for_status()
|
|
81
81
|
r.encoding = "latin1"
|
|
82
82
|
text = r.text.split("2@COMPOSIÇÃO DE CARTEIRA")[1].strip()
|
|
@@ -14,16 +14,16 @@ import io
|
|
|
14
14
|
import logging
|
|
15
15
|
import socket
|
|
16
16
|
from typing import Literal
|
|
17
|
-
from urllib.error import HTTPError, URLError
|
|
18
|
-
from zoneinfo import ZoneInfo
|
|
19
17
|
|
|
20
18
|
import polars as pl
|
|
21
19
|
import polars.selectors as ps
|
|
22
20
|
import requests
|
|
21
|
+
from requests.exceptions import HTTPError, RequestException
|
|
23
22
|
|
|
24
23
|
from pyield import bday
|
|
25
24
|
from pyield.b3 import di1
|
|
26
25
|
from pyield.bc.ptax_api import ptax
|
|
26
|
+
from pyield.config import TIMEZONE_BZ
|
|
27
27
|
from pyield.converters import convert_dates
|
|
28
28
|
from pyield.data_cache import get_cached_dataset
|
|
29
29
|
from pyield.retry import default_retry
|
|
@@ -32,8 +32,6 @@ from pyield.tn.ntnc import duration as duration_c
|
|
|
32
32
|
from pyield.tn.ntnf import duration as duration_f
|
|
33
33
|
from pyield.types import DateScalar, has_null_args
|
|
34
34
|
|
|
35
|
-
BZ_TIMEZONE = ZoneInfo("America/Sao_Paulo")
|
|
36
|
-
|
|
37
35
|
BOND_TYPES = Literal["LFT", "NTN-B", "NTN-C", "LTN", "NTN-F", "PRE"]
|
|
38
36
|
|
|
39
37
|
ANBIMA_URL = "https://www.anbima.com.br/informacoes/merc-sec/arqs"
|
|
@@ -67,7 +65,7 @@ logger = logging.getLogger(__name__)
|
|
|
67
65
|
|
|
68
66
|
def _validate_not_future_date(date: dt.date):
|
|
69
67
|
"""Raises ValueError if the date is in the future."""
|
|
70
|
-
today = dt.datetime.now(
|
|
68
|
+
today = dt.datetime.now(TIMEZONE_BZ).date()
|
|
71
69
|
if date > today:
|
|
72
70
|
date_log = date.strftime("%d/%m/%Y")
|
|
73
71
|
msg = f"Cannot process data for a future date ({date_log})."
|
|
@@ -109,19 +107,13 @@ def _build_file_url(date: dt.date) -> str:
|
|
|
109
107
|
@default_retry
|
|
110
108
|
def _get_csv_data(date: dt.date) -> str:
|
|
111
109
|
file_url = _build_file_url(date)
|
|
112
|
-
r = requests.get(file_url)
|
|
110
|
+
r = requests.get(file_url, timeout=10)
|
|
113
111
|
r.raise_for_status()
|
|
114
112
|
r.encoding = "latin1"
|
|
115
113
|
text = r.text
|
|
116
114
|
return text
|
|
117
115
|
|
|
118
116
|
|
|
119
|
-
def _rename_columns(csv_text: str) -> str:
|
|
120
|
-
for old_name, new_name in COLUMN_NAME_MAPPING.items():
|
|
121
|
-
csv_text = csv_text.replace(old_name, new_name)
|
|
122
|
-
return csv_text
|
|
123
|
-
|
|
124
|
-
|
|
125
117
|
def _read_csv_data(csv_text: str) -> pl.DataFrame:
|
|
126
118
|
df = pl.read_csv(
|
|
127
119
|
source=io.StringIO(csv_text),
|
|
@@ -141,10 +133,8 @@ def _process_raw_df(df: pl.DataFrame) -> pl.DataFrame:
|
|
|
141
133
|
(ps.contains("Rate") / 100).round(6),
|
|
142
134
|
(ps.ends_with("Date")).cast(pl.String).str.strptime(pl.Date, "%Y%m%d"),
|
|
143
135
|
)
|
|
144
|
-
bd_to_mat_pd = bday.count(
|
|
145
|
-
|
|
146
|
-
)
|
|
147
|
-
df = df.with_columns(BDToMat=pl.Series(bd_to_mat_pd))
|
|
136
|
+
bd_to_mat_pd = bday.count(start=df["ReferenceDate"], end=df["MaturityDate"])
|
|
137
|
+
df = df.with_columns(BDToMat=bd_to_mat_pd)
|
|
148
138
|
return df
|
|
149
139
|
|
|
150
140
|
|
|
@@ -192,19 +182,14 @@ def _add_duration(df_input: pl.DataFrame) -> pl.DataFrame:
|
|
|
192
182
|
return df
|
|
193
183
|
|
|
194
184
|
|
|
195
|
-
def _add_dv01(df_input: pl.DataFrame) -> pl.DataFrame:
|
|
185
|
+
def _add_dv01(df_input: pl.DataFrame, ref_date: dt.date) -> pl.DataFrame:
|
|
196
186
|
"""Add the DV01 columns to the DataFrame."""
|
|
197
187
|
mduration_expr = pl.col("Duration") / (1 + pl.col("IndicativeRate"))
|
|
198
|
-
df = df_input.with_columns(
|
|
199
|
-
(0.0001 * mduration_expr * pl.col("Price")).alias("DV01")
|
|
200
|
-
)
|
|
188
|
+
df = df_input.with_columns(DV01=0.0001 * mduration_expr * pl.col("Price"))
|
|
201
189
|
|
|
202
190
|
# DV01 in USD
|
|
203
191
|
try:
|
|
204
|
-
|
|
205
|
-
ptax_rate = ptax(date=reference_date)
|
|
206
|
-
if ptax_rate is None:
|
|
207
|
-
reference_date = bday.offset(reference_date, -1)
|
|
192
|
+
ptax_rate = ptax(date=ref_date)
|
|
208
193
|
df = df.with_columns(DV01USD=pl.col("DV01") / ptax_rate)
|
|
209
194
|
except Exception as e:
|
|
210
195
|
logger.error(f"Error adding USD DV01: {e}")
|
|
@@ -247,7 +232,7 @@ def _custom_sort_and_order(df: pl.DataFrame) -> pl.DataFrame:
|
|
|
247
232
|
"Criteria",
|
|
248
233
|
]
|
|
249
234
|
column_order = [col for col in column_order if col in df.columns]
|
|
250
|
-
return df.select(column_order).sort(
|
|
235
|
+
return df.select(column_order).sort("BondType", "MaturityDate")
|
|
251
236
|
|
|
252
237
|
|
|
253
238
|
def _fetch_tpf_data(date: dt.date) -> pl.DataFrame:
|
|
@@ -292,18 +277,18 @@ def _fetch_tpf_data(date: dt.date) -> pl.DataFrame:
|
|
|
292
277
|
"Returning empty DataFrame."
|
|
293
278
|
)
|
|
294
279
|
return pl.DataFrame()
|
|
295
|
-
|
|
280
|
+
|
|
296
281
|
df = _read_csv_data(csv_text)
|
|
297
282
|
df = _process_raw_df(df)
|
|
298
283
|
df = _add_duration(df)
|
|
299
|
-
df = _add_dv01(df)
|
|
284
|
+
df = _add_dv01(df, date)
|
|
300
285
|
df = _add_di_rate(df, date)
|
|
301
286
|
df = _custom_sort_and_order(df)
|
|
302
287
|
|
|
303
288
|
return df
|
|
304
289
|
|
|
305
290
|
except HTTPError as e:
|
|
306
|
-
if e.
|
|
291
|
+
if e.response.status_code == 404: # noqa
|
|
307
292
|
logger.info(
|
|
308
293
|
f"No Anbima TPF secondary market data for {date_str} (HTTP 404). "
|
|
309
294
|
"Returning empty DataFrame."
|
|
@@ -313,8 +298,8 @@ def _fetch_tpf_data(date: dt.date) -> pl.DataFrame:
|
|
|
313
298
|
raise
|
|
314
299
|
|
|
315
300
|
# Este bloco ainda é útil para outros URLErrors (ex: timeout genuíno na URL pública)
|
|
316
|
-
except
|
|
317
|
-
logger.exception(f"
|
|
301
|
+
except RequestException:
|
|
302
|
+
logger.exception(f"RequestException fetching TPF data for {date_str}")
|
|
318
303
|
raise
|
|
319
304
|
|
|
320
305
|
except Exception:
|
|
@@ -17,8 +17,9 @@ def _load_with_intraday(dates: list[dt.date]) -> pl.DataFrame:
|
|
|
17
17
|
df_cached = get_cached_dataset("di1").filter(pl.col("TradeDate").is_in(dates))
|
|
18
18
|
|
|
19
19
|
# 2. Identifica datas solicitadas que não estão no cache
|
|
20
|
+
requested_dates = set(dates)
|
|
20
21
|
cached_dates = set(df_cached["TradeDate"].unique().to_list())
|
|
21
|
-
missing_dates =
|
|
22
|
+
missing_dates = requested_dates - cached_dates
|
|
22
23
|
|
|
23
24
|
# 3. Para cada data faltante, tenta buscar dados via API
|
|
24
25
|
dfs_to_concat = [df_cached] if not df_cached.is_empty() else []
|
|
@@ -36,16 +36,10 @@ def _validate_reference_date(trade_date: dt.date) -> bool:
|
|
|
36
36
|
"""
|
|
37
37
|
today_bz = dt.datetime.now(TIMEZONE_BZ).date()
|
|
38
38
|
if trade_date > today_bz:
|
|
39
|
-
logger.warning(
|
|
40
|
-
"The provided date %s is in the future. Returning an empty DataFrame.",
|
|
41
|
-
trade_date,
|
|
42
|
-
)
|
|
39
|
+
logger.warning(f"The provided date {trade_date} is in the future.")
|
|
43
40
|
return False
|
|
44
41
|
if not bday.is_business_day(trade_date):
|
|
45
|
-
logger.warning(
|
|
46
|
-
"The provided date %s is not a business day. Returning an empty DataFrame.",
|
|
47
|
-
trade_date,
|
|
48
|
-
)
|
|
42
|
+
logger.warning(f"The provided date {trade_date} is not a business day.")
|
|
49
43
|
return False
|
|
50
44
|
|
|
51
45
|
# Não tem pregão na véspera de Natal e Ano Novo
|
|
@@ -55,16 +49,15 @@ def _validate_reference_date(trade_date: dt.date) -> bool:
|
|
|
55
49
|
}
|
|
56
50
|
if trade_date in special_closed_dates:
|
|
57
51
|
logger.warning(
|
|
58
|
-
"There is no trading session before Christmas and New Year's Eve:
|
|
59
|
-
|
|
60
|
-
trade_date,
|
|
52
|
+
"There is no trading session before Christmas and New Year's Eve: "
|
|
53
|
+
f"{trade_date}"
|
|
61
54
|
)
|
|
62
55
|
return False
|
|
63
56
|
|
|
64
57
|
return True
|
|
65
58
|
|
|
66
59
|
|
|
67
|
-
def
|
|
60
|
+
def _is_intraday_date(check_date: dt.date) -> bool:
|
|
68
61
|
"""Check if a date is a trading day."""
|
|
69
62
|
# Primeiro valida regra geral de dia futuro / não útil / datas especiais
|
|
70
63
|
if not _validate_reference_date(check_date):
|
|
@@ -167,11 +160,12 @@ def futures(
|
|
|
167
160
|
|
|
168
161
|
# Validação centralizada (evita chamadas desnecessárias às APIs B3)
|
|
169
162
|
if not _validate_reference_date(trade_date):
|
|
163
|
+
logger.warning(f"{trade_date} is not a valid date. Returning empty DataFrame.")
|
|
170
164
|
return pl.DataFrame()
|
|
171
165
|
|
|
172
166
|
selected_contract = str(contract_code).upper()
|
|
173
167
|
|
|
174
|
-
if
|
|
168
|
+
if _is_intraday_date(trade_date):
|
|
175
169
|
# É um dia de negociação intraday
|
|
176
170
|
time = dt.datetime.now(TIMEZONE_BZ).time()
|
|
177
171
|
if time < INTRADAY_START_TIME: # Mercado não está aberto ainda
|
|
@@ -55,7 +55,7 @@ class BCSerie(Enum):
|
|
|
55
55
|
@default_retry
|
|
56
56
|
def _do_api_call(api_url: str) -> list[dict[str, Any]]:
|
|
57
57
|
"""Executa uma chamada GET na API do BCB e retorna o JSON."""
|
|
58
|
-
response = requests.get(api_url, timeout=
|
|
58
|
+
response = requests.get(api_url, timeout=10)
|
|
59
59
|
response.raise_for_status()
|
|
60
60
|
return response.json() # type: ignore[return-value]
|
|
61
61
|
|
|
@@ -97,7 +97,7 @@ def _build_file_url(target_date: dt.date, extragroup: bool) -> str:
|
|
|
97
97
|
|
|
98
98
|
@default_retry
|
|
99
99
|
def _fetch_zip_from_url(file_url: str) -> bytes:
|
|
100
|
-
response = requests.get(file_url, timeout=
|
|
100
|
+
response = requests.get(file_url, timeout=10)
|
|
101
101
|
response.raise_for_status()
|
|
102
102
|
return response.content
|
|
103
103
|
|
|
@@ -17,7 +17,7 @@ def _get_text(date: DateScalar) -> str:
|
|
|
17
17
|
url_file = f"{date.strftime('%Y%m%d')}APC238"
|
|
18
18
|
url_vna = url_base + url_file
|
|
19
19
|
|
|
20
|
-
response = requests.get(url_vna)
|
|
20
|
+
response = requests.get(url_vna, timeout=10)
|
|
21
21
|
response.raise_for_status()
|
|
22
22
|
return response.text
|
|
23
23
|
|
|
@@ -17,8 +17,8 @@ def _get_page_text() -> bytes:
|
|
|
17
17
|
"""Faz a requisição HTTP para a página da ANBIMA e retorna o texto HTML."""
|
|
18
18
|
url = "https://www.anbima.com.br/informacoes/indicadores/"
|
|
19
19
|
try:
|
|
20
|
-
r = requests.get(url, timeout=10)
|
|
21
|
-
r.raise_for_status()
|
|
20
|
+
r = requests.get(url, timeout=10)
|
|
21
|
+
r.raise_for_status()
|
|
22
22
|
return r.content
|
|
23
23
|
except requests.exceptions.RequestException as e:
|
|
24
24
|
raise ConnectionError(f"Erro ao acessar a página da ANBIMA: {e}")
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
|
|
3
|
+
from requests.exceptions import ConnectionError, HTTPError, Timeout
|
|
4
|
+
from tenacity import (
|
|
5
|
+
RetryCallState,
|
|
6
|
+
retry,
|
|
7
|
+
stop_after_attempt,
|
|
8
|
+
wait_exponential,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _log_before_sleep(retry_state: RetryCallState):
|
|
15
|
+
"""Loga uma mensagem ANTES de o Tenacity entrar em espera entre tentativas."""
|
|
16
|
+
if not (outcome := retry_state.outcome) or not outcome.failed:
|
|
17
|
+
return
|
|
18
|
+
|
|
19
|
+
if not (exception := outcome.exception()):
|
|
20
|
+
return
|
|
21
|
+
|
|
22
|
+
if not (next_action := retry_state.next_action) or not hasattr(
|
|
23
|
+
next_action, "sleep"
|
|
24
|
+
):
|
|
25
|
+
return
|
|
26
|
+
|
|
27
|
+
sleep_duration = next_action.sleep
|
|
28
|
+
truncated_exc = str(exception).replace("\n", " ")[:150]
|
|
29
|
+
|
|
30
|
+
logger.warning(
|
|
31
|
+
f"Tentativa {retry_state.attempt_number} falhou com "
|
|
32
|
+
f"{type(exception).__name__}: {truncated_exc}... Tentando novamente em "
|
|
33
|
+
f"{sleep_duration:.2f} segundos..."
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def should_retry_exception(retry_state: RetryCallState) -> bool:
|
|
38
|
+
"""Determina se uma exceção capturada justifica uma nova tentativa."""
|
|
39
|
+
if not retry_state.outcome or not (exception := retry_state.outcome.exception()):
|
|
40
|
+
return False
|
|
41
|
+
|
|
42
|
+
# Erros de rede genéricos são sempre transitórios
|
|
43
|
+
if isinstance(exception, (Timeout, ConnectionError)):
|
|
44
|
+
return True
|
|
45
|
+
|
|
46
|
+
# HTTPError: apenas 429 e 5xx são transitórios
|
|
47
|
+
if isinstance(exception, HTTPError):
|
|
48
|
+
status_code = exception.response.status_code
|
|
49
|
+
if status_code == 429 or status_code >= 500: # noqa
|
|
50
|
+
return True
|
|
51
|
+
|
|
52
|
+
return False
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# Retry policy padrão otimizado para timeouts específicos
|
|
56
|
+
default_retry = retry(
|
|
57
|
+
retry=should_retry_exception,
|
|
58
|
+
wait=wait_exponential(multiplier=2, min=1, max=10), # ⚡ Otimizado
|
|
59
|
+
stop=stop_after_attempt(3),
|
|
60
|
+
before_sleep=_log_before_sleep,
|
|
61
|
+
reraise=True,
|
|
62
|
+
)
|
|
@@ -115,7 +115,7 @@ def _fetch_auction_data(auction_date: dt.date) -> list[dict]:
|
|
|
115
115
|
)
|
|
116
116
|
params = {"dataleilao": auction_date.strftime("%d/%m/%Y")}
|
|
117
117
|
|
|
118
|
-
response = requests.get(endpoint, params=params, timeout=
|
|
118
|
+
response = requests.get(endpoint, params=params, timeout=10)
|
|
119
119
|
response.raise_for_status()
|
|
120
120
|
data = response.json()
|
|
121
121
|
if "registros" not in data or not data["registros"]:
|
|
@@ -44,13 +44,13 @@ def _fetch_raw_benchmarks(include_history: bool) -> list[dict]:
|
|
|
44
44
|
api_endpoint = f"{API_BASE_URL}?{API_HISTORY_PARAM}={include_history_param_value}"
|
|
45
45
|
|
|
46
46
|
try:
|
|
47
|
-
response = session.get(api_endpoint)
|
|
47
|
+
response = session.get(api_endpoint, timeout=10)
|
|
48
48
|
response.raise_for_status()
|
|
49
49
|
except requests.exceptions.SSLError as e:
|
|
50
50
|
logger.warning(
|
|
51
51
|
f"SSL error encountered: {e}. Retrying without certificate verification."
|
|
52
52
|
)
|
|
53
|
-
response = session.get(api_endpoint, verify=False)
|
|
53
|
+
response = session.get(api_endpoint, verify=False, timeout=10)
|
|
54
54
|
response.raise_for_status()
|
|
55
55
|
except requests.exceptions.RequestException as e:
|
|
56
56
|
logger.error(f"Error fetching benchmarks from API: {e}")
|
|
@@ -75,7 +75,7 @@ def _process_api_data(raw_data: list[dict]) -> pl.DataFrame:
|
|
|
75
75
|
.drop_nulls()
|
|
76
76
|
.with_columns(pl.col("Benchmark", "BondType").str.strip_chars())
|
|
77
77
|
.cast(DATA_SCHEMA)
|
|
78
|
-
.sort(
|
|
78
|
+
.sort("StartDate", "BondType", "MaturityDate")
|
|
79
79
|
)
|
|
80
80
|
|
|
81
81
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.40.4"
|
pyield-0.40.4/pyield/retry.py
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Módulo de retry configurável para chamadas de API (usando requests) e
|
|
3
|
-
processamento de dados.
|
|
4
|
-
|
|
5
|
-
Este módulo fornece um decorador `tenacity.retry` pré-configurado (`default_retry`)
|
|
6
|
-
para lidar com erros transitórios.
|
|
7
|
-
"""
|
|
8
|
-
|
|
9
|
-
import logging
|
|
10
|
-
|
|
11
|
-
# Dependências de terceiros
|
|
12
|
-
# MUDANÇA CRUCIAL: Importar exceções da biblioteca 'requests'
|
|
13
|
-
from requests.exceptions import ConnectionError, HTTPError, Timeout
|
|
14
|
-
from tenacity import (
|
|
15
|
-
RetryCallState,
|
|
16
|
-
retry,
|
|
17
|
-
stop_after_attempt,
|
|
18
|
-
wait_exponential,
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
# --- Configuração do Logger ---
|
|
22
|
-
logger = logging.getLogger(__name__)
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def _log_before_sleep(retry_state: RetryCallState):
|
|
26
|
-
"""Loga uma mensagem ANTES de o Tenacity entrar em espera entre tentativas."""
|
|
27
|
-
# Esta função já é excelente, sem necessidade de grandes mudanças.
|
|
28
|
-
# Adicionei apenas "..." para indicar que a mensagem de erro foi truncada.
|
|
29
|
-
if not (outcome := retry_state.outcome) or not outcome.failed:
|
|
30
|
-
return
|
|
31
|
-
|
|
32
|
-
if not (exception := outcome.exception()):
|
|
33
|
-
return
|
|
34
|
-
|
|
35
|
-
if not (next_action := retry_state.next_action) or not hasattr(
|
|
36
|
-
next_action, "sleep"
|
|
37
|
-
):
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
sleep_duration = next_action.sleep
|
|
41
|
-
truncated_exc = str(exception).replace("\n", " ")[:150]
|
|
42
|
-
|
|
43
|
-
logger.warning(
|
|
44
|
-
f"Tentativa {retry_state.attempt_number} falhou com "
|
|
45
|
-
f"{type(exception).__name__}: {truncated_exc}... Tentando novamente em "
|
|
46
|
-
f"{sleep_duration:.2f} segundos..."
|
|
47
|
-
)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def should_retry_exception(retry_state: RetryCallState) -> bool:
|
|
51
|
-
"""Determina se uma exceção capturada justifica uma nova tentativa."""
|
|
52
|
-
if not retry_state.outcome or not (exception := retry_state.outcome.exception()):
|
|
53
|
-
return False
|
|
54
|
-
|
|
55
|
-
# SUGESTÃO DE REFINAMENTO: Tratar os casos simples primeiro (early return).
|
|
56
|
-
# Erros de rede genéricos são sempre transitórios e devem ser tentados novamente.
|
|
57
|
-
if isinstance(exception, (Timeout, ConnectionError)):
|
|
58
|
-
return True
|
|
59
|
-
|
|
60
|
-
# Agora, tratar o caso mais complexo do HTTPError.
|
|
61
|
-
if isinstance(exception, HTTPError):
|
|
62
|
-
status_code = exception.response.status_code
|
|
63
|
-
|
|
64
|
-
# CASOS TRANSITÓRIOS (TENTAR NOVAMENTE)
|
|
65
|
-
# Erro 429 (Too Many Requests) ou erros 5xx (Server Error)
|
|
66
|
-
if status_code == 429 or status_code >= 500: # noqa E501
|
|
67
|
-
return True
|
|
68
|
-
|
|
69
|
-
# Se a exceção não foi um dos casos transitórios acima, não tente novamente.
|
|
70
|
-
# Isso cobre implicitamente os outros erros 4xx e quaisquer outras exceções.
|
|
71
|
-
return False
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
# --- Decorador Tenacity Principal ---
|
|
75
|
-
"""Retry policy padrão.
|
|
76
|
-
|
|
77
|
-
IMPORTANTE SOBRE A CONFIGURAÇÃO DE *stop*:
|
|
78
|
-
Antes usávamos: stop=(stop_after_attempt(3) | stop_after_delay(15))
|
|
79
|
-
Com chamadas HTTP usando timeout=30s, um único ReadTimeout já consumia >15s
|
|
80
|
-
e o Tenacity entendia que a condição de parada OR havia sido atingida,
|
|
81
|
-
encerrando o fluxo SEM realizar as tentativas restantes.
|
|
82
|
-
|
|
83
|
-
Para garantir que até 3 tentativas sejam feitas independentemente da duração
|
|
84
|
-
individual (desde que a aplicação queira esperar), removemos o limite de
|
|
85
|
-
tempo agregado e mantivemos apenas stop_after_attempt(3).
|
|
86
|
-
|
|
87
|
-
Se for necessário um limite total de tempo, ele deve ser configurado para um
|
|
88
|
-
valor superior a (timeout_http * número_de_tentativas) ou usar um timeout
|
|
89
|
-
menor na chamada requests.get.
|
|
90
|
-
"""
|
|
91
|
-
|
|
92
|
-
default_retry = retry(
|
|
93
|
-
retry=should_retry_exception,
|
|
94
|
-
wait=wait_exponential(multiplier=1.5, min=5, max=30),
|
|
95
|
-
stop=stop_after_attempt(3),
|
|
96
|
-
before_sleep=_log_before_sleep,
|
|
97
|
-
reraise=True,
|
|
98
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|