mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
formatting
This commit is contained in:
@@ -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,13 +15,14 @@ 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 = ''
|
||||||
|
|
||||||
did_check_frac = False
|
did_check_frac = False
|
||||||
end = 0
|
end = 0
|
||||||
while end < len(x) and (x[end] in string.digits or ((x[end] == '.' or x[end] == ',') and end + 1 < len(x) and x[end+1] in string.digits)):
|
while end < len(x) and (x[end] in string.digits or ((x[end] == '.' or x[end] == ',') and end + 1 < len(x) and x[end + 1] in string.digits)):
|
||||||
end += 1
|
end += 1
|
||||||
if end > 0:
|
if end > 0:
|
||||||
amount = float(x[:end].replace(',', '.'))
|
amount = float(x[:end].replace(',', '.'))
|
||||||
@@ -34,32 +36,34 @@ def parse_amount(x):
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
amount += parse_fraction(x[end])
|
amount += parse_fraction(x[end])
|
||||||
unit = x[end+1:]
|
unit = x[end + 1:]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
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):
|
||||||
# no token ending in a comma found -> use everything as ingredient
|
# no token ending in a comma found -> use everything as ingredient
|
||||||
ingredient = ' '.join(tokens)
|
ingredient = ' '.join(tokens)
|
||||||
else:
|
else:
|
||||||
ingredient = ' '.join(tokens[:start+1])[:-1]
|
ingredient = ' '.join(tokens[:start + 1])[:-1]
|
||||||
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,13 +83,14 @@ 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
|
||||||
unit = ''
|
unit = ''
|
||||||
ingredient = ''
|
ingredient = ''
|
||||||
note = ''
|
note = ''
|
||||||
|
|
||||||
tokens = x.split()
|
tokens = x.split()
|
||||||
if len(tokens) == 1:
|
if len(tokens) == 1:
|
||||||
# there only is one argument, that must be the ingredient
|
# there only is one argument, that must be the ingredient
|
||||||
|
|||||||
Reference in New Issue
Block a user