Common#

ingredient_parser._common.consume(iterator: Iterator[Any], n: int | None) → None[source]#

Advance the iterator n-steps ahead. If n is none, consume entirely.

See consume from https://docs.python.org/3/library/itertools.html#itertools-recipes

Parameters:
iteratorIterator[Any]

Iterator to advance.

nint | None

Number of iterations to advance by. If None, consume entire iterator.

Examples

>>> it = iter(range(10))
>>> consume(it, 3)
>>> next(it)
3
>>> it = iter(range(10))
>>> consume(it, None)
>>> next(it)
StopIteration
ingredient_parser._common.fraction_obj_to_str(frac: Fraction) → str[source]#

Convert Fraction object to string.

If the fractional part of the Fraction object can be converted to a unicode fraction, return as a unicode fraction. Otherwise return as a decimal.

Parameters:
fracFraction

Fraction object to convert to string.

Returns:
str

Converted string.

Examples

>>> fraction_obj_to_str(Fraction(1))
'1'
>>> fraction_obj_to_str(Fraction(5, 2))
'2½'
>>> fraction_obj_to_str(Fraction(0.75))
'¾'
>>> fraction_obj_to_str(Fraction(11829411824999997, 25000000000000))
'473.176'
>>> fraction_obj_to_str(Fraction(100000000000000009336059, 40000000000000000000000))
'2½'
ingredient_parser._common.group_consecutive_idx(idx: list[int]) → Generator[Iterator[int], None, None][source]#

Yield groups of consecutive indices.

Given a list of integers, yield groups of integers where the value of each in a group is adjacent to the previous element’s value.

Parameters:
idxlist[int]

List of indices.

Yields:
Generator[Iterator[int], None, None]

List of lists, where each sub-list contains consecutive indices.

Examples

>>> groups = group_consecutive_idx([0, 1, 2, 4, 5, 6, 8, 9])
>>> [list(g) for g in groups]
[[0, 1, 2], [4, 5, 6], [8, 9]]
ingredient_parser._common.incremental_sublists(sequence: list[T], reverse: bool = False) → list[list[T]][source]#

Return sublists of sequence, retaining the same order, where each sublist contains incrementally more of sequence.

Parameters:
sequencelist[T]

Sequence of elements.

reversebool, optional

If True, generate incremental sublists starting at end of sequence

Returns:
list[list[T]]

Incremental sublists of sequence

Examples

>>> incremental_sublists([0, 1, 2, 3])
[[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]
>>> incremental_sublists(["this", "is", "a", "sequence"])
[["this"], ["this", "is"], ["this", "is", "a"], ["this", "is", "a", "sequence"]]
>>> incremental_sublists([0, 1, 2, 3], reverse=True)
[[3], [3, 2], [3, 2, 1], [3, 2, 1, 0]]
ingredient_parser._common.intermediate_fraction_to_unicode(token: str) → str[source]#

Convert intermediate format fraction to unicode fraction.

The intermediate format uses # to mark the start of the fraction (and separate any leading integer from the fraction) and $ to mark the separation between the numerator and denominator. For example:

#1$2 = ½ 2#1$3 = 2⅓

If the fraction part does not correspond to a unicode fraction, it is returned in a plain text form e.g. 1/2.

Parameters:
tokenstr

Fraction in intermediate format.

Returns:
str

Intermediate format fraction converted to unicode fraction.

Examples

>>> intermediate_fraction_to_unicode("#1$2")
½
>>> intermediate_fraction_to_unicode("1#1$3")
1⅓
>>> intermediate_fraction_to_unicode("2#1$9")
2 1/9
>>> intermediate_fraction_to_unicode("#1$2-#3$4")
½-¾
>>> intermediate_fraction_to_unicode("2#1$2-inch")
2½-inch
ingredient_parser._common.is_float(value: str) → bool[source]#

Check if value can be converted to a float.

Parameters:
valuestr

Value to check.

Returns:
bool

True if the value can be converted to float, else False.

Examples

>>> is_float("3")
True
>>> is_float("2.5")
True
>>> is_float("1-2")
False
>>> if_float("-5")
True
ingredient_parser._common.is_fraction(value: str) → bool[source]#

Check if value is a fraction given in the intermediate format.

Parameters:
valuestr

Value to check.

Returns:
bool

True if the value is an fraction, else False.

Examples

>>> is_fraction("#1$2")
True
>>> is_fraction("2#1$4")
True
>>> is_fraction("#1$2-#3$4")
False
ingredient_parser._common.is_range(value: str) → bool[source]#

Check if value is a range e.g. 100-200.

Parameters:
valuestr

Value to check.

Returns:
bool

True if the value is a range, else False.

Examples

>>> is_range("1-2")
True
>>> is_range("100-500")
True
>>> is_range("1")
False
ingredient_parser._common.show_model_card(lang: str = 'en') → None[source]#

Open model card for specified language in default application.

Parameters:
langstr, optional

Selected language to open model card for.

Raises:
FileNotFoundError

Raised if model card not found at expected path.

ValueError

Raised if unsupported language provided in lang argument.