Welcome to glo’s documentation!¶
Tools for working with and representing nutrition information.
-
class
glo.nutrition.NutritionFact(name: str, quantity: Optional[pint.quantity.Quantity] = None)¶ Representation of a basic nutrition fact (essentially a named unit).
- Parameters
- name: str
String name of the nutrition fact. Examples include “sodium”, “fat”, or “calories”.
- quantity: pint.unit.Quantity
Associated
Quantityinstance that determines value and units for the nutrition fact. IfNoneis given, then quantity with unitdimensionlessand magnitude0is created.
Examples
>>> import glo >>> my_sodium = glo.NutritionFact("sodium", glo.Q_(250, "mg")) >>> my_sodium.name 'sodium' >>> my_sodium.amount 250 >>> my_sodium.units <Unit('milligram')> >>> my_sodium.amount += 10; my_sodium.amount 260 >>> my_sodium.units = glo.ureg.grams; my_sodium.quantity <Quantity(0.26, 'gram')>
- Attributes
- name: str
Read only name for this nutrition fact. See above parameter description for more information.
- quantity: pint.unit.Quantity
See above parameter description.
- amount:
Property that allows for getting and setting the quantity attribute’s value. Setting this attribute to
valis equivalent to:self.quantity = glo.Q_(val, self.units)Getting this attribute is equivalent to
self.quantity.m- units: pint.unit.Unit
Property that allows for getting and setting the quantity attribute’s units. Setting this attribute to
unitis equivalent to:self.quantity = self.quantity.to(self.amount, unit)Getting this attribute is equivalent to
self.quantity.units.
-
property
amount¶ Amount of given nutrition fact this instance represents.
-
property
name¶ Name of given nutrition fact this instance represents.
-
property
units¶ Pint units for the amount of the nutrition fact.
-
class
glo.nutrition.NutritionSet(*facts: glo.nutrition.NutritionFact)¶ Dictionary representation of a group of
NutritionFact.This class inherits from
collections.UserDictand is tailored to make working withNutritionFactmuch easier. Keys in this specialized dictionary are the string names ofNutritionFactand values are theNutritionFactthemselves.- Parameters
- facts: iterable
Iterable of
NutritionFactthat will act as the initial data for the set.
Examples
>>> import glo >>> sodium = glo.NutritionFact("sodium", glo.Q_(10, "grams")) >>> protein = glo.NutritionFact("protein", glo.Q_(15, "grams")) >>> fat = glo.NutritionFact("fat", glo.Q_(5, "grams")) >>> my_set = glo.NutritionSet(sodium, protein, fat) >>> my_set["sodium"].amount 10 >>> my_set["sodium"] += glo.Q_(5, "grams") >>> my_set["sodium"].amount 15 >>> my_set["calories"].amount 0 >>> calories = glo.NutritionFact("calories", glo.Q_(100, "calories")) >>> my_set.update(calories) >>> my_set["calories"].amount 100 >>> my_set += glo.NutritionSet(calories * 2) >>> my_set["calories"].amount 300
Methods
as_dict:
Return dictionary representation of NutritionSet instance. See method docstring below.
clear:
See
dict.clear.get:
See
dict.get.items:
See
dict.items.keys:
See
dict.keys.pop:
See
dict.pop.popitem:
See
dict.popitem.setdefault:
See
dict.setdefault.update:
Overloaded from
dict.updateto ease working withNutritionFacts. See method docstring below.values:
See
dict.values.-
as_dict() → Mapping[str, pint.quantity.Quantity]¶ Return
dictrepresenting thisNutritionSet.Keys in returned dictionary are
NutritionFactnames, and values are their associatedQuantitiesin thisNutritionSet.
-
update(other: Union[glo.nutrition.NutritionFact, Iterable[glo.nutrition.NutritionFact], Mapping[str, pint.quantity.build_quantity_class.<locals>.Quantity], glo.nutrition.NutritionSet], merge_func: Union[None, Callable[[pint.quantity.build_quantity_class.<locals>.Quantity, pint.quantity.build_quantity_class.<locals>.Quantity], pint.quantity.build_quantity_class.<locals>.Quantity]] = None) → None¶ Update the
NutritionSetbased on given arguments.Overloaded form of
dict.update.- Parameters
- other:
Lots of options here. Can be a
NutritionFact, an iterable ofNutritionFactinstances, a mapping of strings topint.quantity.Quantity, or anotherNutritionSet.- merge_func: callable
Callable that takes in two
NutritionFactinstances returns a singleNutritionFactinstance. If a key inothermatches a key in thisNutritionSet, then the quantities from each will be passed throughmerge_funcbefore being added into the newNutritionSet. These two statements are equivalent:
Examples
>>> # Say we have the following variables to start with: >>> import glo >>> sodium = glo.NutritionFact("sodium", glo.Q_(10, "grams")) >>> protein = glo.NutritionFact("protein", glo.Q_(15, "grams")) >>> fat = glo.NutritionFact("fat", glo.Q_(5, "grams")) >>> my_ns = glo.NutritionSet() >>> # We can update my_ns like so >>> my_ns.update(protein) >>> my_ns["protein"].amount 15 >>> my_ns.update([sodium, fat]) >>> my_ns.get("sodium").amount 10 >>> my_ns.get("fat").amount 5 >>> # Clear my_ns >>> my_ns.data = dict() >>> my_ns.update( ... {nf.name: nf.quantity for nf in [sodium, protein, fat]} ... ) >>> print( ... f"{my_ns['sodium'].amount}, " ... f"{my_ns['protein'].amount}, " ... f"{my_ns['fat'].amount}" ... ) 10, 15, 5 >>> my_ns.update( ... {'sodium': glo.Q_(10, 'grams')}, ... merge_func=lambda a, b: (a + b) * 2 ... ) >>> my_ns["sodium"].amount 40
Tools for working with and representing purchasable items.
-
class
glo.product.Product(name: str, upc: Optional[str] = None, price: Optional[float] = None)¶ Generic product a user can purchase at a store.
- Parameters
- name: str
- upc: str, optional
No validity checks are performed on this parameter in order to support multiple upc standards.
- price: float, optional
Not designated with specific currency, such as USD, in order to support multiple currencies.
Examples
>>> import glo >>> glo.Product('my_product') Product 'my_product'(None)@None >>> glo.Product('my_second_product', price=15.0) Product 'my_second_product'(None)@15.0 >>> glo.Product('my_third_product', upc='000000000000', price=10.0) Product 'my_third_product'(000000000000)@10.0
- Attributes
- name: str
Product’s string name. May be human-readable or id-like.
- upc: str
Universal Product Code for the item. If not given, will default to
None.- price: float
Product cost. If not given, will default to
None.
Methods
as_dict:
Return dictionary representation of Item instance. See method docstring below.
-
as_dict() → Mapping[str, Union[str, float]]¶ Return dictionary representation of the product.
Dictionary contains documented attributes and their values.
Examples
>>> import glo >>> my_product = glo.Product('my_product', '1337000012340000', 0.50) >>> my_product.as_dict() {'name': 'my_product', 'upc': '1337000012340000', 'price': 0.5}