ssb-klass-python 1.0.0__tar.gz → 1.0.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.
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/PKG-INFO +3 -6
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/pyproject.toml +42 -38
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/classification.py +77 -69
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/codes.py +30 -29
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/correspondence.py +61 -26
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/family.py +10 -8
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/search.py +25 -22
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/variant.py +33 -32
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/version.py +70 -41
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/requests/klass_requests.py +69 -59
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/requests/klass_types.py +15 -4
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/requests/validate.py +23 -21
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/utility/classification.py +1 -1
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/utility/codes.py +1 -1
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/LICENSE +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/README.md +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/__init__.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/classes/__init__.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/config.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/py.typed +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/requests/__init__.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/requests/sections.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/utility/cleanup.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/utility/naming.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/widgets/__init__.py +0 -0
- {ssb_klass_python-1.0.0 → ssb_klass_python-1.0.2}/src/klass/widgets/search_ipywidget.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: ssb-klass-python
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.2
|
|
4
4
|
Summary: A Python package built on top of KLASS's API for retrieving classifications, codes, correspondences etc.
|
|
5
5
|
License: MIT
|
|
6
6
|
Author: Carl Corneil, ssb-pythonistas
|
|
@@ -13,16 +13,13 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.12
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
-
Requires-Dist:
|
|
16
|
+
Requires-Dist: ipython (<9)
|
|
17
17
|
Requires-Dist: ipywidgets (>=8.0.6)
|
|
18
18
|
Requires-Dist: pandas (>=1.5.3)
|
|
19
|
-
Requires-Dist: pandas-stubs (>=2.1.1.230928)
|
|
20
19
|
Requires-Dist: python-dateutil (>=2.8.2)
|
|
21
20
|
Requires-Dist: requests (>=2.31.0)
|
|
22
21
|
Requires-Dist: toml (>=0.10.2)
|
|
23
|
-
Requires-Dist:
|
|
24
|
-
Requires-Dist: types-requests (>=2.31.0.10)
|
|
25
|
-
Requires-Dist: types-toml (>=0.10.8.7)
|
|
22
|
+
Requires-Dist: typing-extensions (>=4.12.2)
|
|
26
23
|
Project-URL: Changelog, https://github.com/statisticsnorway/ssb-klass-python/releases
|
|
27
24
|
Project-URL: Documentation, https://statisticsnorway.github.io/ssb-klass-python
|
|
28
25
|
Project-URL: Homepage, https://github.com/statisticsnorway/ssb-klass-python
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "ssb-klass-python"
|
|
3
|
-
version = "1.0.
|
|
3
|
+
version = "1.0.2"
|
|
4
4
|
description = "A Python package built on top of KLASS's API for retrieving classifications, codes, correspondences etc."
|
|
5
5
|
authors = ["Carl Corneil, ssb-pythonistas <ssb-pythonistas@ssb.no>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -16,17 +16,13 @@ Changelog = "https://github.com/statisticsnorway/ssb-klass-python/releases"
|
|
|
16
16
|
|
|
17
17
|
[tool.poetry.dependencies]
|
|
18
18
|
python = ">=3.10,<4.0"
|
|
19
|
-
dapla-toolbelt = ">=2.0.6"
|
|
20
19
|
python-dateutil = ">=2.8.2"
|
|
21
20
|
toml = ">=0.10.2"
|
|
22
21
|
ipywidgets = ">=8.0.6"
|
|
23
22
|
pandas = ">=1.5.3"
|
|
24
23
|
requests = ">=2.31.0"
|
|
25
|
-
#
|
|
26
|
-
|
|
27
|
-
types-requests = ">=2.31.0.10"
|
|
28
|
-
types-python-dateutil = ">=2.8.19.14"
|
|
29
|
-
types-toml = ">=0.10.8.7"
|
|
24
|
+
ipython = "<9" # IPython v9 removes Python 3.10 support
|
|
25
|
+
typing-extensions = ">=4.12.2"
|
|
30
26
|
|
|
31
27
|
[tool.poetry.group.dev.dependencies]
|
|
32
28
|
pygments = ">=2.10.0"
|
|
@@ -51,6 +47,12 @@ sphinx-click = ">=3.0.2"
|
|
|
51
47
|
typeguard = ">=2.13.3"
|
|
52
48
|
xdoctest = { extras = ["colors"], version = ">=0.15.10" }
|
|
53
49
|
myst-parser = { version = ">=0.16.1" }
|
|
50
|
+
deptry = ">=0.23.0"
|
|
51
|
+
# Stubs for Mypy
|
|
52
|
+
pandas-stubs = ">=2.1.1.230928"
|
|
53
|
+
types-requests = ">=2.31.0.10"
|
|
54
|
+
types-python-dateutil = ">=2.8.19.14"
|
|
55
|
+
types-toml = ">=0.10.8.7"
|
|
54
56
|
|
|
55
57
|
[tool.pytest.ini_options]
|
|
56
58
|
pythonpath = ["src"]
|
|
@@ -79,17 +81,14 @@ exclude = ["tests", "docs", "noxfile.py"]
|
|
|
79
81
|
explicit_package_bases = true
|
|
80
82
|
|
|
81
83
|
[[tool.mypy.overrides]]
|
|
82
|
-
module = [
|
|
83
|
-
"ipywidgets.*",
|
|
84
|
-
"argcomplete.*",
|
|
85
|
-
]
|
|
84
|
+
module = ["ipywidgets.*", "argcomplete.*"]
|
|
86
85
|
ignore_missing_imports = true
|
|
87
86
|
|
|
88
87
|
[tool.ruff]
|
|
89
|
-
force-exclude = true
|
|
88
|
+
force-exclude = true # Apply excludes to pre-commit
|
|
90
89
|
show-fixes = true
|
|
91
90
|
src = ["src", "tests"]
|
|
92
|
-
target-version = "py310"
|
|
91
|
+
target-version = "py310" # Minimum Python version supported
|
|
93
92
|
|
|
94
93
|
include = ["*.py", "*.pyi", "**/pyproject.toml", "*.ipynb"]
|
|
95
94
|
extend-exclude = [
|
|
@@ -103,23 +102,23 @@ extend-exclude = [
|
|
|
103
102
|
# Ruff rules may be customized as desired: https://docs.astral.sh/ruff/rules/
|
|
104
103
|
[tool.ruff.lint]
|
|
105
104
|
select = [
|
|
106
|
-
"A",
|
|
107
|
-
"ANN",
|
|
108
|
-
"B",
|
|
109
|
-
"D",
|
|
110
|
-
"E",
|
|
111
|
-
"F",
|
|
112
|
-
"ISC",
|
|
113
|
-
"I",
|
|
114
|
-
"UP",
|
|
115
|
-
"RUF",
|
|
105
|
+
"A", # prevent using keywords that clobber python builtins
|
|
106
|
+
"ANN", # check type annotations
|
|
107
|
+
"B", # bugbear: security warnings
|
|
108
|
+
"D", # documentation
|
|
109
|
+
"E", # pycodestyle
|
|
110
|
+
"F", # pyflakes
|
|
111
|
+
"ISC", # implicit string concatenation
|
|
112
|
+
"I", # sort imports
|
|
113
|
+
"UP", # alert you when better syntax is available in your python version
|
|
114
|
+
"RUF", # the ruff developer's own rules
|
|
116
115
|
]
|
|
117
116
|
ignore = [
|
|
118
117
|
"ANN202", # Don't requiere return type annotation for private functions.
|
|
119
118
|
"ANN401", # Allow type annotation with type Any.
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
119
|
+
"D100", # Supress undocumented-public-module. Only doc of public api required.
|
|
120
|
+
"E402", # Supress module-import-not-at-top-of-file, needed in jupyter notebooks.
|
|
121
|
+
"E501", # Supress line-too-long warnings: trust black's judgement on this one.
|
|
123
122
|
]
|
|
124
123
|
|
|
125
124
|
[tool.ruff.lint.isort]
|
|
@@ -129,29 +128,34 @@ force-single-line = true
|
|
|
129
128
|
max-complexity = 15
|
|
130
129
|
|
|
131
130
|
[tool.ruff.lint.pydocstyle]
|
|
132
|
-
convention = "google"
|
|
131
|
+
convention = "google" # You can also use "numpy".
|
|
133
132
|
|
|
134
133
|
[tool.ruff.lint.pep8-naming]
|
|
135
|
-
classmethod-decorators = [
|
|
134
|
+
classmethod-decorators = [
|
|
135
|
+
"classmethod",
|
|
136
|
+
"validator",
|
|
137
|
+
"root_validator",
|
|
138
|
+
"pydantic.validator",
|
|
139
|
+
]
|
|
136
140
|
|
|
137
141
|
[tool.ruff.lint.per-file-ignores]
|
|
138
142
|
"*/__init__.py" = ["F401"]
|
|
139
143
|
"**/tests/*" = [
|
|
140
|
-
"ANN001",
|
|
141
|
-
"ANN002",
|
|
142
|
-
"ANN003",
|
|
143
|
-
"ANN201",
|
|
144
|
-
"ANN204",
|
|
145
|
-
"ANN205",
|
|
146
|
-
"ANN206",
|
|
147
|
-
"D100",
|
|
144
|
+
"ANN001", # type annotations don't add value for test functions
|
|
145
|
+
"ANN002", # type annotations don't add value for test functions
|
|
146
|
+
"ANN003", # type annotations don't add value for test functions
|
|
147
|
+
"ANN201", # type annotations don't add value for test functions
|
|
148
|
+
"ANN204", # type annotations don't add value for test functions
|
|
149
|
+
"ANN205", # type annotations don't add value for test functions
|
|
150
|
+
"ANN206", # type annotations don't add value for test functions
|
|
151
|
+
"D100", # docstrings are overkill for test functions
|
|
148
152
|
"D101",
|
|
149
153
|
"D102",
|
|
150
154
|
"D103",
|
|
151
155
|
"D100",
|
|
152
|
-
"S101",
|
|
156
|
+
"S101", # asserts are encouraged in pytest
|
|
153
157
|
]
|
|
154
|
-
"demo/*.py" = ["B018"]
|
|
158
|
+
"demo/*.py" = ["B018"] # Ignore the useless expressions in the notebooks
|
|
155
159
|
|
|
156
160
|
[build-system]
|
|
157
161
|
requires = ["poetry-core>=1.0.0"]
|
|
@@ -1,14 +1,19 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
|
|
1
3
|
import pandas as pd
|
|
4
|
+
from typing_extensions import Self
|
|
2
5
|
|
|
3
|
-
from
|
|
4
|
-
from
|
|
5
|
-
from
|
|
6
|
-
from
|
|
7
|
-
from
|
|
8
|
-
from
|
|
9
|
-
from
|
|
10
|
-
from
|
|
11
|
-
from
|
|
6
|
+
from ..requests.klass_requests import changes
|
|
7
|
+
from ..requests.klass_requests import classification_by_id
|
|
8
|
+
from ..requests.klass_types import ClassificationsByIdType
|
|
9
|
+
from ..requests.klass_types import Language
|
|
10
|
+
from ..requests.klass_types import OptionalLanguage
|
|
11
|
+
from ..requests.klass_types import VersionPartType
|
|
12
|
+
from .codes import KlassCodes
|
|
13
|
+
from .correspondence import KlassCorrespondence
|
|
14
|
+
from .variant import KlassVariant
|
|
15
|
+
from .variant import KlassVariantSearchByName
|
|
16
|
+
from .version import KlassVersion
|
|
12
17
|
|
|
13
18
|
|
|
14
19
|
class KlassClassification:
|
|
@@ -48,9 +53,9 @@ class KlassClassification:
|
|
|
48
53
|
_links (dict): A dictionary containing the links to different possible endpoints using the classification.
|
|
49
54
|
|
|
50
55
|
Args:
|
|
51
|
-
classification_id
|
|
52
|
-
language
|
|
53
|
-
include_future
|
|
56
|
+
classification_id: The classification_id of the classification. For example: '36'
|
|
57
|
+
language: The language of the classification. "nb", "nn" or "en".
|
|
58
|
+
include_future: Whether to include future versions of the classification.
|
|
54
59
|
|
|
55
60
|
Raises:
|
|
56
61
|
ValueError: If the language is not "no", "nb" or "en".
|
|
@@ -58,11 +63,14 @@ class KlassClassification:
|
|
|
58
63
|
"""
|
|
59
64
|
|
|
60
65
|
def __init__(
|
|
61
|
-
self
|
|
66
|
+
self: Self,
|
|
67
|
+
classification_id: str | int,
|
|
68
|
+
language: Language = "nb",
|
|
69
|
+
include_future: bool = False,
|
|
62
70
|
) -> None:
|
|
63
71
|
"""Get the data for the classification from the API."""
|
|
64
72
|
self.classification_id = classification_id
|
|
65
|
-
self.language = language
|
|
73
|
+
self.language: Language = language
|
|
66
74
|
self.include_future = include_future
|
|
67
75
|
result: ClassificationsByIdType = classification_by_id(
|
|
68
76
|
classification_id, language=language, include_future=include_future
|
|
@@ -71,7 +79,7 @@ class KlassClassification:
|
|
|
71
79
|
self.classificationType: str = result.get("classificationType", "")
|
|
72
80
|
self.lastModified: str = result.get("lastModified", "")
|
|
73
81
|
self.description: str = result.get("description", "")
|
|
74
|
-
self.primaryLanguage:
|
|
82
|
+
self.primaryLanguage: Language | Literal[""] = result.get("primaryLanguage", "")
|
|
75
83
|
self.copyrighted: bool = result.get("copyrighted", True)
|
|
76
84
|
self.includeShortName: bool = result.get("includeShortName", False)
|
|
77
85
|
self.includeNotes: bool = result.get("includeNotes", False)
|
|
@@ -117,9 +125,9 @@ class KlassClassification:
|
|
|
117
125
|
|
|
118
126
|
def get_version(
|
|
119
127
|
self,
|
|
120
|
-
version_id: int =
|
|
121
|
-
select_level: int =
|
|
122
|
-
language:
|
|
128
|
+
version_id: int | None = None,
|
|
129
|
+
select_level: int | None = None,
|
|
130
|
+
language: OptionalLanguage = None,
|
|
123
131
|
include_future: bool | None = None,
|
|
124
132
|
) -> KlassVersion:
|
|
125
133
|
"""Return a KlassVersion object of the classification based on ID.
|
|
@@ -128,10 +136,10 @@ class KlassClassification:
|
|
|
128
136
|
If no ID is specified, will get the first version under the attribute .versions on this class.
|
|
129
137
|
|
|
130
138
|
Args:
|
|
131
|
-
version_id
|
|
132
|
-
select_level
|
|
133
|
-
language
|
|
134
|
-
include_future
|
|
139
|
+
version_id: The version ID of the version.
|
|
140
|
+
select_level: The level of the version to keep in the data.
|
|
141
|
+
language: The language of the version. "nn", "nb" or "en".
|
|
142
|
+
include_future: Whether to include future versions of the version.
|
|
135
143
|
|
|
136
144
|
Returns:
|
|
137
145
|
KlassVersion: A KlassVersion object of the specified ID.
|
|
@@ -140,7 +148,7 @@ class KlassClassification:
|
|
|
140
148
|
version_id = sorted(self.versions, key=lambda x: x["validFrom"])[-1][
|
|
141
149
|
"version_id"
|
|
142
150
|
]
|
|
143
|
-
if language
|
|
151
|
+
if not language:
|
|
144
152
|
language = self.language
|
|
145
153
|
if include_future is None:
|
|
146
154
|
include_future = self.include_future
|
|
@@ -163,11 +171,11 @@ class KlassClassification:
|
|
|
163
171
|
self,
|
|
164
172
|
name: str,
|
|
165
173
|
from_date: str,
|
|
166
|
-
to_date: str =
|
|
167
|
-
select_codes: str =
|
|
168
|
-
select_level: int =
|
|
169
|
-
presentation_name_pattern: str =
|
|
170
|
-
language:
|
|
174
|
+
to_date: str | None = None,
|
|
175
|
+
select_codes: str | None = None,
|
|
176
|
+
select_level: int | None = None,
|
|
177
|
+
presentation_name_pattern: str | None = None,
|
|
178
|
+
language: Language = "nb",
|
|
171
179
|
include_future: bool = False,
|
|
172
180
|
) -> KlassVariantSearchByName:
|
|
173
181
|
"""Get a KlassVariant by searching for its name under the Variants owned by the current classification.
|
|
@@ -177,16 +185,16 @@ class KlassClassification:
|
|
|
177
185
|
redefine upper levels for some lower-level codes.
|
|
178
186
|
|
|
179
187
|
Args:
|
|
180
|
-
name
|
|
181
|
-
from_date
|
|
182
|
-
to_date
|
|
183
|
-
select_codes
|
|
188
|
+
name: The start of the name of the variant.
|
|
189
|
+
from_date: The start date of the time period. "YYYY-MM-DD".
|
|
190
|
+
to_date: The end date of the time period. "YYYY-MM-DD".
|
|
191
|
+
select_codes: Limit the result to codes matching this pattern.
|
|
184
192
|
See rules: https://data.ssb.no/api/klass/v1/api-guide.html#_selectcodes.
|
|
185
|
-
select_level
|
|
186
|
-
presentation_name_pattern
|
|
193
|
+
select_level: The level of the version to keep in the data.
|
|
194
|
+
presentation_name_pattern: Used to build an alternative presentation name for the codes.
|
|
187
195
|
See rules: https://data.ssb.no/api/klass/v1/api-guide.html#_presentationnamepattern.
|
|
188
|
-
language
|
|
189
|
-
include_future
|
|
196
|
+
language: The language of the version. "nn", "nb" or "en".
|
|
197
|
+
include_future: Whether to include future versions of the version.
|
|
190
198
|
|
|
191
199
|
Returns:
|
|
192
200
|
KlassVariantSearchByName: A KlassVariantSearchByName object based on the classification's ID
|
|
@@ -206,10 +214,10 @@ class KlassClassification:
|
|
|
206
214
|
|
|
207
215
|
def get_correspondence_to(
|
|
208
216
|
self,
|
|
209
|
-
target_classification_id: str,
|
|
217
|
+
target_classification_id: str | int,
|
|
210
218
|
from_date: str,
|
|
211
|
-
to_date: str =
|
|
212
|
-
language:
|
|
219
|
+
to_date: str | None = None,
|
|
220
|
+
language: OptionalLanguage = None,
|
|
213
221
|
include_future: bool | None = None,
|
|
214
222
|
) -> KlassCorrespondence:
|
|
215
223
|
"""Treats the current classification as a source of correspondences, specifying the target's ID and a date.
|
|
@@ -217,17 +225,17 @@ class KlassClassification:
|
|
|
217
225
|
Returns a KlassCorrespondence object of the correspondences.
|
|
218
226
|
|
|
219
227
|
Args:
|
|
220
|
-
target_classification_id
|
|
221
|
-
from_date
|
|
222
|
-
to_date
|
|
223
|
-
language
|
|
224
|
-
include_future
|
|
228
|
+
target_classification_id: The classification ID of the target classification.
|
|
229
|
+
from_date: The start date of the time period. "YYYY-MM-DD".
|
|
230
|
+
to_date: The end date of the time period. "YYYY-MM-DD".
|
|
231
|
+
language: The language of the correspondences. "nn", "nb" or "en".
|
|
232
|
+
include_future: Whether to include future correspondences.
|
|
225
233
|
|
|
226
234
|
Returns:
|
|
227
235
|
KlassCorrespondence: A KlassCorrespondence object of the correspondences
|
|
228
236
|
between the current classification and the target classification.
|
|
229
237
|
"""
|
|
230
|
-
if language
|
|
238
|
+
if not language:
|
|
231
239
|
language = self.language
|
|
232
240
|
if include_future is None:
|
|
233
241
|
include_future = self.include_future
|
|
@@ -242,32 +250,32 @@ class KlassClassification:
|
|
|
242
250
|
|
|
243
251
|
def get_codes(
|
|
244
252
|
self,
|
|
245
|
-
from_date: str =
|
|
246
|
-
to_date: str =
|
|
247
|
-
select_codes: str =
|
|
248
|
-
select_level: int =
|
|
249
|
-
presentation_name_pattern: str =
|
|
250
|
-
language:
|
|
253
|
+
from_date: str | None = None,
|
|
254
|
+
to_date: str | None = None,
|
|
255
|
+
select_codes: str | None = None,
|
|
256
|
+
select_level: int | None = None,
|
|
257
|
+
presentation_name_pattern: str | None = None,
|
|
258
|
+
language: OptionalLanguage = None,
|
|
251
259
|
include_future: bool | None = None,
|
|
252
260
|
) -> KlassCodes:
|
|
253
261
|
"""Return a KlassCodes object of the classification at a specific time or in a specific time range.
|
|
254
262
|
|
|
255
263
|
Args:
|
|
256
|
-
from_date
|
|
257
|
-
to_date
|
|
258
|
-
select_codes
|
|
264
|
+
from_date: The start date of the time period. "YYYY-MM-DD".
|
|
265
|
+
to_date: The end date of the time period. "YYYY-MM-DD".
|
|
266
|
+
select_codes: Limit the result to codes matching this pattern.
|
|
259
267
|
See rules: https://data.ssb.no/api/klass/v1/api-guide.html#_selectcodes.
|
|
260
|
-
select_level
|
|
261
|
-
presentation_name_pattern
|
|
268
|
+
select_level: The level of the version to keep in the data.
|
|
269
|
+
presentation_name_pattern: Used to build an alternative presentation name for the codes.
|
|
262
270
|
See rules: https://data.ssb.no/api/klass/v1/api-guide.html#_presentationnamepattern.
|
|
263
|
-
language
|
|
264
|
-
include_future
|
|
271
|
+
language: The language of the version. "nn", "nb" or "en".
|
|
272
|
+
include_future: Whether to include future versions of the version.
|
|
265
273
|
|
|
266
274
|
Returns:
|
|
267
275
|
KlassCodes: A KlassCodes object of the classification at a specific time or in a specific time range.
|
|
268
276
|
"""
|
|
269
277
|
# If not passed to method, grab these from the Classification
|
|
270
|
-
if language
|
|
278
|
+
if not language:
|
|
271
279
|
language = self.language
|
|
272
280
|
if include_future is None:
|
|
273
281
|
include_future = self.include_future
|
|
@@ -286,8 +294,8 @@ class KlassClassification:
|
|
|
286
294
|
def get_changes(
|
|
287
295
|
self,
|
|
288
296
|
from_date: str,
|
|
289
|
-
to_date: str =
|
|
290
|
-
language:
|
|
297
|
+
to_date: str | None = None,
|
|
298
|
+
language: OptionalLanguage = "nb",
|
|
291
299
|
include_future: bool = False,
|
|
292
300
|
) -> pd.DataFrame:
|
|
293
301
|
"""Return a dataframe of the classification at a specific time or in a specific time range.
|
|
@@ -296,10 +304,10 @@ class KlassClassification:
|
|
|
296
304
|
but only what has changed since the last update or within the time range.
|
|
297
305
|
|
|
298
306
|
Args:
|
|
299
|
-
from_date
|
|
300
|
-
to_date
|
|
301
|
-
language
|
|
302
|
-
include_future
|
|
307
|
+
from_date: The start date of the time period. "YYYY-MM-DD".
|
|
308
|
+
to_date: The end date of the time period. "YYYY-MM-DD".
|
|
309
|
+
language: The language of the version. "nn", "nb" or "en".
|
|
310
|
+
include_future: Whether to include future versions of the version.
|
|
303
311
|
|
|
304
312
|
Returns:
|
|
305
313
|
pd.DataFrame: A pandas DataFrame of the changes in the classification at a specific time
|
|
@@ -313,7 +321,7 @@ class KlassClassification:
|
|
|
313
321
|
include_future=include_future,
|
|
314
322
|
)
|
|
315
323
|
|
|
316
|
-
def get_latest_variant_by_name(self, variant_name: str) -> KlassVariant
|
|
324
|
+
def get_latest_variant_by_name(self, variant_name: str) -> KlassVariant:
|
|
317
325
|
"""Attempt to get a single variant from the classification using a search string.
|
|
318
326
|
|
|
319
327
|
Args:
|
|
@@ -323,7 +331,7 @@ class KlassClassification:
|
|
|
323
331
|
ValueError: If the string is not specific enough, and zero, or more than a single variant is found.
|
|
324
332
|
|
|
325
333
|
Returns:
|
|
326
|
-
KlassVariant
|
|
334
|
+
KlassVariant: The single variant we found with the search string. Raises if we found no matches.
|
|
327
335
|
"""
|
|
328
336
|
version = self.get_version()
|
|
329
337
|
variants = version.variants_simple()
|
|
@@ -346,7 +354,7 @@ class KlassClassification:
|
|
|
346
354
|
|
|
347
355
|
def join_all_variants_correspondences_on_data(
|
|
348
356
|
self,
|
|
349
|
-
version_id: int =
|
|
357
|
+
version_id: int | None = None,
|
|
350
358
|
shortname_len: int = 3,
|
|
351
359
|
data_left: pd.DataFrame | None = None,
|
|
352
360
|
code_col_name: str = "code",
|
|
@@ -357,7 +365,7 @@ class KlassClassification:
|
|
|
357
365
|
Can be quite slow, as it is doing a request to the KLASS-API for every variant and Correspondence.
|
|
358
366
|
|
|
359
367
|
Args:
|
|
360
|
-
version_id: If you want, specify the ID of the version. If
|
|
368
|
+
version_id: If you want, specify the ID of the version. If None, will get the "latest" version for the classification.
|
|
361
369
|
shortname_len: Amount of words from the correspondences that the new column names will be constructed from.
|
|
362
370
|
data_left: A dataframe containing a column to join all the correspondences on. If None will get data from the version.
|
|
363
371
|
code_col_name: The column in the data to join the code on.
|
|
@@ -4,8 +4,9 @@ from datetime import datetime
|
|
|
4
4
|
import pandas as pd
|
|
5
5
|
from typing_extensions import Self
|
|
6
6
|
|
|
7
|
-
from
|
|
8
|
-
from
|
|
7
|
+
from ..requests.klass_requests import codes
|
|
8
|
+
from ..requests.klass_requests import codes_at
|
|
9
|
+
from ..requests.klass_types import Language
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class KlassCodes:
|
|
@@ -20,14 +21,14 @@ class KlassCodes:
|
|
|
20
21
|
to_date (str): The end date of the time period. "YYYY-MM-DD".
|
|
21
22
|
|
|
22
23
|
Args:
|
|
23
|
-
classification_id
|
|
24
|
-
from_date
|
|
25
|
-
to_date
|
|
26
|
-
select_codes
|
|
27
|
-
select_level
|
|
28
|
-
presentation_name_pattern
|
|
29
|
-
language
|
|
30
|
-
include_future
|
|
24
|
+
classification_id: The classification ID.
|
|
25
|
+
from_date: The start date of the time period. "YYYY-MM-DD".
|
|
26
|
+
to_date: The end date of the time period. "YYYY-MM-DD".
|
|
27
|
+
select_codes: A list of codes to be selected.
|
|
28
|
+
select_level: The level to keep in the data.
|
|
29
|
+
presentation_name_pattern: A pattern for filtering the code names.
|
|
30
|
+
language: The language of the code names. Defaults to "nb".
|
|
31
|
+
include_future: Whether to include future codes. Defaults to False.
|
|
31
32
|
|
|
32
33
|
Raises:
|
|
33
34
|
ValueError: If from_date or to_date is not a valid date or date-string YYYY-MM-DD.
|
|
@@ -40,13 +41,13 @@ class KlassCodes:
|
|
|
40
41
|
|
|
41
42
|
def __init__(
|
|
42
43
|
self,
|
|
43
|
-
classification_id: str
|
|
44
|
-
from_date: str =
|
|
45
|
-
to_date: str =
|
|
46
|
-
select_codes: str =
|
|
47
|
-
select_level: int =
|
|
48
|
-
presentation_name_pattern: str =
|
|
49
|
-
language:
|
|
44
|
+
classification_id: str | int,
|
|
45
|
+
from_date: str | None = None,
|
|
46
|
+
to_date: str | None = None,
|
|
47
|
+
select_codes: str | None = None,
|
|
48
|
+
select_level: int | None = None,
|
|
49
|
+
presentation_name_pattern: str | None = None,
|
|
50
|
+
language: Language = "nb",
|
|
50
51
|
include_future: bool = False,
|
|
51
52
|
) -> None:
|
|
52
53
|
"""Get the data from the KLASS-api belonging to the code-list."""
|
|
@@ -58,7 +59,7 @@ class KlassCodes:
|
|
|
58
59
|
self.select_codes = select_codes
|
|
59
60
|
self.select_level = select_level
|
|
60
61
|
self.presentation_name_pattern = presentation_name_pattern
|
|
61
|
-
self.language = language
|
|
62
|
+
self.language: Language = language
|
|
62
63
|
self.include_future = include_future
|
|
63
64
|
self.get_codes()
|
|
64
65
|
|
|
@@ -97,16 +98,16 @@ class KlassCodes:
|
|
|
97
98
|
|
|
98
99
|
def change_dates(
|
|
99
100
|
self,
|
|
100
|
-
from_date: str =
|
|
101
|
+
from_date: str | None = None,
|
|
101
102
|
to_date: str = "",
|
|
102
103
|
include_future: bool | None = None,
|
|
103
104
|
) -> Self:
|
|
104
105
|
"""Change the dates of the codelist and get the data again based on new dates.
|
|
105
106
|
|
|
106
107
|
Args:
|
|
107
|
-
from_date
|
|
108
|
-
to_date
|
|
109
|
-
include_future
|
|
108
|
+
from_date: The start date of the time period. "YYYY-MM-DD".
|
|
109
|
+
to_date: The end date of the time period. "YYYY-MM-DD".
|
|
110
|
+
include_future: Whether to include future codes.
|
|
110
111
|
|
|
111
112
|
Returns:
|
|
112
113
|
self (KlassSearchFamilies): Returns self to make the method more easily chainable.
|
|
@@ -128,7 +129,7 @@ class KlassCodes:
|
|
|
128
129
|
the date specified by self.from_date.
|
|
129
130
|
|
|
130
131
|
Args:
|
|
131
|
-
raise_on_empty_data
|
|
132
|
+
raise_on_empty_data: Whether to raise an error if the returned dataframe is empty. Defaults to True.
|
|
132
133
|
|
|
133
134
|
Returns:
|
|
134
135
|
self (KlassSearchFamilies): Returns self to make the method more easily chainable.
|
|
@@ -166,17 +167,17 @@ class KlassCodes:
|
|
|
166
167
|
def to_dict(
|
|
167
168
|
self,
|
|
168
169
|
key: str = "code",
|
|
169
|
-
value: str =
|
|
170
|
-
other: str =
|
|
170
|
+
value: str | None = None, # default is "name" if not set
|
|
171
|
+
other: str | None = None,
|
|
171
172
|
) -> dict[str, str] | defaultdict[str, str]:
|
|
172
173
|
"""Extract two columns from the data, turning them into a dict.
|
|
173
174
|
|
|
174
175
|
If you specify a value for "other", returns a defaultdict instead.
|
|
175
176
|
|
|
176
177
|
Args:
|
|
177
|
-
key
|
|
178
|
-
value
|
|
179
|
-
other
|
|
178
|
+
key: The name of the column with the values you want as keys.
|
|
179
|
+
value: The name of the column with the values you want as values in your dict.
|
|
180
|
+
other: If key is missing from dict, return this value instead, if you specify an OTHER-value.
|
|
180
181
|
|
|
181
182
|
Returns:
|
|
182
183
|
dict | defaultdict: The extracted columns as a dict or defaultdict.
|
|
@@ -201,7 +202,7 @@ class KlassCodes:
|
|
|
201
202
|
First envisioned by @mfmssb
|
|
202
203
|
|
|
203
204
|
Args:
|
|
204
|
-
keep
|
|
205
|
+
keep: The start of the names of the columns you want to keep when done.
|
|
205
206
|
Default is ["code", "name"], but other possibilities are "presentationName",
|
|
206
207
|
"level", "shortName", "validTo", "validFrom", and "notes".
|
|
207
208
|
|