Если вы хотите иметь больше гибкости в своем собственном синтаксисе, вот простой анализатор для приведенных вами определений данных:
data = """\
a = [-vegetable, +fruit, +apple, -orange, -citrus]
o = [-vegetable, +fruit, -apple, +orange, +citrus]
t = [+vegetable, -fruit]"""
from pyparsing import Word, alphas, oneOf, Group, delimitedList
# a basic token for a word of alpha characters plus underscores
ident = Word(alphas + '_')
# define a token for leading '+' or '-', with parse action to convert to bool value
inclFlag = oneOf('+ -')
inclFlag.setParseAction(lambda t: t[0] == '+')
# define a feature as the combination of an inclFlag and a feature name
feature = Group(inclFlag('has') + ident('feature'))
# define a definition
defn = ident('name') + '=' + '[' + delimitedList(feature)('features') + ']'
# search through the input test data for defns, and print out the parsed data
# by name, and the associated features
defns = defn.searchString(data)
for d in defns:
print d.dump()
for f in d.features:
print f.dump(' ')
print
Печать:
['a', '=', '[', [False, 'vegetable'], [True, 'fruit'], [True, 'apple'], [False, 'orange'], [False, 'citrus'], ']']
- features: [[False, 'vegetable'], [True, 'fruit'], [True, 'apple'], [False, 'orange'], [False, 'citrus']]
- name: a
[False, 'vegetable']
- feature: vegetable
- has: False
[True, 'fruit']
- feature: fruit
- has: True
[True, 'apple']
- feature: apple
- has: True
[False, 'orange']
- feature: orange
- has: False
[False, 'citrus']
- feature: citrus
- has: False
['o', '=', '[', [False, 'vegetable'], [True, 'fruit'], [False, 'apple'], [True, 'orange'], [True, 'citrus'], ']']
- features: [[False, 'vegetable'], [True, 'fruit'], [False, 'apple'], [True, 'orange'], [True, 'citrus']]
- name: o
[False, 'vegetable']
- feature: vegetable
- has: False
[True, 'fruit']
- feature: fruit
- has: True
[False, 'apple']
- feature: apple
- has: False
[True, 'orange']
- feature: orange
- has: True
[True, 'citrus']
- feature: citrus
- has: True
['t', '=', '[', [True, 'vegetable'], [False, 'fruit'], ']']
- features: [[True, 'vegetable'], [False, 'fruit']]
- name: t
[True, 'vegetable']
- feature: vegetable
- has: True
[False, 'fruit']
- feature: fruit
- has: False
Pyparsing выполняет за вас большую часть служебной информации, например, перебирает входную строку, пропускает ненужные пробелы и возвращает проанализированные данные с использованием именованных атрибутов. Посмотрите булевский оценщик на вики-сайте pyparsing (SimpleBool.py) или более полный пакет булевых оценщиков booleano.