formatting

This commit is contained in:
vabene1111
2021-01-07 22:47:53 +01:00
parent fe3f817bc5
commit e9f2b875b9

View File

@@ -1,6 +1,7 @@
import unicodedata import unicodedata
import string import string
def parse_fraction(x): def parse_fraction(x):
if len(x) == 1 and 'fraction' in unicodedata.decomposition(x): if len(x) == 1 and 'fraction' in unicodedata.decomposition(x):
frac_split = unicodedata.decomposition(x[-1:]).split() frac_split = unicodedata.decomposition(x[-1:]).split()
@@ -14,6 +15,7 @@ def parse_fraction(x):
except ZeroDivisionError: except ZeroDivisionError:
raise ValueError raise ValueError
def parse_amount(x): def parse_amount(x):
amount = 0 amount = 0
unit = '' unit = ''
@@ -39,11 +41,12 @@ def parse_amount(x):
unit = x[end:] unit = x[end:]
return amount, unit return amount, unit
def parse_ingredient_with_comma(tokens): def parse_ingredient_with_comma(tokens):
ingredient = '' ingredient = ''
note = '' note = ''
start = 0 start = 0
# search for first occurence of an argument ending in a comma # search for first occurrence of an argument ending in a comma
while start < len(tokens) and not tokens[start].endswith(','): while start < len(tokens) and not tokens[start].endswith(','):
start += 1 start += 1
if start == len(tokens): if start == len(tokens):
@@ -54,12 +57,13 @@ def parse_ingredient_with_comma(tokens):
note = ' '.join(tokens[start + 1:]) note = ' '.join(tokens[start + 1:])
return ingredient, note return ingredient, note
def parse_ingredient(tokens): def parse_ingredient(tokens):
ingredient = '' ingredient = ''
note = '' note = ''
if tokens[-1].endswith(')'): if tokens[-1].endswith(')'):
# Check if the matching opening bracket is in the same token # Check if the matching opening bracket is in the same token
if ((not tokens[-1].startswith('(')) and ('(' in tokens[-1])): if (not tokens[-1].startswith('(')) and ('(' in tokens[-1]):
return parse_ingredient_with_comma(tokens) return parse_ingredient_with_comma(tokens)
# last argument ends with closing bracket -> look for opening bracket # last argument ends with closing bracket -> look for opening bracket
start = len(tokens) - 1 start = len(tokens) - 1
@@ -79,6 +83,7 @@ def parse_ingredient(tokens):
ingredient, note = parse_ingredient_with_comma(tokens) ingredient, note = parse_ingredient_with_comma(tokens)
return ingredient, note return ingredient, note
def parse(x): def parse(x):
# initialize default values # initialize default values
amount = 0 amount = 0