本文整理汇总了Python中pyparsing.ParseResults方法的典型用法代码示例。如果您正苦于以下问题:Python pyparsing.ParseResults方法的具体用法?Python pyparsing.ParseResults怎么用?Python pyparsing.ParseResults使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyparsing
的用法示例。
在下文中一共展示了pyparsing.ParseResults方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _set_matcher_element_attributes
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def _set_matcher_element_attributes(self, element):
# Set the ParserElement's action.
element.setParseAction(self._parse_action)
# Save the element's original postParse function.
closure = element.postParse
# Set a new function and use the original function for returning values.
def postParse(instring, loc, tokenlist):
if isinstance(tokenlist, pyparsing.ParseResults):
s = " ".join(tokenlist.asList())
elif isinstance(tokenlist, list):
s = "".join(tokenlist)
elif isinstance(tokenlist, string_types):
s = tokenlist
else:
raise TypeError("postParse received invalid tokenlist %s"
% tokenlist)
self.matching_slice = slice(loc - len(s), loc)
return closure(instring, loc, tokenlist)
element.postParse = postParse
# Return the element.
return element
示例2: _addSimpleData
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def _addSimpleData(self, data_list, data):
if isinstance(data[0], ParseResults):
inferred_dimen = len(data[0])
else:
inferred_dimen = 1
if self.dimen == None:
# infer dimension from records
self.dimen = inferred_dimen
if self.current_slice == None:
self._setSlice(tuple(['*'] * self.dimen))
if len(self.free_indices) == inferred_dimen:
for d in data.asList():
self._addValue(data_list, d)
elif len(self.free_indices) > 1 and inferred_dimen:
for c in chunk(data, len(self.free_indices)):
self._addValue(data_list, c)
else:
raise AmplyError("Dimension of elements (%d) does not match "
"declared dimension, (%d)" %
(inferred_dimen, self.dimen))
示例3: handle_document
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def handle_document(self, line: str, position: int, tokens: ParseResults) -> ParseResults:
"""Handle statements like ``SET DOCUMENT X = "Y"``.
:raises: InvalidMetadataException
:raises: VersionFormatWarning
"""
key = tokens['key']
value = tokens['value']
if key not in DOCUMENT_KEYS:
raise InvalidMetadataException(self.get_line_number(), line, position, key, value)
norm_key = DOCUMENT_KEYS[key]
if norm_key in self.document_metadata:
logger.warning('Tried to overwrite metadata: %s', key)
return tokens
self.document_metadata[norm_key] = value
if norm_key == METADATA_VERSION:
self.raise_for_version(line, position, value)
return tokens
示例4: handle_annotations_url
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def handle_annotations_url(self, line: str, position: int, tokens: ParseResults) -> ParseResults:
"""Handle statements like ``DEFINE ANNOTATION X AS URL "Y"``.
:raises: RedefinedAnnotationError
"""
keyword = tokens['name']
self.raise_for_redefined_annotation(line, position, keyword)
url = tokens['url']
self.annotation_url_dict[keyword] = url
if self.skip_validation:
return tokens
self.annotation_to_term[keyword] = self.manager.get_annotation_entry_names(url)
return tokens
示例5: make_span
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def make_span(s, l, t):
def compute_hi(init_loc, tokens):
hi = init_loc
for tok in tokens:
if isinstance(tok, ASTNode):
hi = max(hi, tok.span.hi)
elif isinstance(tok, six.string_types):
hi += len(tok)
elif isinstance(tok, p.ParseResults):
hi = max(hi, compute_hi(init_loc, tok))
else:
raise exception.BananaGrammarBug(
"Couldn't create span for: {}".format(tok)
)
return hi
if len(t) > 0:
span_hi = compute_hi(l, t)
return Span(s, l, span_hi)
else:
return Span(s, l, 2)
示例6: __init__
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def __init__(self, span, expr_tree):
"""
Construct an expression
:type span: Span
:param span: Span for the expression.
:type expr_tree: p.ParseResults
;:param expr_tree: The tree generated by pyparsing.infixNotation
"""
super(Expr, self).__init__(span)
# We don't use this tree at this point.
# During typecheck we will make sure
# that the expression can evaluate
# Finally during evaluation, we will evaluate
# the final result.
if isinstance(expr_tree, p.ParseResults):
expr_tree = expr_tree.asList()
if isinstance(expr_tree, list):
for i in range(0, len(expr_tree)):
if isinstance(expr_tree[i], list):
expr_tree[i] = Expr(span, expr_tree[i])
self.expr_tree = expr_tree
else:
self.expr_tree = [expr_tree]
示例7: _GetTimeElements
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def _GetTimeElements(self, time_structure):
"""Builds time elements from a setupapi time_stamp field.
Args:
time_structure (pyparsing.ParseResults): structure of tokens derived from
a setupapi time_stamp field.
Returns:
dfdatetime.TimeElements: date and time extracted from the value or None
if the structure does not represent a valid date and time value.
"""
try:
date_time = dfdatetime_time_elements.TimeElementsInMilliseconds(
time_elements_tuple=time_structure)
# Setupapi logs store date and time values in local time.
date_time.is_local_time = True
return date_time
except ValueError:
return None
示例8: ParseRecord
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def ParseRecord(self, parser_mediator, key, structure):
"""Parses a log record structure and produces events.
Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): identifier of the structure of tokens.
structure (pyparsing.ParseResults): structure of tokens derived from
a line of a text file.
Raises:
ParseError: when the structure type is unknown.
"""
if key not in ('comment', 'logline'):
raise errors.ParseError(
'Unable to parse record, unknown structure: {0:s}'.format(key))
if key == 'comment':
self._ParseCommentRecord(structure)
elif key == 'logline':
self._ParseLogLine(parser_mediator, structure)
# pylint: disable=unused-argument
示例9: ParseRecord
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def ParseRecord(self, parser_mediator, key, structure):
"""Parses a log record structure and produces events.
Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): name of the parsed structure.
structure (pyparsing.ParseResults): structure parsed from the log file.
Raises:
ParseError: when the structure type is unknown.
"""
if key not in ('comment', 'logline'):
raise errors.ParseError(
'Unable to parse record, unknown structure: {0:s}'.format(key))
if key == 'logline':
self._ParseLogLine(parser_mediator, structure)
elif key == 'comment':
self._ParseComment(structure)
# pylint: disable=unused-argument
示例10: ParseRecord
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def ParseRecord(self, parser_mediator, key, structure):
"""Parses a matching entry.
Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): name of the parsed structure.
structure (pyparsing.ParseResults): elements parsed from the file.
Raises:
ParseError: when the structure type is unknown.
"""
if key not in self._SUPPORTED_KEYS:
raise errors.ParseError(
'Unable to parse record, unknown structure: {0:s}'.format(key))
if key == 'greeting':
self._ReadGreeting(structure)
elif key == 'log_entry':
self._ParseLine(parser_mediator, structure)
示例11: ParseRecord
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def ParseRecord(self, parser_mediator, key, structure):
"""Parses a log record structure and produces events.
Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): name of the parsed structure.
structure (pyparsing.ParseResults): structure of tokens derived from
a line of a text file.
Raises:
ParseError: when the structure type is unknown.
"""
if key not in ('logline', 'repeated'):
raise errors.ParseError(
'Unable to parse record, unknown structure: {0:s}'.format(key))
self._ParseLogLine(parser_mediator, structure, key)
示例12: ParseRecord
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def ParseRecord(self, parser_mediator, key, structure):
"""Parses a log record structure and produces events.
Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): identifier of the structure of tokens.
structure (pyparsing.ParseResults): structure of tokens derived from
a line of a text file.
Raises:
ParseError: when the structure type is unknown.
"""
if key not in ('logline', 'repeated'):
raise errors.ParseError(
'Unable to parse record, unknown structure: {0:s}'.format(key))
self._ParseLogLine(parser_mediator, structure, key)
示例13: _GetTimeElementsTuple
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def _GetTimeElementsTuple(self, structure):
"""Retrieves a time elements tuple from the structure.
Args:
structure (pyparsing.ParseResults): structure of tokens derived from
a line of a vsftp log file.
Returns:
tuple: containing:
year (int): year.
month (int): month, where 1 represents January.
day_of_month (int): day of month, where 1 is the first day of the month.
hours (int): hours.
minutes (int): minutes.
seconds (int): seconds.
"""
time_elements_tuple = self._GetValueFromStructure(structure, 'date_time')
_, month, day_of_month, hours, minutes, seconds, year = time_elements_tuple
month = timelib.MONTH_DICT.get(month.lower(), 0)
return (year, month, day_of_month, hours, minutes, seconds)
示例14: ParseRecord
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def ParseRecord(self, parser_mediator, key, structure):
"""Parses a log record structure and produces events.
Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
key (str): identifier of the structure of tokens.
structure (pyparsing.ParseResults): structure of tokens derived from
a line of a text file.
Raises:
ParseError: when the structure type is unknown.
"""
if key != 'logline':
raise errors.ParseError(
'Unable to parse record, unknown structure: {0:s}'.format(key))
self._ParseLogLine(parser_mediator, structure)
示例15: _BuildDateTime
# 需要导入模块: import pyparsing [as 别名]
# 或者: from pyparsing import ParseResults [as 别名]
def _BuildDateTime(time_elements_structure):
"""Builds time elements from an APT History time stamp.
Args:
time_elements_structure (pyparsing.ParseResults): structure of tokens
derived from an APT History time stamp.
Returns:
dfdatetime.TimeElements: date and time extracted from the structure or
None f the structure does not represent a valid string.
"""
try:
date_time = dfdatetime_time_elements.TimeElements(
time_elements_tuple=time_elements_structure)
# APT History logs store date and time values in local time.
date_time.is_local_time = True
return date_time
except ValueError:
return None