本文整理汇总了Python中pyparsing.nestedExpr函数的典型用法代码示例。如果您正苦于以下问题:Python nestedExpr函数的具体用法?Python nestedExpr怎么用?Python nestedExpr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了nestedExpr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_string
def parse_string(self, string):
'''Populate a new object from a string.
Parsing is hard, so we're going to call out to the pyparsing
library here. I hope you installed it!
FTR: this is hideous.
'''
from pyparsing import Suppress, Regex, quotedString, restOfLine, Keyword, nestedExpr, Group, OneOrMore, Word, Literal, alphanums, removeQuotes, replaceWith, nums, printables
gr_eq = Literal('=')
gr_stripped_string = quotedString.copy().setParseAction( removeQuotes )
gr_opt_quoted_string = gr_stripped_string | restOfLine
gr_number = Word(nums)
gr_yn = Keyword('yes', caseless=True).setParseAction(replaceWith('1')) | Keyword('no', caseless=True).setParseAction(replaceWith('0'))
def _handle_ip(*x):
a,b,c = x[2]
return ' %s = { %s }' % (a,c[0])
def _handle_diraddr(*x):
a,b,c = x[2]
self._set(DIRADDRESSES, ' %s' % '\n '.join(c))
return
def np(words, fn = gr_opt_quoted_string, action=None):
p = Keyword(words[0], caseless=True)
for w in words[1:]:
p = p | Keyword(w, caseless=True)
p = p + gr_eq + fn
p.setParseAction(action)
return p
gr_name = np((NAME,), action=lambda x: self._set_name(x[2]))
gr_address = np((ADDRESS,), action=self._parse_setter(ADDRESS))
gr_fd_conn = np(PList('fd connect timeout'), gr_number, self._parse_setter(FD_CONNECT_TIMEOUT, True))
gr_heart = np(PList('heartbeat interval'), gr_number, self._parse_setter(HEARTBEATINTERVAL, True))
gr_max_con = np(PList('maximum console connections'),
gr_number, self._parse_setter(MAXIMUMCONSOLECONNECTIONS, True))
gr_max_jobs = np(PList('maximum concurrent jobs'), gr_number, action=self._parse_setter(MAXIMUMCONCURRENTJOBS, True))
gr_pass = np((PASSWORD,), action=self._parse_setter(PASSWORD))
gr_pid = np(PList('pid directory'), action=self._parse_setter(PIDDIRECTORY))
gr_query = np(PList('query file'), action=self._parse_setter(QUERYFILE))
gr_scripts = np(PList('scripts directory'), action=self._parse_setter(SCRIPTS_DIRECTORY))
gr_sd_conn = np(PList('sd connect timeout'), gr_number, self._parse_setter(SD_CONNECT_TIMEOUT, True))
gr_source = np(PList('source address'), action=self._parse_setter(SOURCEADDRESS))
gr_stats = np(PList('statistics retention'), action=self._parse_setter(STATISTICS_RETENTION))
gr_verid = np((VERID,), action=self._parse_setter(VERID))
gr_messages = np((MESSAGES,), action=lambda x:self._parse_setter(MESSAGE_ID, dereference=True))
gr_work_dir = np(PList('working directory'), action=self._parse_setter(WORKINGDIRECTORY))
gr_port = np(PList('dir port'), gr_number, self._parse_setter(PORT, True))
gr_monitor = np((MONITOR,), gr_yn, action=self._parse_setter(MONITOR))
# This is a complicated one
da_addr = np(('Addr','Port'), Word(printables), lambda x,y,z: ' '.join(z))
da_ip = np(('IPv4','IPv6','IP'), nestedExpr('{','}', OneOrMore(da_addr).setParseAction(lambda x,y,z: ' ; '.join(z)))).setParseAction(_handle_ip)
da_addresses = np(PList('dir addresses'), nestedExpr('{','}', OneOrMore(da_ip)), _handle_diraddr)
gr_res = OneOrMore(gr_name | gr_address | gr_fd_conn | gr_heart | gr_max_con | gr_max_jobs | gr_pass | gr_pid | gr_query | gr_scripts | gr_sd_conn | gr_source | gr_stats | gr_verid | gr_messages | gr_work_dir | gr_port | gr_monitor | da_addresses)
result = gr_res.parseString(string, parseAll=True)
return 'Director: ' + self[NAME]
示例2: parse_string
def parse_string(self, string):
'''Populate a new object from a string.
Parsing is hard, so we're going to call out to the pyparsing
library here. I hope you installed it!
'''
from pyparsing import quotedString, restOfLine, Keyword, nestedExpr, OneOrMore, Word, Literal, removeQuotes, nums, replaceWith, printables
gr_eq = Literal('=')
gr_stripped_string = quotedString.copy().setParseAction( removeQuotes )
gr_opt_quoted_string = gr_stripped_string | restOfLine
gr_number = Word(nums)
gr_yn = Keyword('yes', caseless=True).setParseAction(replaceWith('1')) | Keyword('no', caseless=True).setParseAction(replaceWith('0'))
def _handle_ip(*x):
a,b,c = x[2]
return ' %s = { %s }' % (a,c[0])
def _handle_fdaddr(*x):
a,b,c = x[2]
self._set(FDADDRESSES, ' %s' % '\n '.join(c))
return
def np(words, fn = gr_opt_quoted_string, action=None):
p = Keyword(words[0], caseless=True)
for w in words[1:]:
p = p | Keyword(w, caseless=True)
p = p + gr_eq + fn
p.setParseAction(action)
return p
gr_line = np((NAME,), action=lambda x: self._set_name(x[2]))
gr_line = gr_line | np((ADDRESS,), action=self._parse_setter(ADDRESS))
gr_line = gr_line | np((CATALOG,), action=self._parse_setter(CATALOG_ID, dereference=True))
gr_line = gr_line | np((PASSWORD,), action=self._parse_setter(PASSWORD))
gr_line = gr_line | np(PList('file retention'), action=self._parse_setter(FILERETENTION))
gr_line = gr_line | np(PList('job retention'), action=self._parse_setter(JOBRETENTION))
gr_line = gr_line | np((PRIORITY,), gr_number, action=self._parse_setter(PRIORITY))
gr_line = gr_line | np(PList('working directory'), action=self._parse_setter(WORKINGDIRECTORY))
gr_line = gr_line | np(PList('pid directory'), action=self._parse_setter(PIDDIRECTORY))
gr_line = gr_line | np(PList('heart beat interval'), action=self._parse_setter(HEARTBEATINTERVAL))
gr_line = gr_line | np(PList('fd address'), action=self._parse_setter(FDADDRESS))
gr_line = gr_line | np(PList('fd source address'), action=self._parse_setter(FDSOURCEADDRESS))
gr_line = gr_line | np(PList('pki key pair'), action=self._parse_setter(PKIKEYPAIR))
gr_line = gr_line | np(PList('pki master key'), action=self._parse_setter(PKIMASTERKEY))
gr_line = gr_line | np(PList('fd port'), gr_number, action=self._parse_setter(FDPORT))
gr_line = gr_line | np(PList('auto prune'), gr_yn, action=self._parse_setter(AUTOPRUNE))
gr_line = gr_line | np(PList('maximum concurrent jobs'), gr_number, action=self._parse_setter(FDPORT))
gr_line = gr_line | np(PList('pki encryption'), gr_yn, action=self._parse_setter(PKIENCRYPTION))
gr_line = gr_line | np(PList('pki signatures'), gr_yn, action=self._parse_setter(PKISIGNATURES))
# This is a complicated one
da_addr = np(('Addr','Port'), Word(printables), lambda x,y,z: ' '.join(z))
da_ip = np(('IPv4','IPv6','IP'), nestedExpr('{','}', OneOrMore(da_addr).setParseAction(lambda x,y,z: ' ; '.join(z)))).setParseAction(_handle_ip)
da_addresses = np(('fd addresses', FDADDRESSES), nestedExpr('{','}', OneOrMore(da_ip)), _handle_fdaddr)
gr_res = OneOrMore(gr_line|da_addresses)
result = gr_res.parseString(string, parseAll=True)
return 'Client: ' + self[NAME]
示例3: __init__
def __init__(self):
# supported operators
operator = pp.Regex(r"<=|>=|<>|\!=|==|<|>|not|in|regex_partial|regex_exact|geo_box|geo_radius|geo_polygon|contains_any|substr|contains_near|any|contains_substr|near|contains").setName("operator").addParseAction(self.validateOperator)
# literals
number = pp.Regex(r"[+-]?\d+(:?\.\d*)?(:?[eE][+-]?\d+)?").setName("number")
numberList = pp.Group(pp.Literal('[') + number + pp.ZeroOrMore("," + number) + pp.Literal(']')).setName("numberList")
string = pp.dblQuotedString
literals = number | numberList | string
# symbols
identifier = pp.Regex(r"[a-z][a-z_]+(?:\.[a-z][a-z_]+)+").addParseAction(self.validateIdentifier).setName("identifier")
# we'll get there...
subExpr = pp.Forward()
# predicates
stream = pp.Group(pp.Literal("stream") + string).setName("stream")
exists = pp.Group(identifier + pp.Literal("exists")).setName("exists")
# boolean predicates
comparison = pp.Group(
identifier + operator + literals
| literals + operator + identifier
).setName("comparison")
condition = comparison | stream | exists | subExpr
subExpr << pp.nestedExpr(content=condition)
# standard boolean operator precedence
expr = pp.operatorPrecedence(condition,[
(pp.CaselessLiteral("not"), 1, pp.opAssoc.RIGHT, ),
(pp.CaselessLiteral("AND"), 2, pp.opAssoc.LEFT, ),
(pp.CaselessLiteral("OR"), 2, pp.opAssoc.LEFT, ),
])
# tag "thing" { expr }
tag = pp.Group(pp.Literal("tag") + pp.quotedString + pp.nestedExpr("{", "}", expr)).setName("tag")
# return { expr }
a_return = pp.Group(pp.Literal("return") + pp.nestedExpr("{", "}", expr)).setName("return")
# a single expression or tag [, tag, ...] return { expression }
parser = expr | (pp.OneOrMore(tag) + a_return)
# handle multilines
parser.setDefaultWhitespaceChars(" \t\n\r")
# handle // comments
parser.ignore("//" + pp.restOfLine)
self.parser = parser
示例4: removeComments
def removeComments(string):
"""Remove all comments from string"""
if __strip_nested__:
print "Removing nested comments...\nIf you are sure that you don't have nested comments, set \"__strip_nested__ = False\"\nIt will be faster."
print "Warning: You can still have problems if you have unmatched comment delimiters inside literal strings.\n\n"
comment1 = pyparsing.nestedExpr("(*", "*)").suppress()
comment2 = pyparsing.nestedExpr("{", "}").suppress()
string = comment1.transformString(string)
string = comment2.transformString(string)
else:
print "Warning! Removing simple comments...\nIf you have nested comments set \"__strip_nested__ = True\" and re-run."
string = re.sub(re.compile("\(\*.*?\*\)",re.DOTALL ) ,"" ,string)
string = re.sub(re.compile("\{.*?\}",re.DOTALL ) ,"" ,string)
return string
示例5: evaluateBV
def evaluateBV(s):
l=nestedExpr('(',')').parseString(s).asList()
#print(l)
e=nlToS(l)
return e
示例6: read_sets_java
def read_sets_java(string):
from pyparsing import nestedExpr, alphas, Word, nums, ParserElement, delimitedList
ParserElement.setDefaultWhitespaceChars(" ,")
element = Word(alphas + nums).setParseAction(parse_elem_java)
elements = delimitedList(element)
setofsets = nestedExpr("[", "]", content=elements).setParseAction(lambda x: frozenset(x[0]))
return setofsets.parseString(string).asList()[0]
示例7: parse
def parse(self, input_string):
# check for unclosed quotes/parentheses
paired_exprs = nestedExpr('(', ')') | quotedString
stripped_line = paired_exprs.suppress().transformString(input_string)
error_dictsql = {
'operator': None,
'val1': None,
'val2': None,
'interpretation': {
'interpretation': None,
'string': input_string,
'attribute': 'text',
'operator': None,
'error': True,
'error_message': None
}
}
if '"' in stripped_line or "'" in stripped_line:
error_dictsql['interpretation']['error_message'] = 'unclosed quote'
return False, error_dictsql
if '(' in stripped_line or ')' in stripped_line:
error_dictsql['interpretation']['error_message'] = 'unclosed parentheses'
return False, error_dictsql
ast = self._string_to_ast(input_string)
return self._ast_to_dictsql(ast)
示例8: load_config
def load_config(filename):
assert(isinstance(filename, str))
text = ""
try:
f = open(filename)
except IOError:
print(std.strerr, "Cannot open {}".format(filename))
sys.exit(1)
else:
text = f.read()
f.close()
# Remove c comments /* ... */
ccomments = pp.nestedExpr("/*", "*/").suppress()
text = ccomments.transformString(text)
# Fixme: The regex substitution wrongly uncomments global occurences of
# 'sync_trash_ttl'. This may lead to problems reading the json file in case
# multiple global occurences of 'sync_trash_ttl' exists. It may also
# trigger an incorrect warning in the function test_config()!
# Uncomment //"sync_trash_ttl" : x"
text = re.sub(r'/{2,}\s*("sync_trash_ttl"\s+:\s+[0-9]+)','\g<1>',text)
# Remove c++ comments // ...
cppcomments = pp.cppStyleComment.suppress()
text = cppcomments.transformString(text)
# Return config as dict
return json.loads(text)
示例9: handle
def handle(self):
self.data = self.rfile.readline().strip()
logger.info('-----------------------------------')
logger.info('%s wrote: %s' % (self.client_address[0], self.data))
data = OneOrMore(nestedExpr()).parseString(self.data)[0]
if data[0] != 'get-annotation':
self.wfile.write('ERROR: method "%s" not supported' % data[0])
elif len(data) != 3:
self.wfile.write('ERROR: badly formatted request')
else:
object = data[1][1:-1]
sentence = data[2][1:-1]
try:
logging.info("Sentence: %s" % sentence)
a = annotation.annotate(sentence, object)
logging.info("Annotation: %s" % a.join_tokens())
self.wfile.write(a.join_tokens() + "\n")
except annotation.ObjectNotFoundException as e:
logging.exception(e)
self.wfile.write('ERROR: %s' % e)
except annotation.ObjectSymbolNotFoundException as e:
logging.exception(e)
self.wfile.write('ERROR: %s' % e)
logger.info('-----------------------------------')
示例10: output
def output(line):
txt = line.strip()
parse = nestedExpr('(',')').parseString(txt).asList()[0]
#print "\n\n"; pprint.pprint(parse)
depstruct = depgen(parse, None, object())
#pprint.pprint(depstruct)
parents = [x[2] for x in depstruct]
ids = [None] + [x[1] for x in depstruct]
try:
deps = [ids.index(p) for p in parents]
except:
pp.pprint(p)
pp.pprint(ids)
raise
#assert deps[1::2]==deps[::2], deps
#deps = [(d+1)/2 for d in deps[::2]]
for i in range(0, len(deps)):
if deps[i] > 0:
deps[i] = (deps[i]+1)/2
print ' '.join(map(str, deps[::2]))
"""
示例11: _parse_get_symbols_response
def _parse_get_symbols_response(response):
if response[:2] == '()':
return None
# some responses are HUGE ... we truncate them
if len(response) > 0 and response[-2:] != "))":
last_occurence = response.rfind(':priority 0)')
response = response[:last_occurence + len(':priority 0)')] + ")"
parsed_response = []
try:
data = OneOrMore(nestedExpr()).parseString(response)
except:
raise OmnibaseParseException("Could not parse %s" % response)
if data[0][0] == "error":
raise OmnibaseException(response)
for d in data[0]:
r = {}
r['class'] = d[0]
r['match'] = d[1].replace('"', '')
r['span'] = (int(d[3][0]), int(d[3][1]))
r['symbol'] = d[5].replace('"', '')
parsed_response.append(r)
return parsed_response
示例12: cleanAnnotation
def cleanAnnotation(filepath):
"""Clean out the obsolete or superflous annotations."""
with open(filepath, 'r') as mo_file:
string = mo_file.read()
# remove 'Window(),' and 'Coordsys()' annotations:
WindowRef = ZeroOrMore(White(' \t')) + (Keyword('Window')|Keyword('Coordsys')) + nestedExpr() + ',' + ZeroOrMore(White(' \t') + lineEnd)
out = Suppress(WindowRef).transformString(string)
# special care needs to be taken if the annotation is the last one
WindowLastRef = Optional(',') + ZeroOrMore(White(' \t')) + (Keyword('Window')|Keyword('Coordsys')) + nestedExpr() + ZeroOrMore(White(' \t') + lineEnd)
out = Suppress(WindowLastRef).transformString(out)
# remove empty '[__Dymola_]experimentSetupOutput(),' annotation:
expRef = Optional(',') + ZeroOrMore(White(' \t')) + Optional('__Dymola_') + (Keyword('experimentSetupOutput')|Keyword('experiment')|Keyword('DymolaStoredErrors')|Keyword('Diagram')|Keyword('Icon')) + ~nestedExpr() + ~CharsNotIn(',)')
out = Suppress(expRef).transformString(out)
# Remove Icon and Diagram annotations that do not contain any graphics
emptyRef = ZeroOrMore(White(' \t')) + (Keyword('Icon')|Keyword('Diagram')) + nestedExpr()('args') + ',' + ZeroOrMore(White(' \t') + lineEnd)
emptyRef.setParseAction(skipNonEmptyGraphics)
out = Suppress(emptyRef).transformString(out)
# special care for the last annotation again
emptyRef = Optional(',') + ZeroOrMore(White(' \t')) + (Keyword('Icon')|Keyword('Diagram')) + nestedExpr()('args') + ZeroOrMore(White(' \t') + lineEnd)
emptyRef.setParseAction(skipNonEmptyGraphics)
out = Suppress(emptyRef).transformString(out)
# in case we end up with empty annotations remove them too
AnnotationRef = ZeroOrMore(White(' \t')) + Keyword('annotation') + nestedExpr('(',');',content=' ') + ZeroOrMore(White(' \t') + lineEnd)
out = Suppress(AnnotationRef).transformString(out)
with open(filepath,'w') as mo_file:
mo_file.write(out)
示例13: 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
示例14: _is_running
def _is_running(self, host):
"""Checks if a virtual machine is running.
@param host: name of the virtual machine.
@return: running status.
"""
#FIXME use domstate instead
try:
proc = subprocess.Popen([self.options.xen.path, 'list', '-l',
host],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
if proc.returncode != 0:
log.debug("Xen returns error checking status for machine %s: %s"
% (host, err))
return False
data = OneOrMore(nestedExpr()).parseString(output)
for row in data.asList()[0]:
if row[0] == 'status' and row[1] == '2':
return True
return False
except OSError as e:
log.warning("Xen failed to check status for machine %s: %s"
% (label, e))
return False
示例15: parseOb
def parseOb(self,obpath):
obfile=open(obpath)
outpath=obpath.replace('.observable','.ob')
of=open(outpath,'w+')
allline=[]
for line in obfile:
allline.append(line)
count={}
assignment={}
index=1
while index <len(allline):
line=allline[index]
while line.count('(')>line.count(')'):
index=index+1
line=line+" "+allline[index]
pair=line.split(': ')
index=index+1
if count.has_key(pair[0]):
count[pair[0]]=count[pair[0]]+1
else:
count[pair[0]]=0
key=pair[0]+str(count[pair[0]])
#print pair[1]
path_str=pair[1].replace('\n',' ')
exprs=nestedExpr(opener='(',closer=')').parseString(path_str).asList()
result=self.printNested(of,exprs,False,True)
assignment[key]=result
of.write(key+":"+str(result)+"\n")
obfile.close()
of.close()