我处于一种需要解析旧格式的情况。我想要做的是编写一个识别格式的解析器,并将其转换为更易于使用的对象。
我设法解析输入,问题是当我想将其转换回字符串时。总结一下:当我将parse()
的结果作为参数传递给compose()
方法时,它不会返回正确的字符串。
这是输出和源代码。我是钉子的初学者,有什么我误会的东西吗?注意,我的初始字符串中有(126000-147600,3);
,而在组成的字符串中,其前面带有-
。
输出:
********************************************************************************
-t gmt+1 -n GB_EN -p '39600-61200,0; (126000-147600,3); -(212400-234000,5); 298800; (320400); 385200-406800,0; 471600-493200,0; 558000-579600,0'
********************************************************************************
gmt+1 GB_EN
********************************************************************************
[{'end': '61200', 'interval': '0', 'start': '39600'},
{'end': '147600', 'interval': '3', 'start': '126000'},
{'end': '234000', 'interval': '5', 'inverted': True, 'start': '212400'},
{'start': '298800'},
{'start': '320400'},
{'end': '406800', 'interval': '0', 'start': '385200'},
{'end': '493200', 'interval': '0', 'start': '471600'},
{'end': '579600', 'interval': '0', 'start': '558000'}]
-t gmt+1 -n GB_EN -p '39600-61200,0; -(126000-147600,3); -(212400-234000,5); 298800; -(320400); 385200-406800,0; 471600-493200,0; 558000-579600,0'
Python源代码:
from pypeg2 import *
from pprint import pprint
Timezone = re.compile(r"(?i)gmt[\+\-]\d")
TimeValue = re.compile(r"[\d]+")
class ObjectSerializerMixin(object):
def get_as_object(self):
obj = {}
for attr in ['start', 'end', 'interval', 'inverted']:
if getattr(self, attr, None):
obj[attr] = getattr(self, attr)
return obj
class TimeFixed(str, ObjectSerializerMixin):
grammar = attr('start', TimeValue)
class TimePeriod(Namespace, ObjectSerializerMixin):
grammar = attr('start', TimeValue), '-', attr('end', TimeValue), ',', attr('interval', TimeValue)
class TimePeriodWrapped(Namespace, ObjectSerializerMixin):
grammar = flag("inverted", '-'), "(", attr('start', TimeValue), '-', attr('end', TimeValue), ',', attr('interval', TimeValue), ")"
class TimeFixedWrapped(Namespace, ObjectSerializerMixin):
grammar = flag("inverted", '-'), "(", attr('start', TimeValue), ")"
class TimeList(List):
grammar = csl([TimePeriod, TimeFixed, TimePeriodWrapped, TimeFixedWrapped], separator=";")
def __str__(self):
for a in self:
print(a.get_as_object())
return ''
class AlertExpression(List):
grammar = '-t', blank, attr('timezone', Timezone), blank, '-n', blank, attr('locale'), blank, "-p", optional(blank), "'", attr('timelist', TimeList), "'"
def get_time_objects(self):
for item in self.timelist:
yield item.get_as_object()
def __str__(self):
return '{} {}'.format(self.timezone, self.locale)
if __name__ == '__main__':
s="""-t gmt+1 -n GB_EN -p '39600-61200,0; (126000-147600,3); -(212400-234000,5); 298800; (320400); 385200-406800,0; 471600-493200,0; 558000-579600,0'"""
p = parse(s, AlertExpression)
print("*"*80)
print(s)
print("*"*80)
print(p)
print("*"*80)
pprint(list(p.get_time_objects()))
print(compose(p))
最佳答案
我很确定这是pypeg2
中的错误
您可以使用pypeg2示例given here的简化版本进行验证,但使用与您使用的值类似的值:
>>>from pypeg2 import *
>>> class AddNegation:
... grammar = flag("inverted",'-'), blank, "(1000-5000,3)"
...
>>> t = AddNegation()
>>> t.inverted = False
>>> compose(t)
'- (1000-5000,3)'
>>> t.inverted = True
>>> compose(t)
'- (1000-5000,3)'
这用一个最小的示例证明了标志变量(
inverted
)的值对合成没有影响。正如您自己发现的那样,您的parse
可以根据需要工作。我快速浏览了代码和this is where the compose is。该模块全部写在一个
__init__.py
文件中,并且此函数是递归的。据我所知,问题是当标志为False时,-
对象仍作为str
类型传递到compose(在递归的最底层),并简单地添加到组合字符串。更新将错误隔离到here(1406),该错误会错误地解包flag属性,并且会将字符串
'-'
发送回compose()
并将其附加为属性的任何值(类型为bool
的字符串)。一个部分解决方法是用与上述条款相似的
text.append(self.compose(thing, g))
替换该行(因此,将Attribute
类型与从元组中拔出后通常对待它们的方式相同),但是然后在可选的地方单击this line属性(标志只是Attribute
类型的特例)在对象中缺失的地方不能正确组合。作为解决方法,您可以转到同一文件的第1350行并替换
if grammar.subtype == "Flag":
if getattr(thing, grammar.name):
result = self.compose(thing, grammar.thing, attr_of=thing)
else:
result = terminal_indent()
与
if grammar.subtype == "Flag":
try:
if getattr(thing, grammar.name):
result = self.compose(thing, grammar.thing, attr_of=thing)
else:
result = terminal_indent()
except AttributeError:
#if attribute error missing, insert nothing
result = terminal_indent()
我不确定这是否是完全可靠的修复程序,但可以通过变通方法解决问题
输出量
通过将这两种解决方法/修复程序应用于
pypeg2
模块文件,从print(compose(p))
获得的输出为-t gmt+1 -n GB_EN -p '39600-61200,0; (126000-147600,3); -(212400-234000,5); 298800; (320400); 385200-406800,0; 471600-493200,0; 558000-579600,0'
根据需要,您可以继续使用
pypeg2
模块。