core.bones.numeric ================== .. py:module:: core.bones.numeric Attributes ---------- .. autoapisummary:: core.bones.numeric.MIN core.bones.numeric.MAX Classes ------- .. autoapisummary:: core.bones.numeric.NumericBone Module Contents --------------- .. py:data:: MIN :value: -9223372036854775806 Constant for the minimum possible value in the system .. py:data:: MAX :value: 9223372036854775807 Constant for the maximum possible value in the system Also limited by the datastore (8 bytes). Halved for positive and negative values. Which are around 2 ** (8 * 8 - 1) negative and 2 ** (8 * 8 - 1) positive values. .. py:class:: NumericBone(*, min = MIN, max = MAX, precision = 0, mode=None, **kwargs) Bases: :py:obj:`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. Initializes a new NumericBone. :param min: Minimum accepted value (including). :param max: Maximum accepted value (including). :param precision: How may decimal places should be saved. Zero casts the value to int instead of float. .. py:attribute:: type :value: 'numeric' .. py:attribute:: precision :value: 0 .. py:attribute:: min :value: -9223372036854775806 .. py:attribute:: max :value: 9223372036854775807 .. py:method:: __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. :param key: The name of the attribute to be set. :param value: The value to set the attribute to. :raises AssertionError: If the 'multiple' attribute is set to True for a float bone. .. py:method:: singleValueUnserialize(val) Unserializes a single value of the bone from the stored database value. Derived bone classes should overwrite this method to implement their own logic for unserializing single values. The unserialized value should be suitable for use in the application logic. .. py:method:: singleValueSerialize(value, skel, name, parentIndexed) Serializes a single value of the bone for storage in the database. Derived bone classes should overwrite this method to implement their own logic for serializing single values. The serialized value should be suitable for storage in the database. .. py:method:: isInvalid(value) This method checks if a given value is invalid (e.g., NaN) for the NumericBone instance. :param value: The value to be checked for validity. :return: Returns a string "NaN not allowed" if the value is invalid (NaN), otherwise None. .. py:method:: getEmptyValue() This method returns an empty value depending on the precision attribute of the NumericBone instance. :return: Returns 0 for integers (when precision is 0) or 0.0 for floating-point numbers (when precision is non-zero). .. py:method:: 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. :param value: The raw value to be checked for emptiness. :return: Returns True if the raw value is considered empty, otherwise False. .. py:method:: singleValueFromClient(value, skel, bone_name, client_data) Load a single value from a client :param value: The single value which should be loaded. :param skel: The SkeletonInstance where the value should be loaded into. :param bone_name: The bone name of this bone in the SkeletonInstance. :param client_data: The data taken from the client, a dictionary with usually bone names as key :return: 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*. .. py:method:: 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) :param name: The property-name this bone has in its Skeleton (not the description!) :param skel: The :class:`viur.core.db.Query` this bone is part of :param dbFilter: The current :class:`viur.core.db.Query` instance the filters should be applied to :param rawFilter: The dictionary of filters the client wants to have applied :returns: The modified :class:`viur.core.db.Query` .. py:method:: 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. :param skel: The skeleton instance containing the bone. :param name: The name of the bone. :return: Returns a set of search tags as strings. .. py:method:: _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. .. py:method:: 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. .. py:method:: 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. :param skel: The skeleton instance where the values should be loaded from. This is an instance of a class derived from `viur.core.skeleton.SkeletonInstance`. :param name: 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. :return: 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. .. py:method:: structure() Describes the bone and its settings as an JSON-serializable dict. This function has to be implemented for subsequent, specialized bone types.