本文整理汇总了Python中pyparsing.Literal.searchString方法的典型用法代码示例。如果您正苦于以下问题:Python Literal.searchString方法的具体用法?Python Literal.searchString怎么用?Python Literal.searchString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing.Literal
的用法示例。
在下文中一共展示了Literal.searchString方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_function_def_above_main
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def check_function_def_above_main(self, code):
prototype = check_if_function_prototype(code)
function = check_if_function(code)
inside = Literal("int main")
if len(inside.searchString(code)):
return
elif function and not prototype and self.outside_main:
function_regex = re.compile("^\s*(\w+)\s+(\w+)")
match = function_regex.search(code)
function_name = match.group(2) if match else "NOT_FOUND"
self.add_error(label="DEFINITION_ABOVE_MAIN", data={'function': function_name})
示例2: check_main_syntax
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def check_main_syntax(self, code):
# Return value for main is optional in C++11
parser = Literal("int") + Literal("main") + Literal("(") + SkipTo(Literal(")")) + Literal(")")
if len(parser.searchString(code)):
main_prefix = Literal("int") + Literal("main") + Literal("(")
full_use = Literal("int") + "argc" + "," + Optional("const") + "char" + "*" + "argv" + "[" + "]" + ")"
# 3 options for main() syntax
if not len((main_prefix + Literal(")")).searchString(code)) and \
not len((main_prefix + Literal("void") + Literal(")")).searchString(code)) and \
not len((main_prefix + full_use).searchString(code)):
self.add_error(label="MAIN_SYNTAX")
示例3: check_main_prefix
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def check_main_prefix(self, code):
#Return value for main is optional in C++11
parser = Literal("main")+Literal("(")+SkipTo(Literal(")"))+Literal(")")+Literal("{")
if len(parser.searchString(code)):
main_prefix = Literal("main")+Literal("(")
full_use = "int"+Word(alphanums)+","+"char*"+Word(alphanums)+"["+"]"+")"
# 3 options for main() syntax
if not len((main_prefix+Literal(")")).searchString(code)) and \
not len((main_prefix+Literal("void")+Literal(")")).searchString(code)) and \
not len((main_prefix+full_use).searchString(code)):
self.add_error("MAIN_SYNTAX")
示例4: get_log_formats
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def get_log_formats(config):
"""
Parse config for log_format directives
:return: iterator over ('format name', 'format string') tuple of found directives
"""
# log_format name [params]
log_format = Literal('log_format') + parameter + Group(OneOrMore(parameter)) + semicolon
log_format.ignore(pythonStyleComment)
for directive in log_format.searchString(config).asList():
name = directive[1]
format_string = ''.join(directive[2])
yield name, format_string
示例5: check_non_const_global
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def check_non_const_global(self, code):
inside = Literal("int main")
if len(inside.searchString(code)):
self.outside_main = False
if self.outside_main:
function = check_if_function(code)
variables = variables = re.compile("^(?:\w|_)+\s+(?:\w|_|\[|\])+\s*=\s*.+;")
keywords = re.compile("^\s*(?:using|class|struct)")
constants = re.compile("^\s*(?:static\s+)?const")
if not function and variables.search(code) and \
not keywords.search(code) and \
not constants.search(code):
self.add_error(label="NON_CONST_GLOBAL")
示例6: get_access_logs
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def get_access_logs(config):
"""
Parse config for access_log directives
:return: iterator over ('path', 'format name') tuple of found directives
"""
access_log = Literal("access_log") + ZeroOrMore(parameter) + semicolon
access_log.ignore(pythonStyleComment)
for directive in access_log.searchString(config).asList():
path = directive[1]
if path == 'off' or path.startswith('syslog:'):
# nothing to process here
continue
format_name = 'combined'
if len(directive) > 2 and '=' not in directive[2]:
format_name = directive[2]
yield path, format_name
示例7: print
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
#################
print("Example of an extractor")
print("----------------------")
# simple grammar to match #define's
ident = Word(alphas, alphanums+"_")
macroDef = Literal("#define") + ident.setResultsName("name") + "=" + restOfLine.setResultsName("value")
for t,s,e in macroDef.scanString( testData ):
print(t.name,":", t.value)
# or a quick way to make a dictionary of the names and values
# (return only key and value tokens, and construct dict from key-value pairs)
# - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
macros = dict(list(macroDef.searchString(testData)))
print("macros =", macros)
print()
#################
print("Examples of a transformer")
print("----------------------")
# convert C++ namespaces to mangled C-compatible names
scopedIdent = ident + OneOrMore( Literal("::").suppress() + ident )
scopedIdent.setParseAction(lambda t: "_".join(t))
print("(replace namespace-scoped names with C-compatible names)")
print(scopedIdent.transformString( testData ))
示例8: parse_file
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def parse_file(self):
"""Parses an existing namelist file and creates a deck of cards to
hold the data. After this is executed, you need to call the ``load_model()``
method to extract the variables from this data structure."""
infile = open(self.filename, "r")
data = infile.readlines()
infile.close()
# Lots of numerical tokens for recognizing various kinds of numbers
digits = Word(nums)
dot = "."
sign = oneOf("+ -")
ee = CaselessLiteral("E") | CaselessLiteral("D")
num_int = ToInteger(Combine(Optional(sign) + digits))
num_float = ToFloat(
Combine(
Optional(sign)
+ ((digits + dot + Optional(digits)) | (dot + digits))
+ Optional(ee + Optional(sign) + digits)
)
)
# special case for a float written like "3e5"
mixed_exp = ToFloat(Combine(digits + ee + Optional(sign) + digits))
# I don't suppose we need these, but just in case (plus it's easy)
nan = ToFloat(oneOf("NaN Inf -Inf"))
numval = num_float | mixed_exp | num_int | nan
strval = QuotedString(quoteChar='"') | QuotedString(quoteChar="'")
b_list = "T TRUE True true F FALSE False false .TRUE. .FALSE. .T. .F."
boolval = ToBool(oneOf(b_list))
fieldval = Word(alphanums)
# Tokens for parsing a line of data
numstr_token = numval + ZeroOrMore(Suppress(",") + numval) | strval
data_token = numstr_token | boolval
index_token = Suppress("(") + num_int + Suppress(")")
card_token = Group(
fieldval("name")
+ Optional(index_token("index"))
+ Suppress("=")
+ Optional(num_int("dimension") + Suppress("*"))
+ data_token("value")
+ Optional(Suppress("*") + num_int("dimension"))
)
multi_card_token = card_token + ZeroOrMore(Suppress(",") + card_token)
array_continuation_token = numstr_token.setResultsName("value")
array2D_token = (
fieldval("name")
+ Suppress("(")
+ Suppress(num_int)
+ Suppress(",")
+ num_int("index")
+ Suppress(")")
+ Suppress("=")
+ numval
+ ZeroOrMore(Suppress(",") + numval)
)
# Tokens for parsing the group head and tai
group_end_token = Literal("/") | Literal("$END") | Literal("$end") | Literal("&END") | Literal("&end")
group_name_token = (
(Literal("$") | Literal("&"))
+ Word(alphanums).setResultsName("name")
+ Optional(multi_card_token)
+ Optional(group_end_token)
)
# Comment Token
comment_token = Literal("!")
# Loop through each line and parse.
current_group = None
for line in data:
line_base = line
line = line.strip()
# blank line: do nothing
if not line:
continue
if current_group:
# Skip comment cards
if comment_token.searchString(line):
pass
# Process orindary cards
elif multi_card_token.searchString(line):
cards = multi_card_token.parseString(line)
for card in cards:
name, value = _process_card_info(card)
self.cards[-1].append(Card(name, value))
#.........这里部分代码省略.........
示例9: restscrape
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def restscrape(resturl, filenamersc, filenamerevsc):
time.sleep(randint(2,8))
# Read the url
response = urllib2.urlopen(resturl)
soup = BeautifulSoup(response.read())
response.close()
# Check if it is rated
if soup.find(itemprop="ratingValue") == None:
return
# Anamoly
if soup.find(class_="container no-reviews") != None:
return
# Check if it is not the alternate version
if soup.find(id="mapbox") != None:
print "alt version"
restscrape(resturl, filenamersc, filenamerevsc)
return
# Check if it is not an alternate version
if soup.find(class_="friend-count miniOrange") == None:
print "alt version rev"
restscrape(resturl, filenamersc, filenamerevsc)
return
#### ## ## ######## #######
## ### ## ## ## ##
## #### ## ## ## ##
## ## ## ## ###### ## ##
## ## #### ## ## ##
## ## ### ## ## ##
#### ## ## ## #######
# Key Yelp information
title = soup.find(property="og:title").get("content").encode('utf-8')
latitude = soup.find(property="place:location:latitude").get("content")
longitude = soup.find(property="place:location:longitude").get("content")
rating = soup.find(itemprop="ratingValue").get("content")
reviewCount = soup.find(itemprop="reviewCount").get_text()
if soup.find(id="cat_display") != None:
categories = soup.find(id="cat_display").get_text().strip()
categories = ' '.join(categories.split())
else:
categories = "None"
if soup.find(class_="photo-box-img")['src'] != "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/5f69f303f17c/default_avatars/business_medium_square.png":
photos = "Has photos"
else:
photos = "None"
if soup.find(id="bizUrl") != None:
URL = soup.find(id="bizUrl").get_text().strip().encode('utf-8')
else:
URL = "None"
# Get Neighborhoods
# Particularly special code because it has to be stripped from javascript script
# Automatically strip quotes from quoted strings
# quotedString matches single or double quotes
neighborhood = ""
quotedString.setParseAction(removeQuotes)
# Define a pattern to extract the neighborhoods: entry
neighborhoodsSpec = Literal('\"neighborhoods\":') + '[' + delimitedList(quotedString)('neighborhoods') + ']'
for hoods in neighborhoodsSpec.searchString(soup):
neighborhood = str(hoods.neighborhoods)
# Yelp Interaction/Information
if soup.find(class_="yelp-menu") != None:
menu = "Has menu"
else:
menu = "None"
if soup.find(id="opentable-reservation-actions") != None:
reservable = "Reservable"
else:
reservable = "None"
if soup.find(class_="media-story offer-detail") != None:
deal = "Has deal"
else:
deal = "None"
if soup.find(id="delivery-address-form") != None:
yelpDelivery = "Delivery system"
else:
yelpDelivery = "None"
if soup.find(id="bizSlide") != None:
slides = "Has slides"
else:
slides = "None"
#.........这里部分代码省略.........
示例10: check_while_true
# 需要导入模块: from pyparsing import Literal [as 别名]
# 或者: from pyparsing.Literal import searchString [as 别名]
def check_while_true(self, code):
statement_parser = Literal("while") + Literal("(") + Literal("true") + Literal(")")
if len(statement_parser.searchString(code)):
self.add_error(label="WHILE_TRUE")