本文整理汇总了Python中pyparsing.Word类的典型用法代码示例。如果您正苦于以下问题:Python Word类的具体用法?Python Word怎么用?Python Word使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Word类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
示例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())
示例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
示例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
示例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
示例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)
示例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)
示例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)
示例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)
示例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
示例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()
示例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
示例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)
示例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
示例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