使用pyparsing进行部分评估 [英] Partial evaluation with pyparsing

查看:107
本文介绍了使用pyparsing进行部分评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够采用使用OpenDocument公式语法的公式,将其解析为Python可以理解的语法,但无需评估变量,然后能够通过更改变量的贵重物品多次评估公式. 公式可以作为用户输入,因此pyparsing使我既可以有效地处理公式语法,又可以清理用户输入. pyparsing的例子很多,但是所有数学上的例子似乎都假设一个能立即评估当前范围内的所有内容.

I need to be able to take a formula that uses the OpenDocument formula syntax, parse it into syntax that Python can understand, but without evaluating the variables, and then be able to evaluate the formula many times with changing valuables for the variables. Formulas can be user input, so pyparsing allows me to both effectively handle the formula syntax, and clean user input. There are a number of good examples of pyparsing available, but all the mathematical ones seem to assume that one evaluates everything in the current scope immediately.

就上下文而言,我正在使用一种工业经济模型(生命周期评估或LCA),其中这些公式代表过程之间的物质或能量交换量.可变数量可以是几个参数(例如地理位置)的函数.公式和变量引用的链存储在有向无环图中,因此始终可以对公式进行简单评估.公式以字符串形式存储在数据库中. 我的问题是:

For context, I am working with a model of the industrial economy (life cycle assessment, or LCA), where these formulas represent the amount of material or energy exchanges between processes. The variable amount can be a function of several parameters, such as geographical location. THe chain of formula and variable references are stored in a directed acyclic graph, so that formulas can always be simply evaluated. Formulas are stored as strings in a database. My questions are:

  1. 是否可以解析一个公式,使解析后的评估结果也可以存储在数据库中(作为要逃避的字符串或其他方式)?
  2. 是否有这种方法的替代方法?请记住,理想的解决方案是解析/写入一次,然后读取多次.例如,部分解析公式,然后使用ast模块,尽管我不知道如何使用数据库存储.
  3. 我可以查看的与此项目或库类似的任何示例吗?我不是程序员,只是一名学生,在业余时间制作开放源代码LCA软件模型时试图完成论文.
  4. 这种方法太慢了吗?我希望能够进行大量的蒙特卡洛试验,其中每次试验都可能涉及数以万计的公式评估(这是一个大数据库).

推荐答案

1)是的,可以解析表达式的结果并将结果保存到数据库中.然后,您可以仅获取和释放表达式,而不必重新解析原始表达式.

1) Yes, it is possible to pickle the results from parsing your expression, and save that to a database. Then you can just fetch and unpickle the expression, rather than reparse the original again.

2)您可以仅使用compile和eval内置插件来完成此任务,如以下交互式会话所示:

2) You can do a quick-and-dirty pass at this just using the compile and eval built-ins, as in the following interactive session:

>>> y = compile("m*x+b","","eval")
>>> m = 100
>>> x = 5
>>> b = 1
>>> eval(y)
501

当然,这具有任何基于eval或exec的实现的安全隐患,因为不受信任或恶意的源字符串可能会嵌入有害的系统调用.但是,如果这是您的命题,并且完全在您的控制范围之内,那么请不要做任何愚蠢的事情.

Of course, this has the security pitfalls of any eval- or exec-based implementation, in that untrusted or malicious source strings can embed harmful system calls. But if this is your thesis and entirely within your scope of control, just don't do anything foolish.

3)您可以在pyparsing Wiki的示例"页面上获得将表达式解析为可评估"数据结构的在线示例.查看 simpleBool.py 2008年5月号 2008年5月> Python杂志>,其中有我的文章使用Pyparsing写一个简单的解释器/编译器",其中更详细地描述了所使用的方法,并描述了对已解析的结果进行酸洗和未酸洗的方式.

3) You can get an online example of parsing an expression into a "evaluatable" data structure at the pyparsing wiki's Examples page. Check out simpleBool.py and evalArith.py especially. If you're feeling flush, order a back issue of the May,2008 issue of Python magazine, which has my article "Writing a Simple Interpreter/Compiler with Pyparsing" with a more detailed description of the methods used, plus a description of how pickling and unpickling the parsed results works.

4)解析的最慢部分,因此您以正确的中间值和可重复评估的形式保存这些结果是正确的.评估部分应该相当活泼.第二个慢的部分是从数据库中获取这些腌制的结构.在您的MC运行期间,我将打包一个函数,该函数采用表达式的选择参数,从数据库中获取数据,并进行点刺并返回可评估的表达式.然后,一旦完成这项工作,就可以使用备忘录装饰器来缓存这些查询结果对,这样,任何给定的表达式只需要提取/取消提取一次即可.

4) The slow part will be the parsing, so you are on the right track in preserving these results in some intermediate and repeatably-evaluatable form. The eval part should be fairly snappy. The second slow part will be in fetching these pickled structures from your database. During your MC run, I would package a single function that takes the selection parameters for an expression, fetches from the database, and unpickles and returns the evaluatable expression. Then once you have this working, use a memoize decorator to cache these query-results pairs, so that any given expression only needs to be fetched/unpickled once.

祝你论文好运!

这篇关于使用pyparsing进行部分评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆