化学元素のデータベースのクエリを解析したいと思います。
データベースは xml ファイルに保存されます。そのファイルを解析すると、collections.OrderedDict から継承するシングルトン オブジェクトに格納されるネストされた辞書が生成されます。
要素を要求すると、対応するプロパティの順序付けられた辞書が得られます (つまり、ELEMENTS['C'] --> {'name':'carbon','neutron' : 0,'proton':6, ...} )。
逆に、プロパティを要求すると、すべての要素の値の順序付けられた辞書が得られます (つまり、ELEMENTS['proton'] --> {'H' : 1, 'He' : 2} ...)。
典型的なクエリは次のとおりです。
mass > 10 or (nucleon < 20 and atomic_radius < 5)
ここで、各「サブクエリ」(つまり、質量 > 10) は、それに一致する要素のセットを返します。
次に、クエリが変換され、内部的に文字列に変換されます。この文字列はさらに評価され、一致した要素の一連のインデックスが生成されます。そのコンテキストでは、演算子 and/or はブール演算子ではなく、Python セットに作用するアンサンブル演算子です。
私は最近、そのようなクエリを作成するための投稿を送信しました。私が得た有用な回答のおかげで、多かれ少なかれ仕事をしたと思います (良い方法であるといいのですが!) が、パイパーシングに関連するいくつかの質問がまだあります。
これが私のコードです:
import numpy
from pyparsing import *
# This import a singleton object storing the datase dictionary as
# described earlier
from ElementsDatabase import ELEMENTS
and_operator = oneOf(['and','&'], caseless=True)
or_operator = oneOf(['or' ,'|'], caseless=True)
# ELEMENTS.properties is a property getter that returns the list of
# registered properties in the database
props = oneOf(ELEMENTS.properties, caseless=True)
# A property keyword can be quoted or not.
props = Suppress('"') + props + Suppress('"') | props
# When parsed, it must be replaced by the following expression that
# will be eval later.
props.setParseAction(lambda t : "numpy.array(ELEMENTS['%s'].values())" % t[0].lower())
quote = QuotedString('"')
integer = Regex(r'[+-]?\d+').setParseAction(lambda t:int(t[0]))
float_ = Regex(r'[+-]?(\d+(\.\d*)?)?([eE][+-]?\d+)?').setParseAction(lambda t:float(t[0]))
comparison_operator = oneOf(['==','!=','>','>=','<', '<='])
comparison_expr = props + comparison_operator + (quote | float_ | integer)
comparison_expr.setParseAction(lambda t : "set(numpy.where(%s)%s%s)" % tuple(t))
grammar = Combine(operatorPrecedence(comparison_expr, [(and_operator, 2, opAssoc.LEFT) (or_operator, 2, opAssoc.LEFT)]))
# A test query
res = grammar.parseString('"mass " > 30 or (nucleon == 1)',parseAll=True)
print eval(' '.join(res._asStringList()))
私の質問は次のとおりです。
1 using 'transformString' instead of 'parseString' never triggers any
exception even when the string to be parsed does not match the grammar.
However, it is exactly the functionnality I need. Is there is a way to do so ?
2 I would like to reintroduce white spaces between my tokens in order
that my eval does not fail. The only way I found to do so it the one
implemented above. Would you see a better way using pyparsing ?
長い投稿で申し訳ありませんが、そのコンテキストをより詳細に紹介したかった. ところで、このアプローチが悪いと思ったら、遠慮なく教えてください!
ご助力ありがとうございます。
エリック