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 Quantity instance that determines value and units for the nutrition fact. If None is given, then quantity with unit dimensionless and magnitude 0 is 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 val is 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 unit is 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.UserDict and is tailored to make working with NutritionFact much easier. Keys in this specialized dictionary are the string names of NutritionFact and values are the NutritionFact themselves.

Parameters
facts: iterable

Iterable of NutritionFact that 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.update to ease working with NutritionFacts. See method docstring below.

values:

See dict.values.

as_dict()Mapping[str, pint.quantity.Quantity]

Return dict representing this NutritionSet.

Keys in returned dictionary are NutritionFact names, and values are their associated Quantities in this NutritionSet.

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 NutritionSet based on given arguments.

Overloaded form of dict.update.

Parameters
other:

Lots of options here. Can be a NutritionFact, an iterable of NutritionFact instances, a mapping of strings to pint.quantity.Quantity, or another NutritionSet.

merge_func: callable

Callable that takes in two NutritionFact instances returns a single NutritionFact instance. If a key in other matches a key in this NutritionSet, then the quantities from each will be passed through merge_func before being added into the new NutritionSet. 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}

Indices and tables