-
Notifications
You must be signed in to change notification settings - Fork 2
fix(cvm): match holdings CNPJ with or without punctuation #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |||||||||||||
|
|
||||||||||||||
| import csv | ||||||||||||||
| import io | ||||||||||||||
| import re | ||||||||||||||
| import sys | ||||||||||||||
| import zipfile | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -43,6 +44,13 @@ | |||||||||||||
|
|
||||||||||||||
| CDA_URL = f"{CVM_BASE}/FI/DOC/CDA/DADOS/cda_fi_{{ym}}.zip" | ||||||||||||||
|
|
||||||||||||||
| _NON_DIGIT = re.compile(r"\D") | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _digits(value: str | None) -> str: | ||||||||||||||
| """Strip a CNPJ down to its digits so punctuated and bare forms compare equal.""" | ||||||||||||||
| return _NON_DIGIT.sub("", value or "") | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| class FundHolding(BaseModel): | ||||||||||||||
| """One holding row (one asset position) inside one fund's portfolio.""" | ||||||||||||||
|
|
@@ -139,7 +147,7 @@ async def get_fund_holdings( | |||||||||||||
| """ | ||||||||||||||
| ym = f"{year}{month:02d}" | ||||||||||||||
| raw = await get_bytes(CDA_URL.format(ym=ym), cache_ttl=86400) | ||||||||||||||
| cnpj_norm = cnpj.strip() | ||||||||||||||
| cnpj_norm = _digits(cnpj) | ||||||||||||||
| wanted_blocks = {b.upper() for b in blocks} if blocks else None | ||||||||||||||
| holdings: list[FundHolding] = [] | ||||||||||||||
| with zipfile.ZipFile(io.BytesIO(raw)) as zf: | ||||||||||||||
|
|
@@ -152,7 +160,7 @@ async def get_fund_holdings( | |||||||||||||
| with zf.open(entry) as f: | ||||||||||||||
| reader = csv.DictReader(io.StringIO(f.read().decode("iso-8859-1")), delimiter=";") | ||||||||||||||
| for row in reader: | ||||||||||||||
| row_cnpj = (row.get("CNPJ_FUNDO_CLASSE") or row.get("CNPJ_FUNDO", "")).strip() | ||||||||||||||
| row_cnpj = _digits(row.get("CNPJ_FUNDO_CLASSE") or row.get("CNPJ_FUNDO")) | ||||||||||||||
| if row_cnpj != cnpj_norm: | ||||||||||||||
| continue | ||||||||||||||
|
Comment on lines
+163
to
165
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of running the regex-based
Suggested change
|
||||||||||||||
| h = _row_to_holding(row, block, include_raw=include_raw) | ||||||||||||||
|
|
||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can optimize this function by validating the CNPJ and pre-computing both its bare and punctuated forms before downloading the large ZIP file. This avoids unnecessary network requests for invalid inputs and allows us to perform fast string comparisons in the hot loop instead of running regex normalization on every single row.