Skip to content

Commit

Permalink
Fix imports and add utilities; upgrade version
Browse files Browse the repository at this point in the history
  • Loading branch information
hdadhich01 committed Jul 8, 2022
1 parent c6b67cb commit e93d816
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
[project]
name = "round-nutrition"
authors = [{ name = "Harsh Dadhich" }]
version = "1.0.2"
readme = "README.md"
version = "1.0.3"
description = "Round nutritional values for federal compliance."
classifiers = [
"License :: OSI Approved :: MIT License",
Expand Down
2 changes: 1 addition & 1 deletion round_nutrition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from mineral import Mineral
from other import Other

__version__ = "1.0.0"
__version__ = "1.0.3"
__sources__ = [
"https://www.fda.gov/files/food/published/Food-Labeling-Guide-%28PDF%29.pdf",
"https://www.usdairy.com/~/media/usd/public/dmi-quick-reference-guide_nutrition-claims-for-dairy-products_2018.pdf",
Expand Down
46 changes: 1 addition & 45 deletions round_nutrition/main.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
from re import match


def parse_quantity(quantity):
if quantity is None:
return 0
elif type(quantity) in [int, float]:
return quantity
quantity = quantity.strip()
parsed = match(r"\d+(\.\d+)?", quantity).group()
unit = quantity[len(parsed) :].strip() if not quantity.isdigit() else ""
value = float(parsed) if "." in parsed else int(parsed)
return value, f" {unit}"


def round_increment(value, increment):
minimum = value // increment * increment
maximum = minimum + increment
res = maximum if (maximum - value) <= (value - minimum) else minimum
return res if res % 1.0 else int(res)


# # https://stackoverflow.com/a/61212047/14767766
# def find_first_meaningful_decimal(x):
# candidate = 0
# MAX_DECIMAL = 10
# EPSILON = 1 / 10 ** MAX_DECIMAL
# while round(x, candidate) < EPSILON:
# candidate += 1
# if candidate > MAX_DECIMAL:
# raise Exception('Number is too small: {}'.format(x))
# if int(x * 10 ** (candidate + 1)) == 5:
# candidate += 1
# return candidate

# def round_increment(value, increment):
# correction = 1e-5 if x > 0 else -1e-5
# result = round(value / increment + correction) * increment
# return round(result, find_first_meaningful_decimal(increment))
from utilities import round_increment, parse_quantity


class Main:
Expand Down Expand Up @@ -137,9 +99,3 @@ def protein(self, quantity: "int/str", minimal: bool = False) -> str:
elif value < 1:
return f"1{unit}" if minimal else f"less than 1{unit}"
return f"{round_increment(value, 1)}{unit}"


def vmo(quantity, increment):
value, unit = parse_quantity(quantity)
unit = " mcg" if unit.strip() == "" else unit
return f"{round_increment(value, increment)}{unit}"
2 changes: 1 addition & 1 deletion round_nutrition/mineral.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from main import vmo
from utilities import vmo


class Mineral:
Expand Down
2 changes: 1 addition & 1 deletion round_nutrition/other.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from main import vmo
from utilities import vmo


class Other:
Expand Down
46 changes: 46 additions & 0 deletions round_nutrition/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from re import match


def parse_quantity(quantity):
if quantity is None:
return 0
elif type(quantity) in [int, float]:
return quantity
quantity = quantity.strip()
parsed = match(r"\d+(\.\d+)?", quantity).group()
unit = quantity[len(parsed) :].strip() if not quantity.isdigit() else ""
value = float(parsed) if "." in parsed else int(parsed)
return value, f" {unit}"


def round_increment(value, increment):
minimum = value // increment * increment
maximum = minimum + increment
res = maximum if (maximum - value) <= (value - minimum) else minimum
return res if res % 1.0 else int(res)


# # https://stackoverflow.com/a/61212047/14767766
# def find_first_meaningful_decimal(x):
# candidate = 0
# MAX_DECIMAL = 10
# EPSILON = 1 / 10 ** MAX_DECIMAL
# while round(x, candidate) < EPSILON:
# candidate += 1
# if candidate > MAX_DECIMAL:
# raise Exception('Number is too small: {}'.format(x))
# if int(x * 10 ** (candidate + 1)) == 5:
# candidate += 1
# return candidate


# def round_increment(value, increment):
# correction = 1e-5 if x > 0 else -1e-5
# result = round(value / increment + correction) * increment
# return round(result, find_first_meaningful_decimal(increment))


def vmo(quantity, increment):
value, unit = parse_quantity(quantity)
unit = " mcg" if unit.strip() == "" else unit
return f"{round_increment(value, increment)}{unit}"

0 comments on commit e93d816

Please sign in to comment.