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:
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:
- frac
Fraction Fraction object to convert to string.
- frac
- Returns:
strConverted 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:
- Yields:
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:
- Returns:
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:
- token
str Fraction in intermediate format.
- token
- Returns:
strIntermediate 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:
- value
str Value to check.
- value
- 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.
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.
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:
- lang
str,optional Selected language to open model card for.
- lang
- Raises:
FileNotFoundErrorRaised if model card not found at expected path.
ValueErrorRaised if unsupported language provided in lang argument.