<commit_before>"""
The qiprofile unit of measurement Mongodb data model.
"""

import decimal
from numbers import Number
import mongoengine
from mongoengine import fields

class Measurement(mongoengine.EmbeddedDocument):
    """
    A scientific measurement.

    The measurement is a quantitative amount associated with a unit.
    The unit is the Unit as captured and displayed. The amount is
    expressed as a python Decimal in unscaled units. If the constructor
    is called with a non-Decimal numeric amount, then the value is
    converted to a Decimal, e.g.::

        Measurement(amount=0.006, unit=Weight())

    is equivalent to::

        from decimal import Decimal
        Measurement(amount=Decimal(0.006), unit=Weight())

    :Note: the client is responsible for saving the measurement amount
      in unscaled units and converting the database amount to the preferred
      unit. For example, 40mg is saved as follows::

          Measurement(amount=0.04, unit=Volume())

      which is equivalent to::

          Measurement(amount=0.04, unit=Volume(scale='m'))

      When this measurement is read from the database, the client then
      converts the measurement to the preferred display value ``40mg``.

      The measurement unit can be qualified by a second ``per_unit``
      dimension, e.g. 2 mg/kg dosage per patient weight is expressed
      as::

          Measurement(amount=0.002, unit=Weight(), per_unit=Weight(scale='k'))

    :Note: the amount is a :class:`Decimal` embedded object rather than
      the broken MongoEngine ``DecimalField`` (see the :class:`Decimal`
      comment).
    """

    def __init__(self, *args, **kwargs):
        # Convert the amount to a Decimal, if necessary.
        # The amount can be the first positional argument
        # or a keyword argument.
        if args:
            args = list(args)
            args[0] = self._as_decimal(args[0])
        elif 'amount' in kwargs:
            kwargs['amount'] = self._as_decimal(kwargs['amount'])
        super(Measurement, self).__init__(*args, **kwargs)

    def _as_decimal(self, value):
        if isinstance(value, Number):
            return Decimal(value)
        else:<commit_msg>Document __init__.<commit_after>"""
The qiprofile unit of measurement Mongodb data model.
"""

import decimal
from numbers import Number
import mongoengine
from mongoengine import fields

class Measurement(mongoengine.EmbeddedDocument):
    """
    A scientific measurement.

    The measurement is a quantitative amount associated with a unit.
    The unit is the Unit as captured and displayed. The amount is
    expressed as a python Decimal in unscaled units. If the constructor
    is called with a non-Decimal numeric amount, then the value is
    converted to a Decimal, e.g.::

        Measurement(amount=0.006, unit=Weight())

    is equivalent to::

        from decimal import Decimal
        Measurement(amount=Decimal(0.006), unit=Weight())

    :Note: the client is responsible for saving the measurement amount
      in unscaled units and converting the database amount to the preferred
      unit. For example, 40mg is saved as follows::

          Measurement(amount=0.04, unit=Volume())

      which is equivalent to::

          Measurement(amount=0.04, unit=Volume(scale='m'))

      When this measurement is read from the database, the client then
      converts the measurement to the preferred display value ``40mg``.

      The measurement unit can be qualified by a second ``per_unit``
      dimension, e.g. 2 mg/kg dosage per patient weight is expressed
      as::

          Measurement(amount=0.002, unit=Weight(), per_unit=Weight(scale='k'))

    :Note: the amount is a :class:`Decimal` embedded object rather than
      the broken MongoEngine ``DecimalField`` (see the :class:`Decimal`
      comment).
    """

    def __init__(self, *args, **kwargs):
        """
        Initializes the Measurement document. The amount can be either
        a positional or a keyword argument. The amount is converted to
        a Decimal.
        
        :param args: the amount as a positional argument
        :param kwargs: the following keyword arguments:
        :keyword amount: the amount as a keyword argument
        """
        # Convert the amount to a Decimal, if necessary. The amount
        # can be the first positional argument or a keyword argument.
        if args:
            args = list(args)
            args[0] = self._as_decimal(args[0])
        elif 'amount' in kwargs:
            kwargs['amount'] = self._as_decimal(kwargs['amount'])
        
        super(Measurement, self).__init__(*args, **kwargs)

    def _as_decimal(self, value):
        if isinstance(value, Number):
            return Decimal(value)
        else:
{'fraction_non_alphanumeric': 0.08226652675760755, 'fraction_numerical': 0.01049317943336831, 'mean_word_length': 2.828112449799197, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3073047', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1209, 'n_words': 510}