pxwebpy 0.5.2__tar.gz → 0.5.4__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.
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/.github/workflows/ci.yml +17 -11
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/.github/workflows/docs.yml +4 -4
- pxwebpy-0.5.4/.github/workflows/release.yml +41 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/.gitignore +1 -0
- pxwebpy-0.5.4/.pre-commit-config.yaml +11 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/PKG-INFO +3 -1
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/development.qmd +25 -19
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/examples.qmd +32 -2
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/style.css +5 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/version_config.py +1 -1
- pxwebpy-0.5.4/noxfile.py +13 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/pyproject.toml +9 -2
- pxwebpy-0.5.4/src/pxweb/__init__.py +8 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/src/pxweb/_internal/client.py +34 -3
- pxwebpy-0.5.4/src/pxweb/_version.py +1 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/src/pxweb/api.py +46 -24
- pxwebpy-0.5.4/tests/smoke_test.py +12 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/tests/test_client.py +4 -2
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/uv.lock +719 -593
- pxwebpy-0.5.2/.github/workflows/release.yml +0 -29
- pxwebpy-0.5.2/src/pxweb/__init__.py +0 -3
- pxwebpy-0.5.2/src/pxweb/_version.py +0 -1
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/.python-version +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/CONTRIBUTING.md +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/LICENSE +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/README.md +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/README.qmd +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/.gitignore +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/_brand.yml +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/_extensions/machow/interlinks/.gitignore +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/_extensions/machow/interlinks/_extension.yml +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/_extensions/machow/interlinks/interlinks.lua +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/_quarto.yml +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/docs/index.qmd +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/src/pxweb/_internal/__init__.py +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/src/pxweb/_internal/functions.py +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/src/pxweb/py.typed +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/tests/__snapshots__/test_functions.ambr +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/tests/code_list_agg_/303/205lder10/303/245rJ.json" +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/tests/table_variables_TAB5444.json +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/tests/test_api.py +0 -0
- {pxwebpy-0.5.2 → pxwebpy-0.5.4}/tests/test_functions.py +0 -0
|
@@ -9,7 +9,7 @@ on:
|
|
|
9
9
|
|
|
10
10
|
jobs:
|
|
11
11
|
build:
|
|
12
|
-
name: ${{ matrix.os }}
|
|
12
|
+
name: ${{ matrix.os }}
|
|
13
13
|
|
|
14
14
|
strategy:
|
|
15
15
|
matrix:
|
|
@@ -17,32 +17,38 @@ jobs:
|
|
|
17
17
|
- ubuntu-latest
|
|
18
18
|
- windows-latest
|
|
19
19
|
- macos-latest
|
|
20
|
-
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
|
21
20
|
fail-fast: false
|
|
22
21
|
|
|
23
22
|
runs-on: ${{ matrix.os }}
|
|
24
23
|
steps:
|
|
25
|
-
- uses: actions/checkout@
|
|
24
|
+
- uses: actions/checkout@v5
|
|
26
25
|
with:
|
|
27
26
|
fetch-depth: 0
|
|
28
27
|
|
|
29
|
-
- name:
|
|
30
|
-
uses: actions/setup-python@
|
|
28
|
+
- name: Install Python
|
|
29
|
+
uses: actions/setup-python@v6
|
|
31
30
|
with:
|
|
32
|
-
python-version:
|
|
31
|
+
python-version-file: ".python-version"
|
|
33
32
|
|
|
34
33
|
- name: Install uv
|
|
35
34
|
uses: astral-sh/setup-uv@v6
|
|
36
35
|
with:
|
|
37
|
-
version: "0.
|
|
36
|
+
version: "0.9.11"
|
|
38
37
|
enable-cache: true
|
|
39
38
|
cache-dependency-glob: "uv.lock"
|
|
40
|
-
python-version: ${{ matrix.python-version }}
|
|
41
39
|
|
|
42
40
|
- name: Install the project
|
|
43
|
-
run: uv sync --
|
|
41
|
+
run: uv sync --locked
|
|
42
|
+
|
|
43
|
+
- name: Lint
|
|
44
|
+
uses: astral-sh/ruff-action@v3
|
|
44
45
|
|
|
45
|
-
- name: Run tests
|
|
46
|
+
- name: Run tests
|
|
47
|
+
run: |
|
|
48
|
+
uv run nox -s tests
|
|
49
|
+
|
|
50
|
+
- name: Build & smoke test
|
|
51
|
+
shell: bash
|
|
46
52
|
run: |
|
|
47
|
-
uv run pytest
|
|
48
53
|
uv build
|
|
54
|
+
uv run --isolated --no-project --with dist/*.whl tests/smoke_test.py
|
|
@@ -13,11 +13,11 @@ jobs:
|
|
|
13
13
|
contents: write
|
|
14
14
|
|
|
15
15
|
steps:
|
|
16
|
-
- uses: actions/checkout@
|
|
16
|
+
- uses: actions/checkout@v5
|
|
17
17
|
with:
|
|
18
18
|
fetch-depth: 0
|
|
19
19
|
|
|
20
|
-
- name: Install Python
|
|
20
|
+
- name: Install Python
|
|
21
21
|
uses: actions/setup-python@v5
|
|
22
22
|
with:
|
|
23
23
|
python-version-file: ".python-version"
|
|
@@ -25,14 +25,14 @@ jobs:
|
|
|
25
25
|
- name: Install uv
|
|
26
26
|
uses: astral-sh/setup-uv@v6
|
|
27
27
|
with:
|
|
28
|
-
version: "0.
|
|
28
|
+
version: "0.9.11"
|
|
29
29
|
|
|
30
30
|
- name: Set up Quarto
|
|
31
31
|
uses: quarto-dev/quarto-actions/setup@v2
|
|
32
32
|
|
|
33
33
|
- name: Build docs
|
|
34
34
|
run: |
|
|
35
|
-
uv sync --
|
|
35
|
+
uv sync --locked
|
|
36
36
|
cd docs && uv run quartodoc build
|
|
37
37
|
|
|
38
38
|
- name: Render and publish
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build-release:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
environment: release
|
|
12
|
+
permissions:
|
|
13
|
+
id-token: write
|
|
14
|
+
contents: read
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v5
|
|
18
|
+
with:
|
|
19
|
+
fetch-depth: 0
|
|
20
|
+
|
|
21
|
+
- name: Install uv
|
|
22
|
+
uses: astral-sh/setup-uv@v6
|
|
23
|
+
with:
|
|
24
|
+
version: "0.9.11"
|
|
25
|
+
|
|
26
|
+
- name: Install Python
|
|
27
|
+
uses: actions/setup-python@v5
|
|
28
|
+
with:
|
|
29
|
+
python-version-file: ".python-version"
|
|
30
|
+
|
|
31
|
+
- name: Build for distribution
|
|
32
|
+
run: uv build
|
|
33
|
+
|
|
34
|
+
- name: Smoke test (wheel)
|
|
35
|
+
run: uv run --isolated --no-project --with dist/*.whl tests/smoke_test.py
|
|
36
|
+
|
|
37
|
+
- name: Smoke test (source distribution)
|
|
38
|
+
run: uv run --isolated --no-project --with dist/*.tar.gz tests/smoke_test.py
|
|
39
|
+
|
|
40
|
+
- name: Publish to PyPI
|
|
41
|
+
run: uv publish
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pxwebpy
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.4
|
|
4
4
|
Summary: Client library for the PxWeb API to easily load data into a DataFrame.
|
|
5
5
|
Project-URL: Repository, https://github.com/stefur/pxwebpy
|
|
6
6
|
Project-URL: Documentation, https://stefur.github.io/pxwebpy
|
|
@@ -14,7 +14,9 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.11
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
18
|
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: packaging>=25.0
|
|
18
20
|
Requires-Dist: requests-cache>=1.2.1
|
|
19
21
|
Requires-Dist: requests>=2.32.3
|
|
20
22
|
Description-Content-Type: text/markdown
|
|
@@ -12,13 +12,8 @@ This project is set up to use [`uv`](https://docs.astral.sh/uv/) to manage Pytho
|
|
|
12
12
|
dependencies. First, be sure you
|
|
13
13
|
[have `uv` installed](https://docs.astral.sh/uv/getting-started/installation/).
|
|
14
14
|
|
|
15
|
-
To install dependencies and set up the virtual environment:
|
|
16
15
|
```bash
|
|
17
|
-
#
|
|
18
|
-
git clone https://github.com/stefur/pxwebpy.git
|
|
19
|
-
cd pxwebpy
|
|
20
|
-
|
|
21
|
-
# Sync dependencies and create the virtual environment
|
|
16
|
+
# Install dependencies and set up the virtual environment
|
|
22
17
|
uv sync
|
|
23
18
|
```
|
|
24
19
|
|
|
@@ -27,36 +22,50 @@ For more details on how to use `uv`, head over to the [`uv` docs](https://docs.a
|
|
|
27
22
|
## Formatting
|
|
28
23
|
|
|
29
24
|
### Code
|
|
30
|
-
`ruff` is used for linting and formatting
|
|
25
|
+
`ruff` is used for linting and formatting, automatically executed in a `pre-commit` hook.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Install the pre-commit hooks
|
|
29
|
+
uv run pre-commit install
|
|
30
|
+
```
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
To run `ruff` manually from the command line or editor, see the [`ruff` documentation](https://docs.astral.sh/ruff/).
|
|
33
33
|
|
|
34
34
|
### Docstrings
|
|
35
35
|
Follow the [`NumPy`](https://numpydoc.readthedocs.io/en/latest/format.html#overview) style guide when writing docstrings. This is important for being able to [build the documentation](#building-the-documentation).
|
|
36
36
|
|
|
37
37
|
## Running tests
|
|
38
|
-
|
|
38
|
+
For fast iteration on specific tests, use `uv run pytest`:
|
|
39
39
|
|
|
40
40
|
```bash
|
|
41
|
-
uv run pytest
|
|
42
|
-
uv run pytest -s tests/test_client.py # one test file
|
|
41
|
+
uv run pytest -s tests/test_client.py # one test file
|
|
43
42
|
uv run pytest -s tests/test_client.py::test_call_raises_http_error # one specific test
|
|
44
43
|
```
|
|
45
44
|
|
|
46
|
-
To
|
|
45
|
+
To run the complete test suite across all supported Python versions, use `nox`:
|
|
46
|
+
```bash
|
|
47
|
+
uv run nox # for the entire suite
|
|
48
|
+
uv run nox -s tests # same thing, just being explicit about the session
|
|
49
|
+
```
|
|
47
50
|
|
|
51
|
+
To check test coverage:
|
|
48
52
|
```bash
|
|
49
|
-
uv run pytest --cov=
|
|
53
|
+
uv run pytest --cov=pxweb # for terminal output
|
|
54
|
+
uv run pytest --cov=pxweb --cov-report=html # to get a report
|
|
50
55
|
```
|
|
51
56
|
|
|
52
|
-
Some tests utilize [`syrupy`](https://github.com/syrupy-project/syrupy) for snapshot testing to ensure consistency. If snapshots
|
|
57
|
+
Some tests utilize [`syrupy`](https://github.com/syrupy-project/syrupy) for snapshot testing to ensure consistency. If snapshots need to be updated:
|
|
58
|
+
```bash
|
|
59
|
+
uv run pytest --snapshot-update
|
|
60
|
+
```
|
|
53
61
|
|
|
54
62
|
## Building the documentation
|
|
55
63
|
Documentation is built using [`quartodoc`](https://github.com/machow/quartodoc), thus you also need [`quarto`](https://quarto.org/docs/get-started/) installed on your machine if you wish to build the documentation locally. This is usually a good idea to check that the output looks as expected. The documentation is automatically built and published upon each release.
|
|
56
64
|
|
|
57
|
-
With `quarto` installed
|
|
65
|
+
With `quarto` installed the documentation can be rendered:
|
|
58
66
|
|
|
59
67
|
```bash
|
|
68
|
+
cd docs
|
|
60
69
|
uv run quartodoc build
|
|
61
70
|
uv run quarto render
|
|
62
71
|
```
|
|
@@ -67,8 +76,5 @@ The rendered output is found in the folder called `_site`.
|
|
|
67
76
|
When you’re ready to contribute:
|
|
68
77
|
|
|
69
78
|
1. Clone the repository and create a new branch for your changes.
|
|
70
|
-
2. Ensure
|
|
71
|
-
* All tests pass locally (`uv run pytest`)
|
|
72
|
-
* Code is linted and formatted (`uv run ruff check && uv run ruff format`)
|
|
73
|
-
* Documentation (if relevant) builds successfully
|
|
79
|
+
2. Ensure tests pass and code is properly formatted.
|
|
74
80
|
3. Open a pull request to the `main` branch.
|
|
@@ -75,8 +75,8 @@ api.tables_on_path(path_id="BE0101A")
|
|
|
75
75
|
It's also possible to search for tables using the `~~.PxApi.search()` method.
|
|
76
76
|
|
|
77
77
|
```{python}
|
|
78
|
-
# Keeping it simple and just look for tables updated in the past
|
|
79
|
-
results = api.search(query="energi", past_days=
|
|
78
|
+
# Keeping it simple and just look for tables updated in the past 180 days matching the query string
|
|
79
|
+
results = api.search(query="energi", past_days=180)
|
|
80
80
|
|
|
81
81
|
# Checking how many tables there are in the results
|
|
82
82
|
len(results.get("tables"))
|
|
@@ -219,3 +219,33 @@ pl.DataFrame(data)
|
|
|
219
219
|
|
|
220
220
|
### In-memory caching
|
|
221
221
|
By default `pxwebpy` uses in-memory caching for API responses, which can be useful for exploration and iterative use. Caching both reduces the load on the API and speeds up execution. However it can be turned off if needed simply by setting the attribute `disable_cache` to `True`.
|
|
222
|
+
|
|
223
|
+
## Debugging
|
|
224
|
+
Sometimes things don't work as expected. To troubleshoot you can set the log level to `DEBUG` to get verbose output of the queries sent to the API.
|
|
225
|
+
|
|
226
|
+
```{python}
|
|
227
|
+
# | eval: false
|
|
228
|
+
import logging
|
|
229
|
+
|
|
230
|
+
# Set the log level to debug for the root logger which will affect all loaded modules, which can be quite noisy
|
|
231
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
232
|
+
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
```{python}
|
|
236
|
+
# | echo: false
|
|
237
|
+
import logging
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
```{python}
|
|
241
|
+
# Or set up a handler just for pxwebpy
|
|
242
|
+
handler = logging.StreamHandler()
|
|
243
|
+
handler.setFormatter(
|
|
244
|
+
logging.Formatter("%(levelname)s:%(thread)s:%(name)s:%(message)s")
|
|
245
|
+
)
|
|
246
|
+
logging.getLogger("pxweb").addHandler(handler)
|
|
247
|
+
logging.getLogger("pxweb").setLevel(logging.DEBUG)
|
|
248
|
+
|
|
249
|
+
# Set up an API object with the logging enabled
|
|
250
|
+
ssb = PxApi("ssb")
|
|
251
|
+
```
|
pxwebpy-0.5.4/noxfile.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from nox import Session, options
|
|
2
|
+
from nox_uv import session
|
|
3
|
+
|
|
4
|
+
options.default_venv_backend = "uv"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@session(
|
|
8
|
+
name="tests",
|
|
9
|
+
python=["3.10", "3.11", "3.12", "3.13", "3.14"],
|
|
10
|
+
uv_groups=["dev"],
|
|
11
|
+
)
|
|
12
|
+
def test(s: Session) -> None:
|
|
13
|
+
s.run("python", "-m", "pytest", "-vv")
|
|
@@ -12,11 +12,13 @@ classifiers = [
|
|
|
12
12
|
"Programming Language :: Python :: 3.11",
|
|
13
13
|
"Programming Language :: Python :: 3.12",
|
|
14
14
|
"Programming Language :: Python :: 3.13",
|
|
15
|
+
"Programming Language :: Python :: 3.14",
|
|
15
16
|
]
|
|
16
17
|
authors = [
|
|
17
18
|
{ name = "Stefan Furne", email = "stefan@furne.net" }
|
|
18
19
|
]
|
|
19
20
|
dependencies = [
|
|
21
|
+
"packaging>=25.0",
|
|
20
22
|
"requests>=2.32.3",
|
|
21
23
|
"requests-cache>=1.2.1",
|
|
22
24
|
]
|
|
@@ -50,20 +52,25 @@ fix = true
|
|
|
50
52
|
[tool.ruff.format]
|
|
51
53
|
docstring-code-format = true
|
|
52
54
|
|
|
55
|
+
[tool.ruff.lint]
|
|
56
|
+
extend-select = ["I"]
|
|
57
|
+
|
|
53
58
|
[dependency-groups]
|
|
54
59
|
dev = [
|
|
55
60
|
"ipykernel>=6.29.5",
|
|
56
61
|
"nbclient>=0.10.2",
|
|
57
62
|
"nbformat>=5.10.4",
|
|
63
|
+
"nox>=2025.11.12",
|
|
64
|
+
"nox-uv>=0.6.3",
|
|
58
65
|
"pandas>=2.2.3",
|
|
59
66
|
"pandas-stubs>=2.2.2.240807",
|
|
60
67
|
"polars>=1.19.0",
|
|
61
|
-
"
|
|
68
|
+
"pre-commit>=4.4.0",
|
|
69
|
+
"pyarrow>=22.0.0",
|
|
62
70
|
"pytest>=8.3.4",
|
|
63
71
|
"pytest-cov>=6.2.1",
|
|
64
72
|
"quartodoc>=0.11.1",
|
|
65
73
|
"rich>=14.1.0",
|
|
66
|
-
"ruff>=0.14.4",
|
|
67
74
|
"syrupy>=4.8.0",
|
|
68
75
|
"types-requests>=2.32.0.20241016",
|
|
69
76
|
]
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import time
|
|
2
|
+
from logging import getLogger
|
|
2
3
|
from threading import Lock
|
|
3
4
|
|
|
4
5
|
from packaging.version import parse
|
|
5
6
|
from requests.exceptions import HTTPError, JSONDecodeError
|
|
6
7
|
from requests_cache import CachedSession, CacheSettings, Request
|
|
7
8
|
|
|
9
|
+
logger = getLogger(__name__)
|
|
10
|
+
|
|
8
11
|
|
|
9
12
|
class ApiVersionError(Exception):
|
|
10
13
|
"""Raised if the API version is not >2.0.0"""
|
|
@@ -31,6 +34,7 @@ class Client:
|
|
|
31
34
|
# Setting up params used for every query
|
|
32
35
|
self.params: dict = {"lang": language}
|
|
33
36
|
|
|
37
|
+
logger.debug("Getting the API configuration")
|
|
34
38
|
# Run the init without rate limiting since it's not set up yet
|
|
35
39
|
configuration = self.call(endpoint="/config", enforce_rate_limit=False)
|
|
36
40
|
|
|
@@ -44,6 +48,17 @@ class Client:
|
|
|
44
48
|
self.time_window: int = configuration.get("timeWindow")
|
|
45
49
|
self.call_timestamps: list[float] = []
|
|
46
50
|
|
|
51
|
+
logger.debug(
|
|
52
|
+
f"Query size is limited to {self.max_data_cells} number of data cells"
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
if self.max_calls > 0:
|
|
56
|
+
logger.debug(
|
|
57
|
+
f"Rate limiting set to a maximum of {self.max_calls} calls per time window of {self.time_window} seconds"
|
|
58
|
+
)
|
|
59
|
+
else:
|
|
60
|
+
logger.debug("No rate limit is configured")
|
|
61
|
+
|
|
47
62
|
# Now that we have the configuration set up, get the language if needed
|
|
48
63
|
self.params["lang"] = language or configuration.get(
|
|
49
64
|
"defaultLanguage", None
|
|
@@ -69,9 +84,16 @@ class Client:
|
|
|
69
84
|
method="POST" if query else "GET",
|
|
70
85
|
url=self.url + endpoint,
|
|
71
86
|
json=query,
|
|
72
|
-
params=
|
|
87
|
+
params=(
|
|
88
|
+
set_params := self.params | params if params else self.params
|
|
89
|
+
),
|
|
73
90
|
).prepare()
|
|
74
91
|
|
|
92
|
+
logger.debug(f"{request.method} request prepared for {request.url}")
|
|
93
|
+
logger.debug(f"Request with parameters: {set_params}")
|
|
94
|
+
if query:
|
|
95
|
+
logger.debug(f"Request with query: {query}")
|
|
96
|
+
|
|
75
97
|
# Handle cache settings
|
|
76
98
|
if not self.session.settings.disabled:
|
|
77
99
|
# Use the request as the cache key so we can look it up first
|
|
@@ -79,10 +101,11 @@ class Client:
|
|
|
79
101
|
|
|
80
102
|
# If there's a cache, go ahead without rate limiting
|
|
81
103
|
if cache_key in self.session.cache.responses:
|
|
104
|
+
logger.debug("Request found in cache")
|
|
82
105
|
return self.session.send(request).json()
|
|
83
106
|
|
|
84
|
-
# Otherwise rate limit first
|
|
85
|
-
if enforce_rate_limit:
|
|
107
|
+
# Otherwise rate limit first, if there's a limit set by the API
|
|
108
|
+
if enforce_rate_limit and self.max_calls > 0:
|
|
86
109
|
# Wait if needed
|
|
87
110
|
self.rate_limit()
|
|
88
111
|
|
|
@@ -91,6 +114,7 @@ class Client:
|
|
|
91
114
|
# a heavy number of subqueries. The response contains a retry-after in the headers
|
|
92
115
|
# so we basically back off and then go again
|
|
93
116
|
for attempt in range(max_retries + 1):
|
|
117
|
+
logger.debug(f"Sending request, attempt {attempt + 1}")
|
|
94
118
|
response = self.session.send(request)
|
|
95
119
|
if response.ok:
|
|
96
120
|
return response.json()
|
|
@@ -99,6 +123,10 @@ class Client:
|
|
|
99
123
|
# need to "back off" and then retry
|
|
100
124
|
elif response.status_code == 429 and attempt < max_retries:
|
|
101
125
|
retry_after = response.headers.get("Retry-After", "1")
|
|
126
|
+
logger.debug(
|
|
127
|
+
f"Response 429, backing off for {retry_after} second(s)"
|
|
128
|
+
)
|
|
129
|
+
|
|
102
130
|
time.sleep(int(retry_after))
|
|
103
131
|
# Then continue to do another attempt
|
|
104
132
|
continue
|
|
@@ -130,5 +158,8 @@ class Client:
|
|
|
130
158
|
return None
|
|
131
159
|
# Otherise there have been to many calls, so we need to sleep
|
|
132
160
|
sleep_time = self.time_window - (now - self.call_timestamps[0])
|
|
161
|
+
logger.debug(
|
|
162
|
+
f"Hit the rate limit, sleeping for {sleep_time} second(s)"
|
|
163
|
+
)
|
|
133
164
|
# This way we hold the lock so other threads queue up
|
|
134
165
|
time.sleep(sleep_time)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.5.4"
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from concurrent.futures import ThreadPoolExecutor
|
|
2
|
+
from logging import getLogger
|
|
2
3
|
from typing import Literal, TypeAlias, Union
|
|
3
4
|
|
|
4
5
|
from ._internal.client import Client
|
|
@@ -10,6 +11,8 @@ from ._internal.functions import (
|
|
|
10
11
|
unpack_table_data,
|
|
11
12
|
)
|
|
12
13
|
|
|
14
|
+
logger = getLogger(__name__)
|
|
15
|
+
|
|
13
16
|
KnownApi: TypeAlias = Literal["scb", "ssb"]
|
|
14
17
|
"""Selectable APIs with a preconfigured URL"""
|
|
15
18
|
|
|
@@ -45,6 +48,8 @@ class PxApi:
|
|
|
45
48
|
Disable the in-memory cache that is used for API responses.
|
|
46
49
|
timeout: int
|
|
47
50
|
The timeout in seconds to use when calling the API.
|
|
51
|
+
max_workers: int, optional
|
|
52
|
+
Maximum number of workers to use for parallel execution when fetching data. Set to 1 for sequential execution or set a fixed number to limit the amount of concurrent requests being sent. None uses automatic sizing.
|
|
48
53
|
|
|
49
54
|
Examples
|
|
50
55
|
--------
|
|
@@ -63,7 +68,9 @@ class PxApi:
|
|
|
63
68
|
language: str | None = None,
|
|
64
69
|
disable_cache: bool = False,
|
|
65
70
|
timeout: int = 30,
|
|
71
|
+
max_workers: int | None = None,
|
|
66
72
|
):
|
|
73
|
+
logger.debug("Setting up the client")
|
|
67
74
|
self._client = Client(
|
|
68
75
|
url=API_URLS.get(url, url),
|
|
69
76
|
language=language,
|
|
@@ -71,11 +78,14 @@ class PxApi:
|
|
|
71
78
|
disable_cache=disable_cache,
|
|
72
79
|
) # Resolve the name if known else assume it's a full URL
|
|
73
80
|
|
|
81
|
+
logger.debug("Getting the number of tables available")
|
|
74
82
|
# Pull in the total number of elements (tables) available
|
|
75
83
|
self.number_of_tables: int | None = self._client.call(
|
|
76
84
|
endpoint="/tables"
|
|
77
85
|
)["page"]["totalElements"]
|
|
78
86
|
|
|
87
|
+
self._max_workers = max_workers
|
|
88
|
+
|
|
79
89
|
def __repr__(self) -> str:
|
|
80
90
|
return f"""PxApi(url='{self._client.url}',
|
|
81
91
|
language={self._client.params.get("lang")},
|
|
@@ -119,13 +129,23 @@ class PxApi:
|
|
|
119
129
|
"""Set the cache setting."""
|
|
120
130
|
self._client.session.settings.disabled = value
|
|
121
131
|
|
|
132
|
+
@property
|
|
133
|
+
def max_workers(self) -> int | None:
|
|
134
|
+
"""Get the maximum number of workers used for parallel execution when getting data."""
|
|
135
|
+
return self._max_workers
|
|
136
|
+
|
|
137
|
+
@max_workers.setter
|
|
138
|
+
def max_workers(self, value: int) -> None:
|
|
139
|
+
"""Set the maximum number of workers to use for parallel execution when getting data."""
|
|
140
|
+
self._max_workers = value
|
|
141
|
+
|
|
122
142
|
@property
|
|
123
143
|
def language(self) -> str:
|
|
124
144
|
"""Get the current language."""
|
|
125
145
|
return self._client.params["lang"]
|
|
126
146
|
|
|
127
147
|
@language.setter
|
|
128
|
-
def language(self, value) -> None:
|
|
148
|
+
def language(self, value: str) -> None:
|
|
129
149
|
"""Set the language to use with the API."""
|
|
130
150
|
self._client.params["lang"] = value
|
|
131
151
|
|
|
@@ -445,6 +465,14 @@ class PxApi:
|
|
|
445
465
|
# Perform wildcard expansion
|
|
446
466
|
value_codes = expand_wildcards(value_codes, table_variables)
|
|
447
467
|
|
|
468
|
+
def fetch(query):
|
|
469
|
+
return unpack_table_data(
|
|
470
|
+
self._client.call(
|
|
471
|
+
endpoint=f"/tables/{table_id}/data", query=query
|
|
472
|
+
),
|
|
473
|
+
show=show,
|
|
474
|
+
)
|
|
475
|
+
|
|
448
476
|
# Now count the data cells we're getting to check against the max allowed
|
|
449
477
|
if count_data_cells(value_codes) > self._client.max_data_cells:
|
|
450
478
|
# Split the query into several subqueries for API calls
|
|
@@ -454,37 +482,30 @@ class PxApi:
|
|
|
454
482
|
value_codes, self._client.max_data_cells
|
|
455
483
|
)
|
|
456
484
|
]
|
|
457
|
-
|
|
458
485
|
dataset = []
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
)
|
|
470
|
-
|
|
471
|
-
):
|
|
472
|
-
dataset.extend(result)
|
|
473
|
-
|
|
486
|
+
if self.max_workers == 1:
|
|
487
|
+
# 1 worker = sequential on main thread
|
|
488
|
+
for subquery in subqueries:
|
|
489
|
+
dataset.extend(fetch(subquery))
|
|
490
|
+
else:
|
|
491
|
+
# Use threading for the subqueries
|
|
492
|
+
with ThreadPoolExecutor(
|
|
493
|
+
max_workers=self.max_workers
|
|
494
|
+
) as executor:
|
|
495
|
+
# Map() so that we yield results in order
|
|
496
|
+
for result in executor.map(fetch, subqueries):
|
|
497
|
+
dataset.extend(result)
|
|
474
498
|
else:
|
|
475
499
|
# No batching needed so we just go ahead with the query as is
|
|
476
500
|
query = build_query(value_codes, code_list)
|
|
477
|
-
|
|
478
|
-
endpoint=f"/tables/{table_id}/data",
|
|
479
|
-
query=query,
|
|
480
|
-
)
|
|
481
|
-
dataset = unpack_table_data(response, show=show)
|
|
501
|
+
dataset = fetch(query)
|
|
482
502
|
|
|
483
503
|
return dataset
|
|
484
504
|
|
|
485
505
|
def get_table_data_all(
|
|
486
506
|
self,
|
|
487
507
|
table_id: str,
|
|
508
|
+
show: Literal["code", "value", "code_value"] | None = None,
|
|
488
509
|
) -> list[dict]:
|
|
489
510
|
"""
|
|
490
511
|
Get table data that can be used with dataframes like `polars` or `pandas`. The query is constructed from the metadata.
|
|
@@ -494,6 +515,8 @@ class PxApi:
|
|
|
494
515
|
----------
|
|
495
516
|
table_id: str
|
|
496
517
|
An ID of a table to get data from.
|
|
518
|
+
show: str, optional
|
|
519
|
+
Set to "code_value", "code" or "value", to specify what to show in the categorical columns.
|
|
497
520
|
|
|
498
521
|
Returns
|
|
499
522
|
-------
|
|
@@ -504,8 +527,7 @@ class PxApi:
|
|
|
504
527
|
k: ["*"] for k in self.get_table_variables(table_id).keys()
|
|
505
528
|
}
|
|
506
529
|
return self.get_table_data(
|
|
507
|
-
table_id,
|
|
508
|
-
value_codes=selection_all,
|
|
530
|
+
table_id, value_codes=selection_all, show=show
|
|
509
531
|
)
|
|
510
532
|
|
|
511
533
|
def all_tables(self) -> list[dict[str, str]]:
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import time as real_time
|
|
2
|
+
from unittest.mock import MagicMock, patch
|
|
3
|
+
|
|
1
4
|
import pytest
|
|
2
|
-
from unittest.mock import patch, MagicMock
|
|
3
5
|
from requests import Response
|
|
4
6
|
from requests.exceptions import HTTPError
|
|
7
|
+
|
|
5
8
|
from pxweb._internal.client import Client
|
|
6
|
-
import time as real_time
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
def make_response(status_code=200, json_data=None, headers=None):
|