Skip to main content

foodunits

Validate and convert food units with ease!

Installation

You can install foodunits using pip:

$ pip install foodunits

Usage

foodunits provides functionality to validate and convert food units. It supports both imperial and metric units, including common units such as "liter," "fluid ounce," "pint," "gram," "pound," as well as specific food-related units like "slice," "can," "bottle," and more.

Validation

The validation feature checks whether a string represents a valid food unit. It is case, space, singular-plural, and full name-symbol insensitive. Only the spelling matters. It returns a boolean value indicating the validity of the unit. Here are some examples:

"Value + Unit" type strings can also be validated, such as "5 mls" or "five milliliters".

from foodunits import units_validator

units_validator("fluid ounce")  # True
units_validator("fl oz")  # True
units_validator("5 fl ozs")  # True
units_validator("5 fl  ozs ")  # True
units_validator("five fl ozs")  # True
units_validator("flud ounce")  # False
units_validator("f oz")  # False
units_validator("5 l oz")  # False
units_validator("fiv fl ozs")  # False

Conversation

The conversion feature allows you to convert between volumetric and mass food units, both in imperial and metric systems. For example:

from foodunits import units_convertor

units_convertor("1 fluid ounce", to_unit="ml")
# Output: {"converted value": 29.573, "unit": "ml"}

units_convertor("1 1/2 fluid ounce", to_unit="ml")
# Output: {"converted value": 44.360, "unit": "ml"}

If you want to convert between volume and mass and have the correct conversion based on the food ingredient, you can provide the ingredient name. Currently, the package supports 100+ major ingredients. If the ingredient you input is not supported, you can provide a value for the "ingredient_density" argument. Here's an example:

units_convertor("2.5", to_unit="g", from_unit="fl oz", ingredient="skimmed milk")
# Output: {"converted value": 76.152, "unit": "g"}

For cookery units like cup, tablespoon, and teaspoon, which depend on the country, you need to specify the country name or code: "2.5 cups", "skimmed milk", "United States" -> "g"

units_convertor("2.5 cups", to_unit="g", ingredient="skimmed milk", country="United States")
# Output: {"converted value": 618.0, "unit": "g"}

Note: The decimal parameter can be used to specify the number of decimal places in the converted value.

Make sure to import the relevant functions from the foodunits package to use them in your code.

Contributing

Interested in contributing? Check out the contributing guidelines. Please note that this project is released with a Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

foodunits was created by Ben Zhang. It is licensed under the terms of the MIT license.

Credits

foodunits was created with cookiecutter and the py-pkgs-cookiecutter template. Inspired by validators and UnitConverter

Metadata

Release files for foodunits 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for foodunits 0.1.0
File Size Uploaded Provenance Actions
foodunits-0.1.0.tar.gz 13.7 kB Without provenance Details for foodunits-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for foodunits 0.1.0
File Interpreter ABI Platform Actions
foodunits-0.1.0-py3-none-any.whl Python 3 none any Details for foodunits-0.1.0-py3-none-any.whl

Total release size: 28.0 kB

Release files / foodunits-0.1.0.tar.gz

Download URL foodunits-0.1.0.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e766b330e54c375d83c44506faf2d438963f48edfd79fe33b2e21b8eace3431a
BLAKE2b-256 checksum
How to use checksums
de4c421bc4ba088bb0d6a208d52924d9896f0c12e993ff6e69bbe57e48af7f5a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.9.17 Darwin/22.5.0

Release files / foodunits-0.1.0-py3-none-any.whl

Download URL foodunits-0.1.0-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
05c9079eea979bc6a079c0c0e29fc7e1837c0853fc758311459c1b8383a8c412
BLAKE2b-256 checksum
How to use checksums
2ab46e0983a869949fb31f1c28fa2917a74bd117cc6e875f85592d719803058b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.6 CPython/3.9.17 Darwin/22.5.0

Security for foodunits 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page