core.bones.numeric

Module Contents

Classes

NumericBone

A bone for storing numeric values, either integers or floats.

Attributes

MIN

Constant for the minimum possible value in the system

MAX

Constant for the maximum possible value in the system

core.bones.numeric.MIN

Constant for the minimum possible value in the system

core.bones.numeric.MAX

Constant for the maximum possible value in the system

class core.bones.numeric.NumericBone(*, max=MAX, min=MIN, mode=None, precision=0, **kwargs)

Bases: viur.core.bones.base.BaseBone

A bone for storing numeric values, either integers or floats. For floats, the precision can be specified in decimal-places.

Parameters:
  • precision (int) – How may decimal places should be saved. Zero casts the value to int instead of float.

  • min (int | float) – Minimum accepted value (including).

  • max (int | float) – Maximum accepted value (including).

Initializes a new NumericBone.

type = 'numeric'
__setattr__(key, value)

Sets the attribute with the specified key to the given value.

This method is overridden in the NumericBone class to handle the special case of setting the ‘multiple’ attribute to True while the bone is of type float. In this case, an AssertionError is raised to prevent creating a multiple float bone.

Parameters:
  • key – The name of the attribute to be set.

  • value – The value to set the attribute to.

Raises:

AssertionError – If the ‘multiple’ attribute is set to True for a float bone.

isInvalid(value)

This method checks if a given value is invalid (e.g., NaN) for the NumericBone instance.

Parameters:

value – The value to be checked for validity.

Returns:

Returns a string “NaN not allowed” if the value is invalid (NaN), otherwise None.

getEmptyValue()

This method returns an empty value depending on the precision attribute of the NumericBone instance.

Returns:

Returns 0 for integers (when precision is 0) or 0.0 for floating-point numbers (when precision is non-zero).

isEmpty(value)

This method checks if a given raw value is considered empty for the NumericBone instance. It attempts to convert the raw value into a valid numeric value (integer or floating-point number), depending on the precision attribute of the NumericBone instance.

Parameters:

value (Any) – The raw value to be checked for emptiness.

Returns:

Returns True if the raw value is considered empty, otherwise False.

singleValueFromClient(value, skel, bone_name, client_data)

Load a single value from a client

Parameters:
  • value – The single value which should be loaded.

  • skel – The SkeletonInstance where the value should be loaded into.

  • bone_name – The bone name of this bone in the SkeletonInstance.

  • client_data – The data taken from the client, a dictionary with usually bone names as key

Returns:

A tuple. If the value is valid, the first element is the parsed value and the second is None. If the value is invalid or not parseable, the first element is a empty value and the second a list of ReadFromClientError.

buildDBFilter(name, skel, dbFilter, rawFilter, prefix=None)

Parses the searchfilter a client specified in his Request into something understood by the datastore. This function must:

    • Ignore all filters not targeting this bone

    • Safely handle malformed data in rawFilter (this parameter is directly controlled by the client)

Parameters:
  • name (str) – The property-name this bone has in its Skeleton (not the description!)

  • skel (viur.core.skeleton.SkeletonInstance) – The viur.core.db.Query this bone is part of

  • dbFilter (viur.core.db.Query) – The current viur.core.db.Query instance the filters should be applied to

  • rawFilter (dict) – The dictionary of filters the client wants to have applied

  • prefix (Optional[str]) –

Returns:

The modified viur.core.db.Query

Return type:

viur.core.db.Query

getSearchTags(skel, name)

This method generates a set of search tags based on the numeric values stored in the NumericBone instance. It iterates through the bone values and adds the string representation of each value to the result set.

Parameters:
  • skel (viur.core.skeleton.SkeletonInstance) – The skeleton instance containing the bone.

  • name (str) – The name of the bone.

Returns:

Returns a set of search tags as strings.

Return type:

set[str]

_convert_to_numeric(value)

Convert a value to an int or float considering the precision.

If the value is not convertable an exception will be raised.

Parameters:

value (Any) –

Return type:

int | float

refresh(skel, boneName)

Ensure the value is numeric or None.

This ensures numeric values, for example after changing a bone from StringBone to a NumericBone.

Parameters:
  • skel (viur.core.skeleton.SkeletonInstance) –

  • boneName (str) –

Return type:

None

iter_bone_value(skel, name)

Yield all values from the Skeleton related to this bone instance.

This method handles multiple/languages cases, which could save a lot of if/elifs. It always yields a triplet: index, language, value. Where index is the index (int) of a value inside a multiple bone, language is the language (str) of a multi-language-bone, and value is the value inside this container. index or language is None if the bone is single or not multi-lang.

This function can be used to conveniently iterate through all the values of a specific bone in a skeleton instance, taking into account multiple and multi-language bones.

Parameters:
  • skel (viur.core.skeleton.SkeletonInstance) – The skeleton instance where the values should be loaded from. This is an instance of a class derived from viur.core.skeleton.SkeletonInstance.

  • name (str) – The name of the bone, which is a string representing the key for the bone in the skeleton. This should correspond to an existing bone in the skeleton instance.

Returns:

A generator which yields triplets (index, language, value), where index is the index of a value inside a multiple bone, language is the language of a multi-language bone, and value is the value inside this container. index or language is None if the bone is single or not multi-lang.

Return type:

Iterator[tuple[Optional[int], Optional[str], Any]]

structure()

Describes the bone and its settings as an JSON-serializable dict. This function has to be implemented for subsequent, specialized bone types.

Return type:

dict