本文整理汇总了Python中pyparsing.OneOrMore.parseFile方法的典型用法代码示例。如果您正苦于以下问题:Python OneOrMore.parseFile方法的具体用法?Python OneOrMore.parseFile怎么用?Python OneOrMore.parseFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing.OneOrMore
的用法示例。
在下文中一共展示了OneOrMore.parseFile方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
def parse(self, file_path):
string = Word(printables, excludeChars="|?,:").setParseAction(self._remove_dots)
comment = "|" + restOfLine
delimiter = Suppress(".") | LineEnd()
enum = (Empty().copy().setParseAction(lambda t: self.ENUM) +
Group(delimitedList(string))("next_arg").setParseAction(lambda t: {'values': t.next_arg.asList()}))
discrete = Literal("discrete") + Suppress(Word(nums))
attr_type = (Literal("continuous") | Literal("ignore") | discrete | enum)("type")
attribute = string("name") + Suppress(":") + attr_type
cls = string("cls")
cls.addParseAction(self._get_class)
classes = delimitedList(cls)
entry = attribute | classes
attribute.setParseAction(self._create_attribute)
parser = OneOrMore(entry + Optional(delimiter))
parser.ignore(comment)
try:
parser.parseFile(file_path, parseAll=True)
except ParseException as e:
raise HeaderError(FileType.DATA, e.lineno, e.col, e.line, e)
示例2: manyXYZ
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
def manyXYZ(pathXYZ):
"""
Read one or more molecular geometries in XYZ format from a file.
:param: pathXYZ
:type: string
:return: [[AtomXYZ]]
"""
manyMol = OneOrMore(Group(parser_xyz))
xss = manyMol.parseFile(pathXYZ)
return list(map(createAtoms, xss))
示例3: dict_from_key_values_pyparsing
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
def dict_from_key_values_pyparsing(file, lowercase_keys=False):
"""
>>> filename = os.path.dirname(__file__)
>>> filename = os.path.join(filename, "data/iradio-initial.pls")
>>> with open(filename, "rt", encoding="utf8") as fh:
... d = dict_from_key_values_pyparsing(fh)
>>> for key in sorted(d.keys())[-4:]:
... print("{0}: {1}".format(key, d[key]))
title6: Virgin Xtreme (Broadband)
title7: Virgin Classic Rock (Modem)
title8: Virgin Classic Rock (Broadband)
title9: CBC Radio One (Canada)
>>> d["file13"]
'http://media.hiof.no/streams/m3u/nrk-petre-172.ogg.m3u'
>>> d["genre15"]
''
>>> len(d.keys())
54
"""
def accumulate(tokens):
key, value = tokens
key = key.lower() if lowercase_keys else key
key_values[key] = value
key_values = {}
left_bracket, right_bracket, equals = map(Suppress, "[]=")
ini_header = left_bracket + CharsNotIn("]") + right_bracket
key_value = Word(alphanums) + equals + restOfLine
key_value.setParseAction(accumulate)
comment = "#" + restOfLine
parser = OneOrMore(ini_header | key_value)
parser.ignore(comment)
try:
parser.parseFile(file)
except ParseException as err:
print("parse error: {0}".format(err))
return {}
return key_values
示例4: Forward
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
# Our expr:
expr = Forward()
expr << Group((lparen + symbol + expr + rparen) | (lparen + symbol + expr + expr + rparen) | (lparen + symbol + expr + expr + expr + rparen) | symbol | bitvec)
# Pairs of results for the output.
outpair = Group(lparen + lparen + expr + expr + rparen + rparen)
everything = OneOrMore(outpair)
# Read /all the things/
output_values = dict()
# We know it's qfaufbv in testing.
smt_eater = QFAUFBV()
foo = everything.parseFile(sys.stdin)
for assignment in foo:
paren1, paren2, expr1, expr2, paren3, paren4 = assignment
# expr1 is the source name, expr2 the actual value.
# Do some things
round, match, slot = smt_eater.read_variable(expr1)
team_no = read_assign(expr2)
output_values[round, match, slot] = team_no
# Print all the things.
for i in range(NUMROUNDS):
for j in range(NUMMATCHES):
something = []
for k in range(NUMSLOTS):
something.append(output_values[i, j, k])
print "{0}|{1}|{2}|{3}".format(something[0], something[1], something[2], something[3])
示例5: Group
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
| "PRIORITY" + COLON + propval.setResultsName("priority") \
| "STATUS" + COLON + propval.setResultsName("status") \
| "SUMMARY" + COLON + propval.setResultsName("summary") \
| "URL" + COLON + propval.setResultsName("url") \
calprops = Group(OneOrMore(calprop)).suppress()
evtprops = Group(OneOrMore(evtprop))
almprops = Group(OneOrMore(almprop)).suppress()
alarm = BEGIN + ALARM + almprops + END + ALARM
event = BEGIN + EVENT + evtprops + Optional(alarm) + END + EVENT
events = Group(OneOrMore(event))
calendar = BEGIN + CALENDAR + calprops + ZeroOrMore(event) + END + CALENDAR
calendars = OneOrMore(calendar)
# PARSE ACTIONS
def gotEvent(s,loc,toks):
for event in toks:
print (event['summary'], "from", event["begin"], "to", event["end"])
event.setParseAction(gotEvent)
# MAIN PROGRAM
if __name__=="__main__":
calendars.parseFile("mozilla.ics")
示例6: read_gmsh
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
def read_gmsh(filename):
from pyparsing import Word, Optional, nums, Combine, Literal, \
CaselessLiteral, Group, OneOrMore, StringEnd, restOfLine, \
ParseException, alphanums, Keyword, ZeroOrMore
e = CaselessLiteral("E")
inum = Word("+-"+nums)
fnum = Combine(
Word( "+-"+nums, nums ) + Optional("."+Optional(Word(nums))) +
Optional(e+Word("+-"+nums,nums))
)
semi = Literal(";").suppress()
colon = Literal(",").suppress()
lpar = Literal("(").suppress()
rpar = Literal(")").suppress()
lbrace = Literal("{").suppress()
rbrace = Literal("}").suppress()
eq = Literal("=").suppress()
point = Group(
Keyword("Point")+lpar+inum+rpar+eq+
Group(lbrace+fnum+colon+fnum+colon+fnum+colon+fnum+rbrace)+semi
)
line = Group(
Keyword("Line")+lpar+inum+rpar+eq+
Group(lbrace+inum+colon+inum+rbrace)+semi
)
lineloop = Group(
Keyword("Line Loop")+lpar+inum+rpar+eq+
Group(lbrace+inum+OneOrMore(colon+inum)+rbrace)+semi
)
circle = Group(
Keyword("Circle")+lpar+inum+rpar+eq+
Group(lbrace+inum+colon+inum+colon+inum+rbrace)+semi
)
planesurface = Group(
Keyword("Plane Surface")+lpar+inum+rpar+eq+
Group(lbrace+inum+rbrace)+semi
)
ruledsurface = Group(
Keyword("Ruled Surface")+lpar+inum+rpar+eq+
Group(lbrace+inum+rbrace)+semi
)
surfaceloop = Group(
Keyword("Surface Loop")+lpar+inum+rpar+eq+
Group(lbrace+inum+OneOrMore(colon+inum)+rbrace)+semi
)
volume = Group(
Keyword("Volume")+lpar+inum+rpar+eq+
Group(lbrace+inum+rbrace)+semi
)
physicalsurface = Group(
Keyword("Physical Surface")+lpar+inum+rpar+eq+
Group(lbrace+inum+ZeroOrMore(colon+inum)+rbrace)+semi
)
physicalvolume = Group(
Keyword("Physical Volume")+lpar+inum+rpar+eq+
Group(lbrace+inum+ZeroOrMore(colon+inum)+rbrace)+semi
)
skip1 = Group(
Word(alphanums)+eq+fnum+semi
)
comment = Group( Literal("//")+restOfLine).suppress()
command = point | line | lineloop | circle | planesurface | ruledsurface | \
surfaceloop | volume | physicalsurface | physicalvolume | comment \
| skip1
grammar= OneOrMore(command)+StringEnd()
try:
tokens= grammar.parseFile(filename)
except ParseException, err:
print err.line
print " "*(err.column-1) + "^"
print err
raise err
示例7: Word
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
create_table_def.setParseAction(create_table_act)
add_fkey_def=Literal("ALTER")+"TABLE"+"ONLY" + Word(alphanums+"_").setResultsName("tableName") + "ADD" + "CONSTRAINT" + Word(alphanums+"_") + "FOREIGN"+"KEY"+"("+Word(alphanums+"_").setResultsName("keyName")+")" +"REFERENCES"+Word(alphanums+"_").setResultsName("fkTable")+"("+Word(alphanums+"_").setResultsName("fkCol")+")"+";"
def add_fkey_act(s,loc,tok):
return """ "%(tableName)s":%(keyName)s -> "%(fkTable)s":%(fkCol)s """ % tok
add_fkey_def.setParseAction(add_fkey_act)
other_statement_def = ( OneOrMore(CharsNotIn(";") ) + ";")
def other_statement_act(s,loc,tok):
return ""
other_statement_def.setParseAction(other_statement_act)
comment_def = "--" + ZeroOrMore(CharsNotIn("\n"))
comment_def.setParseAction(other_statement_act)
statement_def = comment_def | create_table_def | add_fkey_def | other_statement_def
defs = OneOrMore(statement_def)
print """digraph g { graph [ rankdir = "LR" ]; """
for i in defs.parseFile("dump.sql"):
if i!="":
print i
print "}"
示例8: from_gmsh_file
# 需要导入模块: from pyparsing import OneOrMore [as 别名]
# 或者: from pyparsing.OneOrMore import parseFile [as 别名]
def from_gmsh_file(filename):
"""
Import geometry - Gmsh geometry format.
Parameters
----------
filename : string
file name
Returns
-------
geo : geometry
geometry description
"""
from pyparsing import Word, Optional, nums, Combine, Literal, \
CaselessLiteral, Group, OneOrMore, StringEnd, restOfLine, \
ParseException, alphanums, Keyword, ZeroOrMore
e = CaselessLiteral("E")
inum = Word("+-"+nums)
fnum = Combine(
Word( "+-"+nums, nums ) + Optional("."+Optional(Word(nums))) +
Optional(e+Word("+-"+nums,nums))
)
semi = Literal(";").suppress()
colon = Literal(",").suppress()
lpar = Literal("(").suppress()
rpar = Literal(")").suppress()
lbrace = Literal("{").suppress()
rbrace = Literal("}").suppress()
eq = Literal("=").suppress()
point = Group(
Keyword("Point")+lpar+inum+rpar+eq+
Group(lbrace+fnum+colon+fnum+colon+fnum+colon+fnum+rbrace)+semi
)
line = Group(
Keyword("Line")+lpar+inum+rpar+eq+
Group(lbrace+inum+colon+inum+rbrace)+semi
)
lineloop = Group(
Keyword("Line Loop")+lpar+inum+rpar+eq+
Group(lbrace+inum+OneOrMore(colon+inum)+rbrace)+semi
)
circle = Group(
Keyword("Circle")+lpar+inum+rpar+eq+
Group(lbrace+inum+colon+inum+colon+inum+rbrace)+semi
)
planesurface = Group(
Keyword("Plane Surface")+lpar+inum+rpar+eq+
Group(lbrace+inum+rbrace)+semi
)
ruledsurface = Group(
Keyword("Ruled Surface")+lpar+inum+rpar+eq+
Group(lbrace+inum+rbrace)+semi
)
surfaceloop = Group(
Keyword("Surface Loop")+lpar+inum+rpar+eq+
Group(lbrace+inum+OneOrMore(colon+inum)+rbrace)+semi
)
volume = Group(
Keyword("Volume")+lpar+inum+rpar+eq+
Group(lbrace+inum+rbrace)+semi
)
physicalsurface = Group(
Keyword("Physical Surface")+lpar+inum+rpar+eq+
Group(lbrace+inum+ZeroOrMore(colon+inum)+rbrace)+semi
)
physicalvolume = Group(
Keyword("Physical Volume")+lpar+inum+rpar+eq+
Group(lbrace+inum+ZeroOrMore(colon+inum)+rbrace)+semi
)
skip1 = Group(
Word(alphanums)+eq+fnum+semi
)
comment = Group( Literal("//")+restOfLine).suppress()
command = point | line | lineloop | circle | planesurface | ruledsurface | \
surfaceloop | volume | physicalsurface | physicalvolume | comment \
| skip1
grammar= OneOrMore(command)+StringEnd()
try:
tokens= grammar.parseFile(filename)
except ParseException as err:
print(err.line)
print(" "*(err.column-1) + "^")
print(err)
raise err
lineloops={}
surfaceloops={}
geo=geometry()
for x in tokens:
if x[0]=="Point":
#.........这里部分代码省略.........