本文整理汇总了Python中pyparsing.Word类的典型用法代码示例。如果您正苦于以下问题:Python Word类的具体用法?Python Word怎么用?Python Word使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Word类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, params):
self.name = ""
self.type = None
self.default_value = None
self.range = None
if isinstance(params, list):
self.name = params[0]
for attr in params[1:]:
if attr[0] == 'default':
self.default_value = attr[1]
elif attr[0] == 'type':
assert attr[1] == 'string' or attr[1] == 'integer'
self.type = attr[1]
elif attr[0] == 'range':
self.range = [attr[1], attr[2]]
if int(self.range[0]) >= int(self.range[1]):
raise ValueError('Invalid range')
if not self.default_value is None:
try:
# consistenza di tipo sui valori dello slot corrente
parser = Word(alphas) if self.type == 'string' else Word(nums)
parser.parseString(self.default_value)
# se slot corrente ha un range definito
if not self.range is None:
spec_range = '{0} >= {1} and {0} <= {2}'.format(self.default_value, self.range[0], self.range[1])
if not eval(spec_range):
raise ValueError('Default value doesn\'t satisfy range constraints')
except (ParseException, SyntaxError):
raise ValueError('Incorrect default value for slot')
else:
self.name = params
开发者ID:aleSuglia,项目名称:YAIEP,代码行数:33,代码来源:Slot.py
示例2: parse_sexp
def parse_sexp(data):
'''parse sexp/S-expression format and return a python list'''
# define punctuation literals
LPAR, RPAR, LBRK, RBRK, LBRC, RBRC, VBAR = map(Suppress, "()[]{}|")
decimal = Word("123456789", nums).setParseAction(lambda t: int(t[0]))
bytes = Word(printables)
raw = Group(decimal.setResultsName("len") + Suppress(":") + bytes).setParseAction(OtrPrivateKeys.verifyLen)
token = Word(alphanums + "-./_:*+=")
base64_ = Group(Optional(decimal, default=None).setResultsName("len") + VBAR
+ OneOrMore(Word( alphanums +"+/=" )).setParseAction(lambda t: b64decode("".join(t)))
+ VBAR).setParseAction(OtrPrivateKeys.verifyLen)
hexadecimal = ("#" + OneOrMore(Word(hexnums)) + "#")\
.setParseAction(lambda t: int("".join(t[1:-1]),16))
qString = Group(Optional(decimal, default=None).setResultsName("len") +
dblQuotedString.setParseAction(removeQuotes)).setParseAction(OtrPrivateKeys.verifyLen)
simpleString = raw | token | base64_ | hexadecimal | qString
display = LBRK + simpleString + RBRK
string_ = Optional(display) + simpleString
sexp = Forward()
sexpList = Group(LPAR + ZeroOrMore(sexp) + RPAR)
sexp << ( string_ | sexpList )
try:
sexpr = sexp.parseString(data)
return sexpr.asList()[0][1:]
except ParseFatalException, pfe:
print("Error:", pfe.msg)
print(pfe.loc)
print(pfe.markInputline())
开发者ID:a-u-me,项目名称:keysync,代码行数:33,代码来源:otr_private_key.py
示例3: read_stats
def read_stats(result_dir, stats_file_name):
stat_rule = Word(printables) + Word('nan.%' + nums) + Optional(restOfLine)
stats = []
try:
with open(path.join(result_dir, stats_file_name)) as stats_file:
i = 0
for stat_line in stats_file:
if len(stats) <= i:
stats.append(collections.OrderedDict())
try:
stat = stat_rule.parseString(stat_line)
key = stat[0]
value = stat[1]
stats[i][key] = value
except ParseException as e:
# print(e)
pass
if 'End Simulation Statistics' in stat_line:
i += 1
except Exception as e:
print(e)
return None
else:
return stats
开发者ID:ChengYanJin,项目名称:gem5,代码行数:29,代码来源:gem5_utils.py
示例4: __init__
def __init__(self):
# literals
star = Literal('*')
comma = Suppress(',')
# indentifiers
identifier = Word(alphas, alphanums+'_')
alias = identifier.copy()
# select clause
column_name = Combine(Optional(alias + '.') +
identifier +
Optional(' as ' + identifier))\
.setResultsName('column_name')
select = Keyword('select', caseless=1)
select_clause = (star | Group(delimitedList(column_name, comma)))\
.setResultsName('select_clause')
# from clause
from_ = Keyword('from', caseless=1)
table_name = delimitedList(identifier + Optional(alias), comma)
from_clause = table_name.setResultsName('from_clause')
# select statment
self.select_stmt = select + select_clause + from_ + from_clause
开发者ID:dlink,项目名称:sqlparser,代码行数:25,代码来源:sqlparser.py
示例5: _construct_grammar
def _construct_grammar():
logical_operator = get_logical_operator()
logical_expression = get_logical_expression()
facets_expression = get_facet_expression()
highlight_expression = get_highlight_expression()
sort_expression = get_sort_expression()
aggs_expression = get_aggregations_expression()
nested_expression = get_nested_expression()
# The below line describes how the type expression should be.
type_expression = Word('type')\
+ Word(':').suppress()\
+ Word(srange("[a-zA-Z0-9_]"))\
+ Optional(CaselessLiteral('AND')).suppress()
type_expression.setParseAction(parse_type_expression)
base_expression = Optional(highlight_expression)\
+ Optional(sort_expression)\
+ Optional(type_expression)\
+ ZeroOrMore(
(facets_expression
| aggs_expression
| nested_expression
| logical_expression)
+ Optional(logical_operator)
).setParseAction(parse_one_or_more_logical_expressions)
base_expression.setParseAction(parse_type_logical_facets_expression)
return base_expression
开发者ID:Aplopio,项目名称:plasticparser,代码行数:30,代码来源:tokenizer.py
示例6: set_param
def set_param(self,line):
# define grammar
a = line.find("]")
b = line.find("=")
if(a>0 and b>0 and (b-a)==1):
return
else:
modele = Word( alphas ) + "[" + Word(nums) + "]" + Word(objectPath) + "=" + Word( divers )
try:
pd = modele.parseString( line )
except ParseException as pe:
pass
else:
obj = pd[0]
key = pd[4]
value = pd[6][:len(pd[6])-1]
nb = int(pd[2])
if(key[0]=="."):
key = key[1:] #expect ".keyword"
if(key.find(".")<0): #a single keyword
if(key in ocd_str_param):
setattr(self,key,set_str(value))
#print("-> ocd[{id}].{key}={value}".format(id=self.id,key=key,value=value))
elif(key in ocd_int_param):
setattr(self,key,int(value))
#print("-> ocd[{id}].{key}={value}".format(id=self.id,key=key,value=value))
else:
keywords = key.split(".")
if(keywords[0]=="likelihood"):
self.likelihood.set_param(keywords,value)
elif(keywords[0]=="posterior"):
self.posterior.set_param(keywords,value)
开发者ID:AlephThot,项目名称:pyC14,代码行数:32,代码来源:pyC14.py
示例7: parse_morse_letter
def parse_morse_letter(line):
"""Parse a line of input from the morse code table file and convert it into a parsetree."""
letter = Word(alphas)
token = Word(alphas)
morse_letter_expression = letter.setResultsName('letter')\
+ OneOrMore(token).setResultsName('tokens')
return morse_letter_expression.parseString(line)
开发者ID:asgeirbjarnason,项目名称:arduino_morse_code,代码行数:7,代码来源:translation.py
示例8: open
def open(self, id):
self.id = id
item = 'BODY'
typ, data = self.imap.fetch(id, '(' + item + ')')
assert typ == 'OK'
inline_parse = Forward()
inline_parse << Suppress("(") + Group(OneOrMore(Or([Keyword("NIL"), QuotedString('"'), Word(nums), inline_parse ]))) + Suppress(")")
parse = Word(nums) + Suppress("(") + Keyword(item) + Suppress("(") + Group(OneOrMore(inline_parse)) + ZeroOrMore(Or([Keyword("NIL"), QuotedString('"'), Word(nums)])) + Suppress(")") + Suppress(")")
p = parse.parseString(data[0])
#print data[0]
#print p
#print
self.attachment = []
for i in p[2]:
#while 'NIL' in i:
# i.remove('NIL')
a = {
'type' : '/'.join(i[0:2]).lower(),
i[2][0].lower() : i[2][1],
}
self.attachment.append(a)
开发者ID:hoerger-old,项目名称:Kolab-Tools,代码行数:26,代码来源:imapparsing.py
示例9: nexus_iter
def nexus_iter(infile):
import pyparsing
pyparsing.ParserElement.enablePackrat()
from pyparsing import Word, Literal, QuotedString, CaselessKeyword, \
OneOrMore, Group, Optional, Suppress, Regex, Dict
## beginblock = Suppress(CaselessKeyword("begin") +
## CaselessKeyword("trees") + ";")
## endblock = Suppress((CaselessKeyword("end") |
## CaselessKeyword("endblock")) + ";")
comment = Optional(Suppress("[&") + Regex(r'[^]]+') + Suppress("]"))
## translate = CaselessKeyword("translate").suppress()
name = Word(string.letters+string.digits+"_.") | QuotedString("'")
## ttrec = Group(Word(string.digits).setResultsName("number") +
## name.setResultsName("name") +
## Optional(",").suppress())
## ttable = Group(translate + OneOrMore(ttrec) + Suppress(";"))
newick = Regex(r'[^;]+;')
tree = (CaselessKeyword("tree").suppress() +
Optional("*").suppress() +
name.setResultsName("tree_name") +
comment.setResultsName("tree_comment") +
Suppress("=") +
comment.setResultsName("root_comment") +
newick.setResultsName("newick"))
## treesblock = Group(beginblock +
## Optional(ttable.setResultsName("ttable")) +
## Group(OneOrMore(tree)) +
## endblock)
def not_begin(s): return s.strip().lower() != "begin trees;"
def not_end(s): return s.strip().lower() not in ("end;", "endblock;")
def parse_ttable(f):
ttable = {}
while True:
s = f.next().strip()
if not s: continue
if s.lower() == ";": break
if s[-1] == ",": s = s[:-1]
k, v = s.split()
ttable[k] = v
if s[-1] == ";": break
return ttable
# read lines between "begin trees;" and "end;"
f = itertools.takewhile(not_end, itertools.dropwhile(not_begin, infile))
s = f.next().strip().lower()
if s != "begin trees;":
print sys.stderr, "Expecting 'begin trees;', got %s" % s
raise StopIteration
ttable = {}
while True:
try: s = f.next().strip()
except StopIteration: break
if not s: continue
if s.lower() == "translate":
ttable = parse_ttable(f)
print "ttable: %s" % len(ttable)
elif s.split()[0].lower()=='tree':
match = tree.parseString(s)
yield nexus.Newick(match, ttable)
开发者ID:ChriZiegler,项目名称:cython-experiments-1,代码行数:60,代码来源:newick.py
示例10: parse_ampersand_comment
def parse_ampersand_comment(s):
import pyparsing
pyparsing.ParserElement.enablePackrat()
from pyparsing import Word, Literal, QuotedString, CaselessKeyword, \
OneOrMore, Group, Optional, Suppress, Regex, Dict
word = Word(string.letters+string.digits+"%_")
key = word.setResultsName("key") + Suppress("=")
single_value = (Word(string.letters+string.digits+"-.") |
QuotedString("'") |
QuotedString('"'))
range_value = Group(Suppress("{") +
single_value.setResultsName("min") +
Suppress(",") +
single_value.setResultsName("max") +
Suppress("}"))
pair = (key + (single_value | range_value).setResultsName("value"))
g = OneOrMore(pair)
d = []
for x in g.searchString(s):
v = x.value
if type(v) == str:
try: v = float(v)
except ValueError: pass
else:
try: v = map(float, v.asList())
except ValueError: pass
d.append((x.key, v))
return d
开发者ID:ChriZiegler,项目名称:cython-experiments-1,代码行数:28,代码来源:newick.py
示例11: __createGram
def __createGram(self):
if self.notNeedSpace:
lNot = Keyword(self.operators['not'])
else:
lNot = Literal(self.operators['not'])
lAnd = Literal(self.operators['and'])
lOr = Literal(self.operators['or'])
lImp = Literal(self.operators['impL'])
lEqu = Literal(self.operators['equ'])
lTrue = Keyword(self.constants['true'])
lFalse = Keyword(self.constants['false'])
lVar = Word(alphas, alphanums+'_')
lVar.setParseAction(self.ffactory.createLogicVariable)
lTrue.setParseAction(self.ffactory.createLogicTruth)
lFalse.setParseAction(self.ffactory.createLogicFalse)
factor = lTrue | lFalse | lVar
expression = myparsing.operatorPrecedence(factor,
[
(lNot, 1, opAssoc.RIGHT, self.ffactory.createNotOperation),
(lAnd, 2, opAssoc.LEFT, self.ffactory.createAndOperation),
(lOr, 2, opAssoc.LEFT, self.ffactory.createOrOperation),
(lImp, 2, opAssoc.LEFT, self.ffactory.createImpicationOperation),
(lEqu, 2, opAssoc.LEFT, self.ffactory.createEquvalenceOperation)
],
[('(', ')'), ('[', ']'), ('{', '}')])
self.final = expression + StringEnd()
开发者ID:pszynk,项目名称:LIProjekt,代码行数:35,代码来源:parsers.py
示例12: getLogLineBNF_DBpedia36
def getLogLineBNF_DBpedia36():
global logLineBNF
if logLineBNF is None:
integer = Word( nums )
ipAddress = delimitedList( integer, ".", combine=True )
hashipAddress = Word(nums+alphas)
timeZoneOffset = Word("+-",nums)
month = Word(string.uppercase, string.lowercase, exact=3)
serverDateTime = Group( Suppress("[") +
Combine( integer + "/" + month + "/" + integer +
" " + integer + ":" + integer + ":" + integer ) +
timeZoneOffset +
Suppress("]") )
logLineBNF = ( hashipAddress.setResultsName("ipAddr") +
Suppress("-") +
("-" | Word( alphas+nums+"@._" )).setResultsName("auth") +
serverDateTime.setResultsName("timestamp") +
dblQuotedString.setResultsName("cmd").setParseAction(getCmdFields2) +
(integer | "-").setResultsName("statusCode") +
(integer | "-").setResultsName("numBytesSent") +
dblQuotedString.setResultsName("referrer").setParseAction(removeQuotes) +
dblQuotedString.setResultsName("clientSfw").setParseAction(removeQuotes) )
return logLineBNF
开发者ID:imclab,项目名称:lod_analysis,代码行数:25,代码来源:parse_tools.py
示例13: init_parser
def init_parser(self):
INTEGER = Word(nums)
INTEGER.setParseAction(lambda x: int(x[0]))
header = INTEGER("species_count") + INTEGER("sequence_length") +\
Suppress(restOfLine)
header.setParseAction(self.set_header)
sequence_name = Word(
alphas + nums + "!#$%&\'*+-./;<=>[email protected][\\]^_`{|}~",
max=100)
# Take a copy and disallow line breaks in the bases
bases = self.BASES.copy()
bases.setWhitespaceChars(" \t")
seq_start = sequence_name("species") + bases(
"sequence") + Suppress(LineEnd())
seq_start.setParseAction(self.set_seq_start)
seq_start_block = OneOrMore(seq_start)
seq_start_block.setParseAction(self.set_start_block)
seq_continue = bases("sequence") + Suppress(LineEnd())
seq_continue.setParseAction(self.set_seq_continue)
seq_continue_block = Suppress(LineEnd()) + OneOrMore(seq_continue)
seq_continue_block.setParseAction(self.set_continue_block)
return header + seq_start_block + ZeroOrMore(seq_continue_block)
开发者ID:brettc,项目名称:tigger,代码行数:29,代码来源:alignment.py
示例14: func_tokens
def func_tokens(dictionary, parse_action):
func_name = Word(alphas+'_', alphanums+'_')
func_ident = Combine('$' + func_name.copy()('funcname'))
func_tok = func_ident + originalTextFor(nestedExpr())('args')
func_tok.leaveWhitespace()
func_tok.setParseAction(parse_action)
func_tok.enablePackrat()
rx_tok = Combine(Literal('$').suppress() + Word(nums)('num'))
def replace_token(tokens):
index = int(tokens.num)
return dictionary.get(index, u'')
rx_tok.setParseAction(replace_token)
strip = lambda s, l, tok: tok[0].strip()
text_tok = CharsNotIn(u',').setParseAction(strip)
quote_tok = QuotedString('"')
if dictionary:
arglist = Optional(delimitedList(quote_tok | rx_tok | text_tok))
else:
arglist = Optional(delimitedList(quote_tok | text_tok))
return func_tok, arglist, rx_tok
开发者ID:frnogueira,项目名称:puddletag,代码行数:27,代码来源:findfunc.py
示例15: load_variables
def load_variables(filename):
"""Load random variables definitions from file (in C45 format but with class at the end).
File must contain information in format 'Variable Name: Values.' as in the example below:
A: true,false.
B: 0,1,2.
C: c1,c2,c3,c4.
D: one.
"""
from DataStructures.randomvariables import RandomVariable
RV = []
# variable = Word(caps + lowers + digits).setResultsName("name") + ": " + OneOrMore(Group(Word(caps + lowers + digits) + Optional("." + Word(caps + lowers + digits))) + Suppress(Optional(","))).setResultsName("domain") + "."
variable = Word(caps + lowers + digits).setResultsName("name") + ": " + OneOrMore(Word(caps + lowers + digits + ".") + Optional(Suppress(","))).setResultsName("domain")
for line in file(filename):
if not line[0] == '#':
dataline = line[0:(len(line)-2)]
#print dataline
rv = variable.parseString(dataline)
#print rv.name
domain = []
for value in rv.domain:
#print value,
value = ''.join(value)
if value.isdigit():
#print 'lv#', value
domain.append(int(value))
else:
domain.append(value)
#print
var = RandomVariable(rv.name,domain)
RV.append(var)
return RV
开发者ID:izquierdo,项目名称:kr,代码行数:32,代码来源:io.py
示例16: make_parser
def make_parser(self):
g = self
lpar = Literal('(').suppress()
rpar = Literal(')').suppress()
colon = Literal(':').suppress()
delimiter = Literal(';').suppress()
unknown = Literal('?').setParseAction(lambda s,l,t: [0]) # ? -> number 0
number = Word(nums).setParseAction(map_int)
ident = Word(alphas+'_', alphas+nums+'_')
label_gen = (
ident + lpar + Optional(number) + rpar
).setParseAction(tokenize('LabelGen'))
label_def = (ident + colon).setParseAction(tokenize('LabelDef'), run(g.gen_label_def))
label_ref = ident.copy().setParseAction(tokenize('LabelRef'))
operand = number | label_gen | label_ref | unknown
instr = (ident + List(operand) + delimiter).setParseAction(tokenize('Instr'), run(g.gen_instr))
entry = instr | label_def
progr = List(entry).setParseAction(run(self.gen_end_progr))
return progr
开发者ID:institution,项目名称:tri,代码行数:30,代码来源:asm.py
示例17: parse
def parse(operator, string, digits=None):
"""Parse a valid interval from strings like '1800-1900' or '12'. If operator is one of
('<=', '>=', '>', '<'), only a single number will be parsed and an open interval will be constructed.
If the optional paramater *digits* is specified, only numbers having exactly this number of digits
will be considered.
"""
string = string.strip()
if digits is not None:
number = Word(pyparsing.nums, exact=digits)
else: number = Word(pyparsing.nums)
try:
if operator not in ('<=', '>=', '>', '<'):
# the ^ means xor. | does not work together with parseAll=True (bug in pyparsing?)
parser = number ^ (number + Suppress('-') + number)
result = parser.parseString(string, parseAll=True).asList()
result = [int(r) for r in result]
if len(result) == 1:
return Interval(result[0], result[0])
else: return Interval(result[0], result[1])
else:
result = number.parseString(string)
date = int(result[0])
if operator == '>=':
return Interval(date, None)
elif operator == '<=':
return Interval(None, date)
elif operator == '>':
return Interval(date+1, None)
elif operator == '<':
return Interval(None, date-1)
else: assert False
except pyparsing.ParseException:
return None
开发者ID:maestromusic,项目名称:maestro,代码行数:33,代码来源:criteria.py
示例18: parse_connection_str
def parse_connection_str(connstr):
## Grammar for connection syntax
digits="0123456789"
othervalid="[email protected]"
identifier= Word(alphas+digits+othervalid)
nodename=identifier.setResultsName('nodename')
outputnames = delimitedList( identifier ).setResultsName('outputnames')
inputnames = delimitedList( identifier ).setResultsName('inputnames')
# middle nodes have both inputs and outputs
middlenode= Group( nodename + Suppress('(') + inputnames
+ Optional( "|" + outputnames)
+ Suppress(")") ).setResultsName('middlenode')
# first node has only outputs
headnode = (nodename + Suppress("(") + outputnames
+ Suppress(")")).setResultsName('headnode')
# last node has only inputs
tailnode = (nodename + Suppress("(") + inputnames
+ Suppress(")")).setResultsName('tailnode')
# connect head -> [middle ->] tail
connect= Group( headnode
+ Group(ZeroOrMore(Suppress("->") \
+ middlenode + FollowedBy("->") )).setResultsName('middlenodes')
+ Suppress("->")+tailnode).setResultsName('nodes')
connectlist = Group( connect + ZeroOrMore( Suppress(";")\
+ connect )).setResultsName('connects')
parsed=connectlist.parseString(connstr)
check_numconnections(parsed)
return parsed
开发者ID:ihrke,项目名称:nipype_connect_str,代码行数:33,代码来源:nipype_connect_str.py
示例19: _define_grammar
def _define_grammar():
"""
Creates and returns a copy of the selector grammar.
Wrapped in a function to avoid polluting the module namespace.
"""
expr = Forward()
label_name = Word(LABEL_CHARS)
label_name.setParseAction(LabelNode)
string_literal = QuotedString('"') | QuotedString("'")
string_literal.setParseAction(LiteralNode)
set_literal = (Suppress("{") +
delimitedList(QuotedString('"') | QuotedString("'"), ",") +
Suppress("}"))
set_literal.setParseAction(SetLiteralNode)
eq_comparison = label_name + Suppress("==") + string_literal
eq_comparison.setParseAction(LabelToLiteralEqualityNode)
not_eq_comparison = label_name + Suppress("!=") + string_literal
not_eq_comparison.setParseAction(InequalityNode)
in_comparison = label_name + Suppress(Keyword("in")) + set_literal
in_comparison.setParseAction(LabelInSetLiteralNode)
not_in = Suppress(Keyword("not") + Keyword("in"))
not_in_comparison = label_name + not_in + set_literal
not_in_comparison.setParseAction(NotInNode)
has_check = (Suppress("has(") +
Word(LABEL_CHARS) +
Suppress(")"))
has_check.setParseAction(HasNode)
comparison = (eq_comparison |
not_eq_comparison |
in_comparison |
not_in_comparison |
has_check)
paren_expr = (Suppress("(") + expr + Suppress(")"))
value = comparison | paren_expr
and_expr = value + ZeroOrMore(Suppress("&&") + value)
and_expr.setParseAction(simplify_and_node)
or_expr = and_expr + ZeroOrMore(Suppress("||") + and_expr)
or_expr.setParseAction(simplify_or_node)
expr << or_expr
grammar = expr + StringEnd()
return grammar
开发者ID:ContainerSolutions,项目名称:calico,代码行数:57,代码来源:selectors.py
示例20: get_highlight_expression
def get_highlight_expression():
field_expression = Word(srange("[a-zA-Z0-9_.*]"))
field_expression.setParseAction(parse_highlight_field_expression)
fields_expression = OneOrMore(
field_expression + Optional(',').suppress())
fields_expression.setParseAction(parse_highlight_expression)
highlight_expression = Word('highlight:').suppress() \
+ Word('[').suppress() \
+ fields_expression + Word(']').suppress()
return highlight_expression
开发者ID:Aplopio,项目名称:plasticparser,代码行数:10,代码来源:tokenizer.py
注:本文中的pyparsing.Word类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论