Validate or convert food units
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file foodunits-0.1.0.tar.gz
.
File metadata
- Download URL: foodunits-0.1.0.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.17 Darwin/22.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e766b330e54c375d83c44506faf2d438963f48edfd79fe33b2e21b8eace3431a |
|
MD5 | b26561e63cd1b8dd7dd53f504af71842 |
|
BLAKE2b-256 | de4c421bc4ba088bb0d6a208d52924d9896f0c12e993ff6e69bbe57e48af7f5a |
File details
Details for the file foodunits-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: foodunits-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.6 CPython/3.9.17 Darwin/22.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05c9079eea979bc6a079c0c0e29fc7e1837c0853fc758311459c1b8383a8c412 |
|
MD5 | b093f36959cfc82075cfad122b4c3f80 |
|
BLAKE2b-256 | 2ab46e0983a869949fb31f1c28fa2917a74bd117cc6e875f85592d719803058b |