bpstat-pt-api 0.6.0__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.
@@ -0,0 +1,57 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ .venv/
6
+ venv/
7
+ .env
8
+
9
+ # Build artifacts
10
+ dist/
11
+ build/
12
+
13
+ # Tooling caches / reports
14
+ .pytest_cache/
15
+ .mypy_cache/
16
+ .ruff_cache/
17
+ .coverage
18
+ .coverage.*
19
+ coverage.xml
20
+ htmlcov/
21
+
22
+ # Local cache of downloaded BPstat API responses
23
+ .bpstat-cache/
24
+ .cache/
25
+
26
+ # Data / scratch output
27
+ *.csv
28
+ *.xlsx
29
+
30
+ # OS / editors
31
+ .DS_Store
32
+ .idea/
33
+ .vscode/
34
+
35
+ # Sphinx docs build output
36
+ docs/_build/
37
+
38
+ # LaTeX build files
39
+ *.aux
40
+ *.log
41
+ *.out
42
+ *.toc
43
+ *.bbl
44
+ *.blg
45
+ *.synctex.gz
46
+ *.fls
47
+ *.fdb_latexmk
48
+ *.bcf
49
+ *.run.xml
50
+ *.nav
51
+ *.snm
52
+ *.lof
53
+ *.lot
54
+ *.xdv
55
+
56
+ # Jupyter
57
+ .ipynb_checkpoints/
@@ -0,0 +1,314 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.6.0] - 2026-12-06
11
+
12
+ ### Added
13
+
14
+ - Requests now keep to BPstat's request limit. BPstat publishes no number
15
+ (its API guide asks to avoid unnecessary requests), so the client sends at
16
+ most one request per second, stated once as `bpstat.client.RATE_LIMIT`. A
17
+ shared throttle gates every request, retries included, across all clients
18
+ in the process; cache hits do not wait.
19
+ - `BPStatClient(requests_per_second=...)` and the CLI's
20
+ `--requests-per-second` flag slow requests down. `None` or `0` (the default)
21
+ means one per second; a faster rate raises `wrapkit.http.RateLimitError`.
22
+ - Transient failures (HTTP 429 and 5xx, dropped connections) are retried up
23
+ to four times, honouring a numeric `Retry-After` in full up to two minutes.
24
+ The CLI logs each retry as a WARNING before the final error line.
25
+
26
+ ### Changed
27
+
28
+ - Published to PyPI as `bpstat-pt-api`; wrapkit now comes from PyPI (`wrapkit>=0.4,<0.5`).
29
+ - **Breaking:** Python 3.12 is now the minimum supported version (3.10
30
+ reached end of life in October 2026); 3.10 and 3.11 users keep 0.5.2, the
31
+ last release that supports them. The `typing-extensions` dependency (needed
32
+ only before 3.11) is dropped.
33
+ - Lock refreshed; soupsieve 4.x (security fixes).
34
+ - The author e-mail in `pyproject.toml` and `CITATION.cff` is now the GitHub
35
+ no-reply address.
36
+ - Behaviour change: the client used to send requests back to back with no
37
+ throttle and no retries. Multi-series fetches and long series-id lists now
38
+ take about one second per request.
39
+ - README: the "License" section is now "Data and terms". The MIT License
40
+ covers the code only; BPstat's conditions (use is free; reproduce accurately,
41
+ name Banco de Portugal as the source, flag any change; read 2026-12-05) are
42
+ stated.
43
+
44
+ ### Removed
45
+
46
+ - **Breaking:** the optional on-disk response cache is gone. Provider terms
47
+ restrict storing their content, so the wrapper no longer keeps API answers
48
+ on disk. Removed: the `cache_dir` and `cache_ttl` arguments of
49
+ `BPStatClient`, the `DEFAULT_CACHE_TTL` constant (`bpstat.client`) and the
50
+ CLI's `--cache-dir` flag. Passing `cache_dir` now raises `TypeError`. Every
51
+ call goes to the API; with a cache directory set, a repeat request used to
52
+ be served from disk and is now sent again. The one-request-per-second limit
53
+ (`RATE_LIMIT`, enforced by the shared throttle) still applies to every
54
+ request. Cache folders you created with `cache_dir` (for example
55
+ `.bpstat-cache`) are no longer read and can be deleted by hand.
56
+
57
+ ## [0.5.2] - 2026-12-01
58
+
59
+ ### Changed
60
+
61
+ - wrapkit is now pinned at
62
+ [v0.2.10](https://github.com/hmorao95/wrapkit/releases/tag/v0.2.10) (was
63
+ v0.2.9). Its only change is to `DiskCache`, which now treats an entry dated
64
+ ahead of the clock as expired; bpstat has its own cache and does not use
65
+ `DiskCache`, so nothing changes here.
66
+
67
+ ### Fixed
68
+
69
+ - `update_csv` and `update_observations` read a stored CSV with pandas'
70
+ default missing-value markers, so a label or dimension code such as
71
+ `NA`, `N/A` or `null` came back as missing (and was written back
72
+ empty). Only an empty field now counts as missing.
73
+ - An empty (zero-byte) CSV passed to `update_csv` or
74
+ `update_observations` raised `EmptyDataError`; it now counts as no
75
+ stored data, like a missing file.
76
+ - A compact month bound such as `start="202402"` (or `--start 202402`)
77
+ raised `DateParseError`; it is now read as February 2024.
78
+
79
+ ## [0.5.1] - 2026-11-18
80
+
81
+ ### Changed
82
+
83
+ - wrapkit is now pinned at v0.2.9 (was v0.2.6). Its fixes are in parts this
84
+ package does not use, so nothing changes here. v0.2.9's security fixes
85
+ cover `RedactFilter`, the error line of wrapkit's `cli.run` and
86
+ `DiskCache`; bpstat needs no key, has its own CLI runner and cache, and
87
+ uses none of the three.
88
+
89
+ ### Fixed
90
+
91
+ - Asking for more than 100 series at once failed: the ids went out in one
92
+ request, which the API answers with HTTP 400 ("no more than 100
93
+ elements"). This broke `get_series`, `get_observations`, the `to_*`
94
+ writers and `update_csv` on a file holding more than 100 series. The
95
+ metadata is now requested in chunks of 100 and combined, in the
96
+ requested order.
97
+ - The CLI printed a traceback instead of a one-line `Error: ...` for
98
+ several ordinary mistakes. `bpstat series 1,2` (which Fire reads as one
99
+ tuple) now asks for both ids, and a list such as `[1,2]` works too; an id
100
+ that is not a whole number and a `--lookback-days` that is not a whole
101
+ number of days are reported as errors; a network failure (for example
102
+ running offline) and an output path that cannot be written now exit
103
+ with code 1 and a short message.
104
+ - On Windows, a clone whose path has non-ASCII characters (such as
105
+ `C:\Users\João\...`) left `bpstat` unimportable when `uv sync` built the
106
+ environment on Python 3.10 or 3.11, which read the editable install's `.pth`
107
+ file in the ANSI code page. The development environment now defaults to
108
+ Python 3.12 (`.python-version`), which reads it as UTF-8. CI still runs the
109
+ checks on 3.10 and the tests on 3.10 to 3.12. The README gives workarounds
110
+ for 3.10 and 3.11.
111
+
112
+ ## [0.5.0] - 2026-11-04
113
+
114
+ ### Fixed
115
+
116
+ - `update_csv` (and the CLI's `update-csv`) now picks up Banco de Portugal
117
+ revisions. It used to keep only dates after the last one stored, so a
118
+ revised value already in the file was never updated. Each run now
119
+ re-checks, per series, the observations from `lookback_days` (new
120
+ argument and `--lookback-days` flag, default 730) before the last stored
121
+ date and overwrites stored values that changed; older history is left as
122
+ stored. The rows a run returns, and the count in its log line and CLI
123
+ status line (now "Added or revised N observations in <path>"), include
124
+ revised rows. The first run after upgrading rewrites any values revised
125
+ in the last two years. The stored CSV is now read back with exact float
126
+ parsing, so a full-precision value is not mistaken for a revision.
127
+ `update_observations` and `new_observations` still return only strictly
128
+ newer dates.
129
+ - wrapkit is now pinned at v0.2.6 (was v0.2.1), which brings these fixes:
130
+ - Comparing a column that goes between missing and a value no longer crashes
131
+ the update.
132
+ - A numeric key matches whether it reads `110` or `110.0`, so a revised row
133
+ replaces the stored one instead of being added next to it.
134
+
135
+ ## [0.4.1] - 2026-11-03
136
+
137
+ ### Changed
138
+
139
+ - `.gitignore` now also ignores LaTeX build files (`*.aux`, `*.log`,
140
+ `*.synctex.gz`, `*.fdb_latexmk` and the like) and Jupyter checkpoints
141
+ (`.ipynb_checkpoints/`). No change to the package.
142
+
143
+ ## [0.4.0] - 2026-11-03
144
+
145
+ ### Changed
146
+
147
+ - wrapkit pin moved from v0.2.0 to [v0.2.1](https://github.com/hmorao95/wrapkit/releases/tag/v0.2.1):
148
+ log lines mask registered secrets longest first, so a secret that
149
+ contains another is never masked in pieces. No other change here.
150
+ - The shared plumbing now comes from
151
+ [`wrapkit`](https://github.com/hmorao95/wrapkit) v0.2.0, a new runtime
152
+ dependency installed from GitHub by tag: the package logger, the CLI's
153
+ console/file logging and plain log-file formatter, the CLI date-bound text
154
+ helper, the tidy shape's column order, dtypes and sort, polars conversion,
155
+ and CSV/Parquet writing. `colorlog` now arrives through it (`fire` stays a
156
+ direct dependency, as `main()` still calls it). The public API, outputs and
157
+ messages are unchanged.
158
+ - `--dimensions`, `--wide` and `--tidy` now work in any position, including
159
+ right before a series id (`bpstat observations --tidy 88877`); before, Fire
160
+ took the id as the flag's value.
161
+ - In the tidy shape a blank unit-of-measure label is now `<NA>`, like a missing
162
+ one (it was an empty string).
163
+ - `pre-commit` is no longer a dev dependency: run the hooks with
164
+ `uvx pre-commit` (for example `uvx pre-commit install`). This drops
165
+ `virtualenv` and `filelock` from `uv.lock`; filelock has no patched release
166
+ for Python 3.10.
167
+ - The manual Publish workflow's version check (tag = pyproject = CITATION) now runs
168
+ when the workflow is started from a release tag; it was tied to release events,
169
+ which no longer trigger the workflow.
170
+
171
+ ## [0.3.0] - 2026-11-01
172
+
173
+ ### Added
174
+
175
+ - Documentation site (Sphinx + PyData theme + MyST) in `docs/`: installation,
176
+ usage guide, API reference generated from the docstrings, glossary and the
177
+ changelog. `.github/workflows/docs.yml` builds it on every push and pull
178
+ request with warnings as errors and uploads the HTML as a `docs-html`
179
+ artifact; it is not deployed. A `docs` dependency group provides Sphinx, the
180
+ theme and MyST; build locally with
181
+ `uv run --group docs sphinx-build docs docs/_build/html`.
182
+ - `get_observations(..., tidy=True)`: opt-in long shape shared with the
183
+ author's other wrappers, with columns `date` (period start, from the
184
+ period-end `reference_date` and the series' periodicity), `series_id`,
185
+ `value` and `unit` (the cube's unit-of-measure dimension). The default output
186
+ is unchanged. `tidy` also works through `to_csv`, `to_excel`, `update_csv`,
187
+ `new_observations`, `update_observations` and the CLI (`--tidy`).
188
+ - `get_observations(..., as_polars=True)`: return a `polars.DataFrame`; polars
189
+ is an optional extra (`bpstat-pt-api[polars]`).
190
+ - `to_parquet()` and the `bpstat to-parquet` command; `pyarrow` is now a
191
+ runtime dependency.
192
+
193
+ - Logging: the library now logs to the `bpstat` logger (silent by default via a
194
+ `NullHandler`). Requests, HTTP status and timing, cache hits and expiries and
195
+ each series fetched are logged at DEBUG; a summary per fetch, export and
196
+ incremental update at INFO.
197
+ - CLI flags `--log-level` (colored console output on stderr, via `colorlog`;
198
+ default `warning`, so output is unchanged) and `--log-file` (appends a full
199
+ DEBUG log with timestamps), accepted with any subcommand. On a failed command
200
+ the traceback goes to the log.
201
+ - `py.typed` marker, so type checkers use the package's inline annotations
202
+ (PEP 561).
203
+ - `AGENTS.md` and `CLAUDE.md` with the contributor and agent conventions.
204
+ - `.github/workflows/publish.yml`: on a published GitHub release, checks that the
205
+ tag, `pyproject.toml` and `CITATION.cff` versions agree, builds with uv and
206
+ uploads to PyPI via Trusted Publishing.
207
+ - Tests that the version agrees across `pyproject.toml`, `CITATION.cff`,
208
+ `bpstat.__version__` and the README citation, that `py.typed` ships, and that
209
+ the CLI exits cleanly on an API error.
210
+ - `ty check` alongside mypy, in CI and pre-commit; `ty` joins the `typing`
211
+ dependency group.
212
+
213
+ ### Changed
214
+
215
+ - The repository was renamed from `fetch_bpstat_pt` to `bpstat-pt-api`, and the
216
+ distribution from `bpstat` to `bpstat-pt-api`. The import name (`bpstat`) and
217
+ the CLI command (`bpstat`) are unchanged. GitHub redirects the old URLs.
218
+ - The CLI now imports python-fire lazily inside `main()`, so importing
219
+ `bpstat.__main__` no longer loads Fire.
220
+ - The CLI prints a one-line `Error: ...` message and exits with status 1 on a
221
+ `BPStatError` or `ValueError`, instead of a traceback.
222
+ - Tooling aligned with the owner's reference wrapper (`eia-api`): the ruff rule
223
+ set adds `DOC`, `PT`, `T10` and uses `TC` (was `TCH`), with fewer and narrower
224
+ ignores; `docstring-code-format` is on; codespell is configured in
225
+ `pyproject.toml`; interrogate also covers `tests/`; mypy no longer pins
226
+ `python_version`; typing stubs moved to a `typing` dependency group, both
227
+ groups installed by default (`[tool.uv] default-groups`).
228
+ - CI runs ruff, ruff format, codespell, deptry, interrogate, mypy, ty and pytest
229
+ on Python 3.10, and pytest on 3.11 and 3.12.
230
+ - Pre-commit uses the upstream pre-commit-hooks, ruff and codespell hooks, and
231
+ runs mypy and ty through uv.
232
+ - README restructured (Installation, Usage, Command line, Development,
233
+ Citation, License). The quickstart no longer shows the removed
234
+ `observations --output` flag; it uses `to-csv`.
235
+ - Removed the unused `dead`, `pytest-mock` and `pre-commit-hooks` dev
236
+ dependencies.
237
+ - CI fails when test coverage drops below 95%: `fail_under = 95` in
238
+ `[tool.coverage.report]`, which `uv run pytest` enforces (coverage was
239
+ 97.45% when the threshold was set).
240
+
241
+ ### Fixed
242
+
243
+ - `BPStatClient(cache_dir="")` no longer caches in the current directory: any
244
+ empty value (`None` or `""`) now turns the cache off, as in the other wrappers.
245
+ - `--start` given as a bare year (`bpstat observations 88877 --start 2026`) now
246
+ filters by that year. Python Fire passes such values as ints, which pandas
247
+ read as nanoseconds since 1970, so the filter kept every row. `observations`,
248
+ `to-csv` and `to-excel` now hand `--start`/`--end` on as text, and the client
249
+ also converts its bounds with `str()` before parsing.
250
+
251
+ ## [0.2.1] - 2026-08-01
252
+
253
+ ### Fixed
254
+
255
+ - `get_observations(end=...)` now expands a partial `end` (e.g. `"2025"` or
256
+ `"2025-06"`) to the end of that period. Because BPstat stamps observations at
257
+ period end, the previous behaviour compared against the period *start* and
258
+ silently dropped the final period(s).
259
+ - A non-positive `cache_ttl` now deterministically bypasses the cache instead of
260
+ depending on file-mtime versus `time.time()` resolution.
261
+
262
+ ## [0.2.0] - 2026-08-01
263
+
264
+ ### Added
265
+
266
+ - Date-range filtering (`start` / `end`) and a `wide=True` pivot (one column per
267
+ series id) on `BPStatClient.get_observations`.
268
+ - `BPStatClient.to_csv` and `BPStatClient.to_excel` one-call exports (Excel via
269
+ the `openpyxl` engine).
270
+ - `BPStatClient.update_csv` (incremental update of a CSV file in place) and
271
+ `BPStatClient.new_observations` (file-free delta), alongside the existing
272
+ `update_observations`.
273
+ - Optional on-disk response cache via `cache_dir` / `cache_ttl`.
274
+ - CLI subcommands `to-csv`, `to-excel`, `update-csv`, and `new-observations`,
275
+ plus `--start` / `--end` / `--wide` on `observations` and a `--cache-dir`
276
+ flag.
277
+
278
+ ### Changed
279
+
280
+ - The `observations` CLI subcommand now always streams CSV to stdout; use
281
+ `to-csv` (or `to-excel`) to write a file.
282
+
283
+ ## [0.1.0] - 2026-07-31
284
+
285
+ ### Added
286
+
287
+ - `BPStatClient`, a typed wrapper over the Banco de Portugal BPstat REST API
288
+ (`https://bpstat.bportugal.pt/data/v1`) with domain, dataset, series-metadata,
289
+ and observation endpoints, plus context-manager support and configurable
290
+ language, timeout, and session.
291
+ - JSON-stat 2.0 parser (`jsonstat_to_dataframe`) that unrolls the cube into a
292
+ tidy long-format DataFrame, handling both dense-array and sparse-object
293
+ `value` encodings and optionally emitting non-time dimension columns.
294
+ - Incremental extraction via `BPStatClient.update_observations`: fetches only
295
+ observations newer than those already held, accepting a DataFrame, a CSV
296
+ path, or none, and extracting unseen series in full.
297
+ - Typed models `Domain`, `Dataset`, and `Series`.
298
+ - `bpstat` command-line interface, built with
299
+ [python-fire](https://github.com/google/python-fire), exposing `domains`,
300
+ `series`, and `observations` subcommands and a global `--lang` flag.
301
+ - Test suite (48 tests) with mocked HTTP, and a GitHub Actions CI workflow
302
+ running ruff, mypy, and pytest across Python 3.10–3.12.
303
+
304
+ [Unreleased]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.6.0...HEAD
305
+ [0.6.0]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.5.2...v0.6.0
306
+ [0.5.2]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.5.1...v0.5.2
307
+ [0.5.1]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.5.0...v0.5.1
308
+ [0.5.0]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.4.1...v0.5.0
309
+ [0.4.1]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.4.0...v0.4.1
310
+ [0.4.0]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.3.0...v0.4.0
311
+ [0.3.0]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.2.1...v0.3.0
312
+ [0.2.1]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.2.0...v0.2.1
313
+ [0.2.0]: https://github.com/hmorao95/bpstat-pt-api/compare/v0.1.0...v0.2.0
314
+ [0.1.0]: https://github.com/hmorao95/bpstat-pt-api/releases/tag/v0.1.0
@@ -0,0 +1,23 @@
1
+ cff-version: 1.2.0
2
+ message: "If you use this software, please cite it as below."
3
+ title: "bpstat-pt-api: A typed Python wrapper for the Banco de Portugal BPstat API"
4
+ abstract: >-
5
+ A typed Python wrapper for the Banco de Portugal BPstat statistics API that
6
+ resolves series metadata, downloads observations, and parses JSON-stat 2.0
7
+ payloads into tidy pandas DataFrames.
8
+ type: software
9
+ authors:
10
+ - family-names: "Morão"
11
+ given-names: "Hugo"
12
+ email: "132693995+hmorao95@users.noreply.github.com"
13
+ version: "0.6.0"
14
+ date-released: "2026-12-06"
15
+ license: MIT
16
+ repository-code: "https://github.com/hmorao95/bpstat-pt-api"
17
+ url: "https://github.com/hmorao95/bpstat-pt-api"
18
+ keywords:
19
+ - statistics
20
+ - portugal
21
+ - bpstat
22
+ - banco de portugal
23
+ - json-stat
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hugo Morão
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.