gpt4 book ai didi

python - 将递归计算器重构为迭代计算器

转载 作者:行者123 更新时间:2023-12-01 04:46:48 27 4
gpt4 key购买 nike

我有一个 Django 应用程序,它是一个计算器。用户在一个屏幕上配置任意深度的计算(例如 Excel 公式),然后在另一个屏幕上输入(单元格)数据。

将字段链接到其值后,我最终得到以下形式的公式

SUM(1,2,4)

可能是任意深度,例如

SUM(1,SUM(5,DIFFERENCE(6,DIVISION(8,10),7),4),2)

让我头疼的公式之一是用户在我们系统中输入的更复杂的公式之一:

ROUND(MULTIPLICATION(DIVISION(ROUND(SUM(MULTIPLICATION(50.00000,50.00000),MULTIPLICATION(50.00000,50.00000,2),MULTIPLICATION(50.00000,50.00000)),-2),300),DIFFERENCE(DIFFERENCE(41,7),0.3)),0)

我使用 pyparsing 来解析公式并提取值和嵌套公式,并递归地执行计算。问题是,由于解析每个嵌套计算,我遇到了 pyparsing 的递归限制。

我的递归代码:

class Calculator:
def __init__(self, formula=None):
self.formula = formula

def do_calculation(self):
# parse the formula we receive, which returns arguments in groups of numbers and nested calculations
# e.g. SUM(MULTIPLICATION(12,11),1,5)
parsed_formula = FormulaParser(self.formula).get_parsed_formula()

#calculation name is the outermost level calculation
calc_name = parsed_formula['calculation_name']
#don't stomp on built in round
if 'ROUND' in "".join(calc_name):
calc_name = ["ROUND_CALCULATION"]
#don't stomp on if
if 'IF' == "".join(calc_name):
calc_name = ['IF_STATEMENT']
#grab the name of the calculation, will match a function name below
ex = getattr(self, string.lower("".join(calc_name)))
calc_arguments = []
#formulas need to be recursively executed
formulas = parsed_formula.args.formulas.asList() if len(parsed_formula.args.formulas) else []
#numbers are just added to the arguments
dnumbers = parsed_formula.args.dnumbers.asList() if len(parsed_formula.args.dnumbers) else []

for arg in parsed_formula.args.asList():
if arg in dnumbers:
calc_arguments.append(''.join(arg))
elif arg in formulas:
new_calc = Calculator(''.join(self.flatten(arg[:])))
calc_arguments.append(new_calc.do_calculation())

#execute the calculation with the number arguments
for idx, arg in enumerate(calc_arguments):
if isinstance(arg, dict) and arg['rounding']:
calc_arguments[idx] = arg['result']
result = ex(*calc_arguments)
#for rounding, output is special to tell the api to not format to default 5 decimal places
if 'ROUND' in "".join(calc_name):
return dict(result=result, rounding=True)
return result

# function called on nested calculations that may have other nested calculations to flatten to a single level list
@staticmethod
def flatten(expr):
for i, x in enumerate(expr):
while isinstance(expr[i], list):
expr[i:i + 1] = expr[i]
return expr

以及公式解析器:

class FormulaParser():
def __init__(self, formula=None):
self.formula = formula

# grammar
# end result
expr = Forward()
formula = Forward()

#calculation keywords
calc_keyword = lambda name: Keyword(name)
calculations = [calc_keyword(calc) for calc in CALCULATION_TYPES]
calc_name = Group(reduce(lambda y, z: y | z, [x for x in calculations])).setResultsName('calculation_name')

#symbols
oparen, cparen, comma, dot, minus = map(Literal, '(),.-')
dnumber = Combine(Optional(minus) + Word(nums) + Optional(dot + Word(nums)))

#possible formulas
expr = Group(formula).setResultsName('formulas', listAllMatches=True) | Group(dnumber).setResultsName(
'dnumbers', listAllMatches=True)
exprs = expr + ZeroOrMore(comma + expr)

#entire formula
formula << Combine(calc_name + Group(oparen + exprs + cparen).setResultsName('args'))
self.parsed_formula = formula

def get_parsed_formula(self):
if self.formula:
return self.parsed_formula.parseString(self.formula)

return None

我已经使用 this SO answer 中的堆栈方法将应用程序的其他递归部分重构为迭代。 .

虽然这可以将字段定义链接到用户输入,但当计算归结为仅参数时,我很难理解如何执行计算,然后将结果传递到下一个堆栈级别,等等.

最佳答案

我不确定这是否能帮助您专注于递归解析堆栈。如果您只想评估表达式,那么您可以使用解析操作处理所有事情,并在解析时进行评估。请参阅我的 mods 中对您提供的源代码的嵌入注释:

sample = """ROUND(MULTIPLICATION(DIVISION(ROUND(SUM(MULTIPLICATION(50.00000,50.00000),MULTIPLICATION(50.00000,50.00000,2),MULTIPLICATION(50.00000,50.00000)),-2),300),DIFFERENCE(DIFFERENCE(41,7),0.3)),0)"""

from pyparsing import *

CALCULATION_TYPES = "ROUND MULTIPLICATION DIVISION SUM DIFFERENCE".split()

functionMap = {
"ROUND" : lambda args: round(args[0]),
"MULTIPLICATION" : lambda args: args[0]*args[1],
"DIVISION" : lambda args: args[0]/args[1],
"SUM" : lambda args: args[0]+args[1],
"DIFFERENCE" : lambda args: args[0]-args[1],
}

class FormulaParser():
def __init__(self, formula=None):
self.formula = formula

# grammar
# end result
expr = Forward()
formula = Forward()

#calculation keywords
calc_keyword = lambda name: Keyword(name)
calculations = [calc_keyword(calc) for calc in CALCULATION_TYPES]
calc_name = Group(reduce(lambda y, z: y | z, [x for x in calculations])).setResultsName('calculation_name')

# a simpler way to create a MatchFirst of all your calculations
# also, save the results names for when you assemble small elements into larger ones
calc_name = MatchFirst(calculations)

#symbols
oparen, cparen, comma, dot, minus = map(Literal, '(),.-')
#dnumber = Combine(Optional(minus) + Word(nums) + Optional(dot + Word(nums)))
# IMPORTANT - convert numbers to floats at parse time with this parse action
dnumber = Regex(r'-?\d+(\.\d+)?').setParseAction(lambda toks: float(toks[0]))

#possible formulas
#expr = Group(formula).setResultsName('formulas', listAllMatches=True) |
# Group(dnumber).setResultsName('dnumbers', listAllMatches=True)
#exprs = expr + ZeroOrMore(comma + expr)

#entire formula
#formula << Combine(calc_name + Group(oparen + exprs + cparen).setResultsName('args'))
#self.parsed_formula = formula

# define what is allowed for a function arg
arg_expr = dnumber | formula
def eval_formula(tokens):
fn = functionMap[tokens.calculation_name]
return fn(tokens.args)

# define overall formula, and add results names here
formula <<= (calc_name("calculation_name") + oparen
+ Optional(delimitedList(arg_expr))('args')
+ cparen).setParseAction(eval_formula)
self.parsed_formula = formula


def get_parsed_formula(self):
if self.formula:
return self.parsed_formula.parseString(self.formula)

return None

fp = FormulaParser(sample)
print fp.get_parsed_formula()

关于python - 将递归计算器重构为迭代计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243804/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com