ssb-klass-python 1.0.1__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.
Files changed (26) hide show
  1. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/PKG-INFO +1 -1
  2. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/pyproject.toml +1 -1
  3. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/classification.py +77 -69
  4. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/codes.py +30 -29
  5. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/correspondence.py +61 -26
  6. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/family.py +10 -8
  7. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/search.py +25 -22
  8. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/variant.py +33 -32
  9. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/version.py +70 -41
  10. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/requests/klass_requests.py +69 -59
  11. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/requests/klass_types.py +15 -4
  12. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/requests/validate.py +23 -21
  13. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/utility/classification.py +1 -1
  14. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/utility/codes.py +1 -1
  15. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/LICENSE +0 -0
  16. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/README.md +0 -0
  17. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/__init__.py +0 -0
  18. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/classes/__init__.py +0 -0
  19. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/config.py +0 -0
  20. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/py.typed +0 -0
  21. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/requests/__init__.py +0 -0
  22. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/requests/sections.py +0 -0
  23. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/utility/cleanup.py +0 -0
  24. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/utility/naming.py +0 -0
  25. {ssb_klass_python-1.0.1 → ssb_klass_python-1.0.2}/src/klass/widgets/__init__.py +0 -0
  26. {ssb_klass_python-1.0.1 → 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.1
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
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ssb-klass-python"
3
- version = "1.0.1"
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"
@@ -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 klass.classes.codes import KlassCodes
4
- from klass.classes.correspondence import KlassCorrespondence
5
- from klass.classes.variant import KlassVariant
6
- from klass.classes.variant import KlassVariantSearchByName
7
- from klass.classes.version import KlassVersion
8
- from klass.requests.klass_requests import changes
9
- from klass.requests.klass_requests import classification_by_id
10
- from klass.requests.klass_types import ClassificationsByIdType
11
- from klass.requests.klass_types import VersionPartType
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 (str): The classification_id of the classification. For example: '36'
52
- language (str): The language of the classification. "nb", "nn" or "en".
53
- include_future (bool): Whether to include future versions of the classification.
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, classification_id: str, language: str = "nb", include_future: bool = False
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: str = result.get("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 = 0,
121
- select_level: int = 0,
122
- language: str = "",
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 (int): The version ID of the version.
132
- select_level (int): The level of the version to keep in the data.
133
- language (str): The language of the version. "nn", "nb" or "en".
134
- include_future (bool): Whether to include future versions of the version.
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 = 0,
169
- presentation_name_pattern: str = "",
170
- language: str = "nb",
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 (str): The start of the name of the variant.
181
- from_date (str): The start date of the time period. "YYYY-MM-DD".
182
- to_date (str): The end date of the time period. "YYYY-MM-DD".
183
- select_codes (str): Limit the result to codes matching this pattern.
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 (int): The level of the version to keep in the data.
186
- presentation_name_pattern (str): Used to build an alternative presentation name for the codes.
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 (str): The language of the version. "nn", "nb" or "en".
189
- include_future (bool): Whether to include future versions of the version.
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: str = "",
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 (str): The classification ID of the target classification.
221
- from_date (str): The start date of the time period. "YYYY-MM-DD".
222
- to_date (str): The end date of the time period. "YYYY-MM-DD".
223
- language (str): The language of the correspondences. "nn", "nb" or "en".
224
- include_future (bool): Whether to include future correspondences.
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 = 0,
249
- presentation_name_pattern: str = "",
250
- language: str = "",
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 (str): The start date of the time period. "YYYY-MM-DD".
257
- to_date (str): The end date of the time period. "YYYY-MM-DD".
258
- select_codes (str): Limit the result to codes matching this pattern.
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 (int): The level of the version to keep in the data.
261
- presentation_name_pattern (str): Used to build an alternative presentation name for the codes.
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 (str): The language of the version. "nn", "nb" or "en".
264
- include_future (bool): Whether to include future versions of the version.
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: str = "nb",
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 (str): The start date of the time period. "YYYY-MM-DD".
300
- to_date (str): The end date of the time period. "YYYY-MM-DD".
301
- language (str): The language of the version. "nn", "nb" or "en".
302
- include_future (bool): Whether to include future versions of the version.
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 | None:
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 | None: The single variant we found with the search string. Or None if we found no matches.
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 = 0,
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 0, will get the "latest" version for the classification.
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 klass.requests.klass_requests import codes
8
- from klass.requests.klass_requests import codes_at
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 (str): The classification ID.
24
- from_date (str): The start date of the time period. "YYYY-MM-DD".
25
- to_date (str): The end date of the time period. "YYYY-MM-DD".
26
- select_codes (str): A list of codes to be selected.
27
- select_level (int): The level to keep in the data.
28
- presentation_name_pattern (str): A pattern for filtering the code names.
29
- language (str): The language of the code names. Defaults to "nb".
30
- include_future (bool): Whether to include future codes. Defaults to False.
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 = 0,
48
- presentation_name_pattern: str = "",
49
- language: str = "nb",
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 (str): The start date of the time period. "YYYY-MM-DD".
108
- to_date (str): The end date of the time period. "YYYY-MM-DD".
109
- include_future (bool): Whether to include future codes.
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 (bool): Whether to raise an error if the returned dataframe is empty. Defaults to True.
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 = "", # default is "name" if not set
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 (str): The name of the column with the values you want as keys.
178
- value (str): The name of the column with the values you want as values in your dict.
179
- other (str): If key is missing from dict, return this value instead, if you specify an OTHER-value.
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 (list[str]): The start of the names of the columns you want to keep when done.
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
 
@@ -5,11 +5,13 @@ from datetime import date
5
5
  import dateutil.parser
6
6
  import pandas as pd
7
7
  from typing_extensions import Self
8
+ from typing_extensions import overload
8
9
 
9
- from klass.requests.klass_requests import correspondence_table_by_id
10
- from klass.requests.klass_requests import corresponds
11
- from klass.requests.klass_types import CorrespondsType
12
- from klass.requests.klass_types import T_correspondanceMaps
10
+ from ..requests.klass_requests import correspondence_table_by_id
11
+ from ..requests.klass_requests import corresponds
12
+ from ..requests.klass_types import CorrespondsType
13
+ from ..requests.klass_types import Language
14
+ from ..requests.klass_types import T_correspondanceMaps
13
15
 
14
16
 
15
17
  class KlassCorrespondence:
@@ -35,26 +37,52 @@ class KlassCorrespondence:
35
37
  include_future (bool): If the correspondence should include future correspondences.
36
38
 
37
39
  Args:
38
- correspondence_id (str): The ID of the correspondence.
39
- source_classification_id (str): The ID of the source classification.
40
- target_classification_id (str): The ID of the target classification.
41
- from_date (str): The start date of the correspondence.
42
- to_date (str, optional): The end date of the correspondence.
43
- contain_quarter (int): The number of quarters the correspondence should contain,
40
+ correspondence_id: The ID of the correspondence.
41
+ source_classification_id: The ID of the source classification.
42
+ target_classification_id: The ID of the target classification.
43
+ from_date: The start date of the correspondence.
44
+ to_date: The end date of the correspondence.
45
+ contain_quarter: The number of quarters the correspondence should contain,
44
46
  this replaces the to_date during initialization.
45
- language (str): The language of the correspondence. "nb", "nn" or "en".
46
- include_future (bool): If the correspondence should include future correspondences.
47
+ language: The language of the correspondence. "nb", "nn" or "en".
48
+ include_future: If the correspondence should include future correspondences.
47
49
  """
48
50
 
51
+ @overload
49
52
  def __init__(
50
- self,
51
- correspondence_id: str = "",
52
- source_classification_id: str = "",
53
- target_classification_id: str = "",
54
- from_date: str = "",
55
- to_date: str = "",
53
+ self: Self,
54
+ correspondence_id: str | int = ...,
55
+ source_classification_id: None = ...,
56
+ target_classification_id: None = ...,
57
+ from_date: None = ...,
58
+ to_date: None = ...,
59
+ contain_quarter: int = ...,
60
+ language: Language = ...,
61
+ include_future: bool = ...,
62
+ ) -> None: ...
63
+
64
+ @overload
65
+ def __init__(
66
+ self: Self,
67
+ correspondence_id: None = ...,
68
+ source_classification_id: str | int = ...,
69
+ target_classification_id: str | int = ...,
70
+ from_date: str = ...,
71
+ to_date: str | None = ...,
72
+ contain_quarter: int = ...,
73
+ language: Language = ...,
74
+ include_future: bool = ...,
75
+ ) -> None: ...
76
+
77
+ def __init__(
78
+ self: Self,
79
+ correspondence_id: str | int | None = None,
80
+ source_classification_id: str | int | None = None,
81
+ target_classification_id: str | int | None = None,
82
+ from_date: str | None = None,
83
+ to_date: str | None = None,
56
84
  contain_quarter: int = 0,
57
- language: str = "nb",
85
+ language: Language = "nb",
58
86
  include_future: bool = False,
59
87
  ) -> None:
60
88
  """Get the correspondence-data from the API."""
@@ -64,7 +92,7 @@ class KlassCorrespondence:
64
92
  self.from_date = from_date
65
93
  self.to_date = to_date
66
94
  self.contain_quarter = contain_quarter
67
- self.language = language
95
+ self.language: Language = language
68
96
  self.include_future = include_future
69
97
 
70
98
  self.get_correspondence()
@@ -166,8 +194,15 @@ class KlassCorrespondence:
166
194
  Uses the attribute "contain_quarter" to determine which quarter to use.
167
195
 
168
196
  Returns:
169
- str: The last date of the quarter.
197
+ The last date of the quarter.
198
+
199
+ Raises:
200
+ ValueError: if from date is missing
170
201
  """
202
+ if not self.from_date:
203
+ raise ValueError(
204
+ "Can't calculate the last date of the quarter without from_date"
205
+ )
171
206
  from_date = dateutil.parser.parse(self.from_date)
172
207
  year = from_date.year
173
208
  last_month_of_quarter = 3 * self.contain_quarter
@@ -180,9 +215,9 @@ class KlassCorrespondence:
180
215
  self,
181
216
  key: str = "sourceCode",
182
217
  value: str = "targetCode",
183
- other: str = "",
218
+ other: str | None = None,
184
219
  remove_na: bool = True,
185
- select_level: int = 0,
220
+ select_level: int | None = None,
186
221
  ) -> dict[str, str | None] | defaultdict[str, str | None]:
187
222
  """Extract two columns from the data, turning them into a dict.
188
223
 
@@ -192,9 +227,9 @@ class KlassCorrespondence:
192
227
  'targetCode', 'targetName', 'targetShortName', 'validFrom', 'validTo'.
193
228
 
194
229
  Args:
195
- key (str): The name of the column with the values you want as keys.
196
- value (str): The name of the column with the values you want as values in your dict.
197
- other (str): The value to use for keys that don't exist in the data.
230
+ key: The name of the column with the values you want as keys.
231
+ value: The name of the column with the values you want as values in your dict.
232
+ other: The value to use for keys that don't exist in the data.
198
233
  remove_na: Set to False if you want to keep empty mappings over the key and value columns. Empty is defined as empty strings or NA-types.
199
234
  select_level: Keep only a specific level defines the variants codes / groups.
200
235
 
@@ -1,7 +1,7 @@
1
- from klass.classes.classification import KlassClassification
2
- from klass.requests.klass_requests import classificationfamilies_by_id
3
- from klass.requests.klass_types import ClassificationFamiliesByIdType
4
- from klass.requests.klass_types import ClassificationPartWithType
1
+ from ..requests.klass_requests import classificationfamilies_by_id
2
+ from ..requests.klass_types import ClassificationFamiliesByIdType
3
+ from ..requests.klass_types import ClassificationPartWithType
4
+ from .classification import KlassClassification
5
5
 
6
6
 
7
7
  class KlassFamily:
@@ -17,10 +17,10 @@ class KlassFamily:
17
17
  _links (dict): A dictionary of API links referencing itself.
18
18
 
19
19
  Args:
20
- family_id (str): The ID of the family.
20
+ family_id: The ID of the family.
21
21
  """
22
22
 
23
- def __init__(self, family_id: str) -> None:
23
+ def __init__(self, family_id: str | int) -> None:
24
24
  """Get the family data from the klass-api, setting it as attributes on the object."""
25
25
  self.family_id = family_id
26
26
  # Setting for mypy
@@ -57,11 +57,13 @@ And contains the following classifications:
57
57
  """Representation of the object, and how to recreate it."""
58
58
  return f"KlassFamily({self.family_id})"
59
59
 
60
- def get_classification(self, classification_id: str = "") -> KlassClassification:
60
+ def get_classification(
61
+ self, classification_id: str | int | None = None
62
+ ) -> KlassClassification:
61
63
  """Get a classification from the family.
62
64
 
63
65
  Args:
64
- classification_id (str): The ID of the classification.
66
+ classification_id: The ID of the classification.
65
67
  If not given, the first classification in the family is returned based on its ID.
66
68
 
67
69
  Returns: