ssb_jordbruk_fagfunksjoner package

ssb_jordbruk_fagfunksjoner.produksjonstilskudd module

Utilities to help maintain and use the Produksjonstilskudd codelist.

Uses a registry pattern to build the Produksjonstilskudd codelist based on all currently created Produksjonskode objects, as they add themselves to the _registry list as they are created.

class Produksjonskode(code, label, groups, measured_in, description=None, valid_from=None, valid_to=None, replaces=None, replaced_by=None)

Bases: object

Represents a production code used in agricultural classification.

When initialized, each instance is automatically registered in the class-level registry _registry for use by Produksjonstilskudd.

code

A 3-digit production code (e.g., “101”).

Type:

str

label

The human-readable name of the production code.

Type:

str

groups

A list of categories this production code belongs to.

Type:

List[str]

measured_in

The unit of measurement (e.g., “antall”, “dekar”).

Type:

str

description

A textual description of the production code.

Type:

Optional[str]

valid_from

The year from which the code is valid.

Type:

Optional[str]

valid_to

The year until which the code is valid.

Type:

Optional[str]

replaces

A list of production codes that this code replaces.

Type:

List[str]

replaced_by

A list of production codes that replace this code.

Type:

List[str]

Raises:
  • ValueError – If code is not exactly 3 digits.

  • TypeError – If groups is not a list.

  • ValueError – If any value in groups is not a string.

  • ValueError – If measured_in is not in the set of valid measurement units.

Parameters:
  • code (str)

  • label (str)

  • groups (list[str])

  • measured_in (str)

  • description (str | None)

  • valid_from (str | None)

  • valid_to (str | None)

  • replaces (list[str] | None)

  • replaced_by (list[str] | None)

Example

>>> kode = Produksjonskode(
...     code="101",
...     label="Melkeku",
...     groups=["Storfe"],
...     measured_in="antall",
...     description="Melkekyr for produksjon"
... )
>>> print(kode.code)
101
is_valid()

Validates the Produksjonskode fields.

Raises:
  • ValueError – If code is not a 3-digit string.

  • TypeError – If groups is not a list.

  • ValueError – If any item in groups is not a string.

  • ValueError – If measured_in is invalid.

Return type:

None

Parameters:

self (Produksjonskode)

class Produksjonstilskudd

Bases: object

A service class for retrieving production codes by various criteria.

This class provides methods for filtering and retrieving registered production codes (Produksjonskode objects) based on categories or measurement units.

get_codes(categories=None, prefix=False)

Retrieves production codes filtered by the specified categories.

  • If categories is None, the method returns codes from all categories.

  • If categories is a string, it is converted into a single-element list.

  • If prefix is True, each returned code is prefixed with ‘pk_’.

Parameters:
  • categories (Union[str, List[str], None], optional) – Categories to filter by. Can be a single category (str), a list of categories, or None. Defaults to None, meaning use all categories.

  • prefix (bool) – Whether to prefix each code with ‘pk_’. Defaults to False.

  • self (Produksjonstilskudd)

Raises:

TypeError – If prefix is not a bool or categories is not a str, list, or None.

Returns:

A list of matching production codes (optionally prefixed).

Return type:

List[str]

get_codes_by_measurement(measurement, prefix=False)

Retrieves production codes that match a specific measurement unit.

  • If prefix is True, each returned code is prefixed with ‘pk_’.

Parameters:
  • measurement (str) – The measurement unit to filter by (e.g., ‘antall’).

  • prefix (bool) – Whether to prefix each code with ‘pk_’. Defaults to False.

  • self (Produksjonstilskudd)

Raises:
  • ValueError – If the measurement is not one of the valid measurement units.

  • TypeError – If prefix is not a bool.

Returns:

A list of production codes (optionally prefixed) that match the given measurement unit.

Return type:

List[str]