pxwebpy 0.3.0__tar.gz → 0.3.2__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.
Files changed (28) hide show
  1. pxwebpy-0.3.2/.github/workflows/ci.yml +62 -0
  2. pxwebpy-0.3.2/.github/workflows/release.yml +31 -0
  3. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/PKG-INFO +7 -5
  4. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/README.md +2 -2
  5. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/pyproject.toml +6 -11
  6. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/src/pxwebpy/table.py +72 -22
  7. pxwebpy-0.3.2/uv.lock +945 -0
  8. pxwebpy-0.3.0/.github/workflows/ci.yml +0 -39
  9. pxwebpy-0.3.0/.github/workflows/release.yml +0 -28
  10. pxwebpy-0.3.0/.python-version +0 -1
  11. pxwebpy-0.3.0/requirements-dev.lock +0 -107
  12. pxwebpy-0.3.0/requirements.lock +0 -21
  13. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/.gitignore +0 -0
  14. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/LICENSE +0 -0
  15. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/examples/example.py +0 -0
  16. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/examples/example_query.json +0 -0
  17. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/src/pxwebpy/__init__.py +0 -0
  18. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/invalid_query.json +0 -0
  19. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/mock/response_fi.json +0 -0
  20. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/mock/response_gl.json +0 -0
  21. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/mock/response_no.json +0 -0
  22. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/mock/response_table_variables.json +0 -0
  23. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/mock/result_fi.json +0 -0
  24. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/mock/result_gl.json +0 -0
  25. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/mock/result_no.json +0 -0
  26. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/queries.json +0 -0
  27. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/test_table.py +0 -0
  28. {pxwebpy-0.3.0 → pxwebpy-0.3.2}/tests/valid_query.json +0 -0
@@ -0,0 +1,62 @@
1
+ name: ci
2
+
3
+ env:
4
+ UV_VERSION: "0.4.9"
5
+ UV_CACHE_DIR: /tmp/.uv-cache
6
+ UV_LINK_MODE: copy
7
+
8
+ on:
9
+ push:
10
+ branches: [ "main" ]
11
+ pull_request:
12
+ branches: [ "main" ]
13
+
14
+ jobs:
15
+ build:
16
+ name: python-${{ matrix.os }}
17
+
18
+ strategy:
19
+ matrix:
20
+ os:
21
+ - ubuntu-latest
22
+ - windows-latest
23
+ - macos-latest
24
+ python-version: ["3.9", "3.10", "3.11", "3.12"]
25
+ fail-fast: false
26
+
27
+ runs-on: ${{ matrix.os }}
28
+
29
+ steps:
30
+ - uses: actions/checkout@v4
31
+
32
+ - name: Set up uv ${{ env.UV_VERSION }}
33
+ if: ${{ matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest' }}
34
+ run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
35
+
36
+ - name: Set up uv ${{ env.UV_VERSION }}
37
+ if: ${{ matrix.os == 'windows-latest' }}
38
+ run: irm https://astral.sh/uv/${{ env.UV_VERSION }}/install.ps1 | iex
39
+ shell: powershell
40
+
41
+ - name: Set up Python ${{ matrix.python-version }}
42
+ run: uv python install ${{ matrix.python-version }}
43
+
44
+ - name: Restore uv cache
45
+ uses: actions/cache@v4
46
+ with:
47
+ path: ${{ env.UV_CACHE_DIR }}
48
+ key: uv-${{ runner.os }}-${{ hashFiles('uv.lock') }}
49
+ restore-keys: |
50
+ uv-${{ runner.os }}-${{ hashFiles('uv.lock') }}
51
+ uv-${{ runner.os }}
52
+
53
+ - name: Install
54
+ run: uv sync --extra ci --no-dev --locked
55
+
56
+ - name: Run tests & build
57
+ run: |
58
+ uv run pytest tests
59
+ uv build
60
+
61
+ - name: Minimize uv cache
62
+ run: uv cache prune --ci
@@ -0,0 +1,31 @@
1
+ name: release
2
+
3
+ env:
4
+ UV_VERSION: "0.4.9"
5
+ UV_LINK_MODE: copy
6
+
7
+ on:
8
+ release:
9
+ types: [published]
10
+
11
+ jobs:
12
+ build-release:
13
+
14
+ runs-on: ubuntu-latest
15
+ environment: release
16
+ permissions:
17
+ id-token: write
18
+ strategy:
19
+ fail-fast: false
20
+
21
+ steps:
22
+ - uses: actions/checkout@v4
23
+
24
+ - name: Set up uv ${{ env.UV_VERSION }}
25
+ run: curl -LsSf https://astral.sh/uv/${{ env.UV_VERSION }}/install.sh | sh
26
+
27
+ - name: Build for distribution
28
+ run: uv build
29
+
30
+ - name: Publish to PyPI
31
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -1,17 +1,19 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pxwebpy
3
- Version: 0.3.0
3
+ Version: 0.3.2
4
4
  Summary: Get data from PxWeb API easily.
5
- Project-URL: Issue tracker, https://github.com/stefur/pxwebpy/issues
5
+ Project-URL: Repository, https://github.com/stefur/pxwebpy
6
6
  Author-email: stefur <stefan@furne.net>
7
7
  License-Expression: MIT
8
8
  License-File: LICENSE
9
9
  Keywords: dataframes,pandas,polars,pxweb,statistics
10
10
  Classifier: License :: OSI Approved :: MIT License
11
11
  Classifier: Programming Language :: Python :: 3 :: Only
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
12
14
  Classifier: Programming Language :: Python :: 3.11
13
15
  Classifier: Programming Language :: Python :: 3.12
14
- Requires-Python: >=3.11
16
+ Requires-Python: >=3.9
15
17
  Requires-Dist: requests>=2.31.0
16
18
  Provides-Extra: ci
17
19
  Requires-Dist: pytest>=8.2.0; extra == 'ci'
@@ -21,8 +23,8 @@ Description-Content-Type: text/markdown
21
23
  [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/stefur/pxwebpy/ci.yml?style=flat-square&label=ci)](https://github.com/stefur/pxwebpy/actions/workflows/ci.yml)
22
24
  ![PyPI - Version](https://img.shields.io/pypi/v/pxwebpy?style=flat-square)
23
25
  ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pxwebpy?style=flat-square)
24
- [![Rye](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/mitsuhiko/rye/main/artwork/badge.json&style=flat-square)](https://rye-up.com)
25
- [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square)](https://github.com/astral-sh/ruff)
26
+ [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json&style=flat-square)](https://github.com/astral-sh/uv)
27
+ [![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square)](https://github.com/astral-sh/ruff)
26
28
  Easily get data from the PxWeb API and into either a polars or pandas dataframe.
27
29
 
28
30
  Pxwebpy parses the PxWeb table data as well as metadata using the json-stat2 response format.
@@ -2,8 +2,8 @@
2
2
  [![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/stefur/pxwebpy/ci.yml?style=flat-square&label=ci)](https://github.com/stefur/pxwebpy/actions/workflows/ci.yml)
3
3
  ![PyPI - Version](https://img.shields.io/pypi/v/pxwebpy?style=flat-square)
4
4
  ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pxwebpy?style=flat-square)
5
- [![Rye](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/mitsuhiko/rye/main/artwork/badge.json&style=flat-square)](https://rye-up.com)
6
- [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square)](https://github.com/astral-sh/ruff)
5
+ [![uv](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/uv/main/assets/badge/v0.json&style=flat-square)](https://github.com/astral-sh/uv)
6
+ [![ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square)](https://github.com/astral-sh/ruff)
7
7
  Easily get data from the PxWeb API and into either a polars or pandas dataframe.
8
8
 
9
9
  Pxwebpy parses the PxWeb table data as well as metadata using the json-stat2 response format.
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pxwebpy"
3
- version = "0.3.0"
3
+ version = "0.3.2"
4
4
  description = "Get data from PxWeb API easily."
5
5
  license = "MIT"
6
6
  repository = "https://github.com/stefur/pxwebpy/"
@@ -8,6 +8,8 @@ keywords = ["pxweb", "statistics", "dataframes", "pandas", "polars"]
8
8
  classifiers = [
9
9
  "License :: OSI Approved :: MIT License",
10
10
  "Programming Language :: Python :: 3 :: Only",
11
+ "Programming Language :: Python :: 3.9",
12
+ "Programming Language :: Python :: 3.10",
11
13
  "Programming Language :: Python :: 3.11",
12
14
  "Programming Language :: Python :: 3.12",
13
15
  ]
@@ -18,10 +20,10 @@ dependencies = [
18
20
  "requests>=2.31.0",
19
21
  ]
20
22
  readme = "README.md"
21
- requires-python = ">= 3.11"
23
+ requires-python = ">= 3.9"
22
24
 
23
25
  [project.urls]
24
- "Issue tracker" = "https://github.com/stefur/pxwebpy/issues"
26
+ Repository = "https://github.com/stefur/pxwebpy"
25
27
 
26
28
  [project.optional-dependencies]
27
29
  ci = [
@@ -32,8 +34,7 @@ ci = [
32
34
  requires = ["hatchling"]
33
35
  build-backend = "hatchling.build"
34
36
 
35
- [tool.rye]
36
- managed = true
37
+ [tool.uv]
37
38
  dev-dependencies = [
38
39
  "types-requests>=2.32.0.20240602",
39
40
  "polars>=0.20.7",
@@ -42,9 +43,3 @@ dev-dependencies = [
42
43
  "pytest>=8.2.0",
43
44
  "ipykernel>=6.29.4",
44
45
  ]
45
-
46
- [tool.hatch.metadata]
47
- allow-direct-references = true
48
-
49
- [tool.hatch.build.targets.wheel]
50
- packages = ["src/pxwebpy"]
@@ -5,6 +5,7 @@ import json
5
5
  from datetime import datetime
6
6
  from enum import Enum
7
7
  from pathlib import Path
8
+ from typing import Optional, Union
8
9
 
9
10
  import requests
10
11
 
@@ -72,13 +73,13 @@ class PxTable:
72
73
  """
73
74
 
74
75
  def __init__(self, url=None, query=None) -> None:
75
- self.url: str | None = url
76
- self.query: dict | None = query
77
- self.dataset: list[dict] | None = None
76
+ self.url: Optional[str] = url
77
+ self.query: Optional[dict] = query
78
+ self.dataset: Optional[list[dict]] = None
78
79
  self.metadata: dict = {
79
80
  key: None for key in ["label", "note", "source", "updated"]
80
81
  }
81
- self.fetched: datetime | None = None
82
+ self.fetched: Optional[datetime] = None
82
83
 
83
84
  if query:
84
85
  try:
@@ -112,25 +113,66 @@ class PxTable:
112
113
 
113
114
  self.fetched = datetime.now()
114
115
 
115
- def __unpack_data(self, response: dict) -> list[dict]:
116
+ def __unpack_data(self, json_data: dict) -> list[dict]:
116
117
  """
117
- Takes the response json-stat2 and turns it into a list of dicts that can
118
+ Takes the json-stat2 and turns it into a list of dicts that can
118
119
  be used to convert into a dataframe, using either pandas or polars.
119
120
  """
120
121
 
121
- category_labels = {
122
- response["dimension"][dim]["label"]: response["dimension"][dim]["category"][
123
- "label"
124
- ].values()
125
- for dim in response["dimension"]
126
- }
122
+ dimension_categories = {}
123
+
124
+ # Go over each dimension
125
+ for dim in json_data["dimension"]:
126
+ # If the dimension has extension data along with a key for show, use
127
+ # that to determine the values shown in the output
128
+ try:
129
+ show_value = json_data["dimension"][dim]["extension"]["show"]
130
+
131
+ if show_value == "code_value":
132
+ values = [
133
+ str(k) + " " + str(v)
134
+ for k, v in json_data["dimension"][dim]["category"][
135
+ "label"
136
+ ].items()
137
+ ]
138
+
139
+ elif show_value == "code":
140
+ values = [
141
+ str(k)
142
+ for k in json_data["dimension"][dim]["category"]["label"].keys()
143
+ ]
144
+
145
+ elif show_value == "value":
146
+ values = [
147
+ str(v)
148
+ for v in json_data["dimension"][dim]["category"][
149
+ "label"
150
+ ].values()
151
+ ]
152
+ # Raise an error if we hit some value in the show key that we don't know how to handle
153
+
154
+ else:
155
+ raise ValueError(
156
+ f"""Unexpected show value. Expected "code", "value" or "code_value", got: {show_value}"""
157
+ )
158
+
159
+ # If there's no show key at all we default to using the label values
160
+ except KeyError:
161
+ values = [
162
+ str(v)
163
+ for v in json_data["dimension"][dim]["category"]["label"].values()
164
+ ]
127
165
 
166
+ dimension_categories.update({json_data["dimension"][dim]["label"]: values})
167
+
168
+ # The result is a list of dicts with the dimension as key and product of the category labels for values
128
169
  result = [
129
- dict(zip(category_labels.keys(), x))
130
- for x in itertools.product(*category_labels.values())
170
+ dict(zip(dimension_categories.keys(), x))
171
+ for x in itertools.product(*dimension_categories.values())
131
172
  ]
132
173
 
133
- for value, dict_row in zip(response["value"], result):
174
+ # Finally add the value for each dict representing a row
175
+ for value, dict_row in zip(json_data["value"], result):
134
176
  dict_row["value"] = value
135
177
 
136
178
  return result
@@ -140,7 +182,7 @@ class PxTable:
140
182
  try:
141
183
  path = Path(query)
142
184
  return path.exists()
143
- except Exception:
185
+ except OSError:
144
186
  return False
145
187
 
146
188
  @property
@@ -151,12 +193,20 @@ class PxTable:
151
193
  return self.__query
152
194
 
153
195
  @query.setter
154
- def query(self, query: str | dict | None) -> None:
196
+ def query(
197
+ self,
198
+ query: Optional[
199
+ Union[
200
+ str,
201
+ dict,
202
+ ]
203
+ ],
204
+ ) -> None:
155
205
  """
156
206
  Set the JSON query from a string representing a path or a JSON structure that is either a string or a dict.
157
207
  """
158
208
 
159
- if not isinstance(query, str | dict | None):
209
+ if not isinstance(query, (str, dict, type(None))):
160
210
  raise TypeError(
161
211
  f"""Invalid input for `query`.
162
212
  Expected `str`, `dict` or `None`, got {type(query)!r}."""
@@ -169,7 +219,7 @@ class PxTable:
169
219
  else:
170
220
  try:
171
221
  self.__query = json.loads(query)
172
- except Exception as err:
222
+ except json.JSONDecodeError as err:
173
223
  raise ValueError(
174
224
  "Provided value could not be decoded as JSON."
175
225
  ) from err
@@ -191,9 +241,9 @@ class PxTable:
191
241
  try:
192
242
  response.raise_for_status()
193
243
  except requests.exceptions.HTTPError:
194
- raise Exception(
244
+ raise RuntimeError(
195
245
  f"Failed to {context.value}: {response.status_code}: {response.reason}"
196
- ) from None
246
+ )
197
247
 
198
248
  return response.json()
199
249
 
@@ -267,7 +317,7 @@ class PxTable:
267
317
  )
268
318
  self.query = {"query": conversion, "response": {"format": "json-stat2"}}
269
319
 
270
- def get_table_variables(self) -> dict | None:
320
+ def get_table_variables(self) -> Union[dict, None]:
271
321
  """
272
322
  Returns a dict of variables and the respective values from the table.
273
323
  """