quantity package
quantity.forms module
quantity.models module
- class quantity.models.Quantity(unit, value)
Bases:
coolfarmtool.comparable.ComparableMixin
Combination of
decimal.Decimal
&Unit
- as_tuple()
Generate a
tuple
>>> Quantity('kg', 10).as_tuple() (10, 'kgs')
FIXME: implement using iterator (?) instead
- convert_to(to_unit, area=None)
Convert to
to_unit
(if possible) area parameter can be provided to convert detph to volume (to_unit=mm)- Parameters
- Raises
TypeError
if units are not compatible
Useful when you need to handle irrigation units volume = depth * area / 1000 with volume in KL, depth in mm, area in m2
- readable()
As __str__ but more readble rather than consistent!
- class quantity.models.QuantityField(*args, **kwargs)
Bases:
django.db.models.fields.DecimalField
Two-column model field for storing a
Quantity
- contribute_to_class(cls, name)
Register the field with the model class it belongs to.
If private_only is True, create a separate instance of this field for every subclass of cls, even if cls is not an abstract model.
- get_db_prep_save(value)
Return field’s value prepared for saving into a database.
- class quantity.models.Unit(*args, **kwargs)
Bases:
django.db.models.base.Model
Storage and conversion of units (e.g. “kg”)
>>> kg = Unit(name="kg") >>> kg.save() >>> tonne = Unit(name="tonne", conversion=kg, >>> conversion_factor=Decimal('0.001')) >>> tonne.save() >>> Quantity(tonne, 1).convert_to(kg) <Quantity: 1000.000 kg>
Unit conversion is handled using 3 fields: see
conversion
,conversion_factor
andconversion_intercept
. The asscociated database table is cached using the Django cache, so care should be taken to invalidate the cache when updating using something other than the Django admin.- exception DoesNotExist
- exception MultipleObjectsReturned
- class quantity.models.UnitField(*args, **kwargs)
Bases:
django.db.models.fields.related.ForeignKey
Store a reference to a
Unit
- formfield(**kwargs)
Pass
limit_choices_to
to the field being constructed.Only passes it if there is a type that supports related fields. This is a similar strategy used to pass the
queryset
to the field being constructed.
- class quantity.models.UnitGroup(id, name, description)
Bases:
django.db.models.base.Model
- exception DoesNotExist
- exception MultipleObjectsReturned
- class quantity.models.ValueField(*args, **kwargs)
Bases:
django.db.models.fields.DecimalField
Store a decimal value with default options
- formfield(**kwargs)
Specify
ValueInput
aswidget
, and definelabel
- quantity.models.get_unit(string)
Convenience method to get unit by name
quantity.widgets module
- class quantity.widgets.ValueInput(attrs=None)
Bases:
quantity.widgets.RemoveTrailingZeroesMixin
,django.forms.widgets.TextInput
Remove trailing zeroes when rendering field values
- quantity.widgets.remove_trailing_zeroes(value)
Remove trailing zeroes from a
decimal.Decimal