我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用pyparsing.Suppress()。
def parser(self): join_type = (pp.Literal("LEFT") | pp.Literal("RIGHT") | pp.Literal("INNER") | pp.Literal("OUTER")) node_name = pp.Word(pp.alphas, pp.alphanums + "_$") col_name = pp.Word(pp.alphas, pp.alphanums + "_$") col_name_list = pp.Group(pp.delimitedList(col_name, delim=",")) l_brac = pp.Suppress("[") r_brac = pp.Suppress("]") single_join = (join_type + pp.Suppress("(") + node_name + l_brac + col_name_list + r_brac + pp.Suppress("==>") + node_name + l_brac + col_name_list + r_brac + pp.Suppress(")")) single_join.addParseAction(lambda x: self._add_join(join_type=x[0], child_node_name=x[1], child_cols=x[2], parent_node_name=x[3], parent_cols=x[4])) join_block = pp.OneOrMore(single_join) return join_block
def __init__(self, identifier_parser=None): """ :param IdentifierParser identifier_parser: An identifier parser for checking the 3P and 5P partners """ self.identifier_parser = identifier_parser if identifier_parser is not None else IdentifierParser() identifier = self.identifier_parser.language reference_seq = oneOf(['r', 'p', 'c']) coordinate = pyparsing_common.integer | '?' missing = Keyword('?') range_coordinate = missing(FUSION_MISSING) | ( reference_seq(FUSION_REFERENCE) + Suppress('.') + coordinate(FUSION_START) + Suppress('_') + coordinate( FUSION_STOP)) self.language = fusion_tags + nest(Group(identifier)(PARTNER_5P), Group(range_coordinate)(RANGE_5P), Group(identifier)(PARTNER_3P), Group(range_coordinate)(RANGE_3P)) super(FusionParser, self).__init__(self.language)
def _parse_atat_lattice(lattice_in): """Parse an ATAT-style `lat.in` string. The parsed string will be in three groups: (Coordinate system) (lattice) (atoms) where the atom group is split up into subgroups, each describing the position and atom name """ float_number = Regex(r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?').setParseAction(lambda t: [float(t[0])]) vector = Group(float_number + float_number + float_number) angles = vector vector_line = vector + Suppress(LineEnd()) coord_sys = Group((vector_line + vector_line + vector_line) | (vector + angles + Suppress(LineEnd()))) lattice = Group(vector + vector + vector) atom = Group(vector + Group(OneOrMore(Word(alphas + '_')))) atat_lattice_grammer = coord_sys + lattice + Group(OneOrMore(atom)) # parse the input string and convert it to a POSCAR string return atat_lattice_grammer.parseString(lattice_in)
def parser(cls): group_by = pp.Suppress("group_by") column = common_parsers.column group_by_block = group_by + pp.Suppress("(") + pp.Group(pp.delimitedList(column)) + pp.Suppress(")") summarize = pp.Suppress("summarize") summary_col_name = common_parsers.column summary_type = pp.Word(pp.alphas, pp.alphanums + "_$") target_column = common_parsers.column single_summary = summary_col_name + pp.Suppress("=") + summary_type +\ pp.Suppress("(") + target_column + pp.Suppress(")") single_summary.setParseAction(lambda x: {'summary_col_name': x[0], 'summary_type': x[1], 'target_col': x[2]}) summarize_block = summarize + pp.Suppress("(") + pp.Group(pp.delimitedList(single_summary)) + pp.Suppress(")") parser = group_by_block + pp.Suppress(">>") + summarize_block parser.setParseAction(lambda x: GroupedSummary(group_by=x[0], aggregations=x[1])) return parser
def parser(self): connector_name = pp.Word(pp.alphas, pp.alphanums + "_$") connector_type = pp.Word(pp.alphas) connector_kwarg = (pp.Word(pp.alphas, pp.alphanums + "_$") + pp.Suppress("=") + pp.QuotedString(quoteChar="'")) connector_kwarg.setParseAction(lambda x: {x[0]: x[1]}) conn_kwarg_list = pp.delimitedList(connector_kwarg) conn_kwarg_list.setParseAction(lambda x: dict(pair for d in x for pair in d.items())) single_connector = (connector_name + pp.Suppress("<-") + connector_type + pp.Suppress("(") + conn_kwarg_list + pp.Suppress(")")) single_connector.setParseAction(lambda x: self._add_connector(conn_name=x[0], conn_type=x[1], conn_kwargs=x[2])) connector_block = pp.OneOrMore(single_connector) return connector_block
def _parse(self, df=None, independent_param_vals=None): expr_evaluator = Evaluator(df=df, name_dict=independent_param_vals) param_expr = expr_evaluator.parser() render_as_type = pp.Word(pp.alphas, pp.alphanums + "_$") render_as_type.setParseAction(lambda x: self._set_render_type(value=x[0])) container_type = pp.Optional(pp.Word(pp.alphas, pp.alphanums + "_$") + pp.Suppress(":"), default=None) container_type.setParseAction(lambda x: self._set_container_type(value=x[0])) parser = param_expr + pp.Suppress("->") + container_type + render_as_type try: parser.parseString(self.parameter_str) except pp.ParseException, e: raise ParameterRenderError("Error parsing parameter string: \n %s" % e) python_value = expr_evaluator.output_value() return python_value
def __init__(self, namespace_dict=None, namespace_regex=None, default_namespace=None, allow_naked_names=False): """ :param dict[str,dict[str,str]] namespace_dict: A dictionary of {namespace: {name: encoding}} :param dict[str,str] namespace_regex: A dictionary of {namespace: regular expression string} to compile :param set[str] default_namespace: A set of strings that can be used without a namespace :param bool allow_naked_names: If true, turn off naked namespace failures """ self._namespace_dict = namespace_dict self._namespace_regex = {} if namespace_regex is None else namespace_regex self._namespace_regex_compiled = { keyword: re.compile(pattern) for keyword, pattern in self.namespace_regex.items() } self.default_namespace = set(default_namespace) if default_namespace is not None else None self.allow_naked_names = allow_naked_names self.identifier_qualified = word(NAMESPACE) + Suppress(':') + (word | quote)(NAME) if self.namespace_dict is not None: self.identifier_qualified.setParseAction(self.handle_identifier_qualified) self.identifier_bare = (word | quote)(NAME) if self.default_namespace is not None: self.identifier_bare.setParseAction(self.handle_identifier_default) elif self.allow_naked_names: self.identifier_bare.setParseAction(IdentifierParser.handle_namespace_lenient) else: self.identifier_bare.setParseAction(self.handle_namespace_invalid) super(IdentifierParser, self).__init__(self.identifier_qualified | self.identifier_bare)
def __init__(self, ffilter, queue_out): FuzzQueue.__init__(self, queue_out) Thread.__init__(self) self.setName('filter_thread') self.queue_out = queue_out if PYPARSING: element = oneOf("c l w h") digits = "XB0123456789" integer = Word( digits )#.setParseAction( self.__convertIntegers ) elementRef = Group(element + oneOf("= != < > >= <=") + integer) operator = oneOf("and or") definition = elementRef + ZeroOrMore( operator + elementRef) nestedformula = Group(Suppress(Optional(Literal("("))) + definition + Suppress(Optional(Literal(")")))) self.finalformula = nestedformula + ZeroOrMore( operator + nestedformula) elementRef.setParseAction(self.__compute_element) nestedformula.setParseAction(self.__compute_formula) self.finalformula.setParseAction(self.__myreduce) self.res = None self.hideparams = ffilter if "XXX" in self.hideparams['codes']: self.hideparams['codes'].append("0") self.baseline = None
def __init__(self): if PYPARSING: category = Word( alphas + "_-*", alphanums + "_-*" ) operator = oneOf("and or ,") neg_operator = "not" elementRef = category definition = elementRef + ZeroOrMore( operator + elementRef) nestedformula = Group(Suppress(Optional(Literal("("))) + definition + Suppress(Optional(Literal(")")))) neg_nestedformula = Optional(neg_operator) + nestedformula self.finalformula = neg_nestedformula + ZeroOrMore( operator + neg_nestedformula) elementRef.setParseAction(self.__compute_element) neg_nestedformula.setParseAction(self.__compute_neg_formula) nestedformula.setParseAction(self.__compute_formula) self.finalformula.setParseAction(self.__myreduce)
def main(): word = Word(alphanums) command = Group(OneOrMore(word)) token = Suppress("->") device = Group(OneOrMore(word)) argument = Group(OneOrMore(word)) event = command + token + device + Optional(token + argument) gate = Gate() garage = Garage() airco = Aircondition() heating = Heating() boiler = Boiler() fridge = Fridge() tests = ('open -> gate', 'close -> garage', 'turn on -> aircondition', 'turn off -> heating', 'increase -> boiler temperature -> 5 degrees', 'decrease -> fridge temperature -> 2 degrees') open_actions = {'gate':gate.open, 'garage':garage.open, 'aircondition':airco.turn_on, 'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature, 'fridge temperature':fridge.increase_temperature} close_actions = {'gate':gate.close, 'garage':garage.close, 'aircondition':airco.turn_off, 'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature, 'fridge temperature':fridge.decrease_temperature} for t in tests: if len(event.parseString(t)) == 2: # no argument cmd, dev = event.parseString(t) cmd_str, dev_str = ' '.join(cmd), ' '.join(dev) if 'open' in cmd_str or 'turn on' in cmd_str: open_actions[dev_str]() elif 'close' in cmd_str or 'turn off' in cmd_str: close_actions[dev_str]() elif len(event.parseString(t)) == 3: # argument cmd, dev, arg = event.parseString(t) cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg) num_arg = 0 try: num_arg = int(arg_str.split()[0]) # extract the numeric part except ValueError as err: print("expected number but got: '{}'".format(arg_str[0])) if 'increase' in cmd_str and num_arg > 0: open_actions[dev_str](num_arg) elif 'decrease' in cmd_str and num_arg > 0: close_actions[dev_str](num_arg)
def build_parser(self): parsed_term = pyparsing.Group(pyparsing.Combine(pyparsing.Word(pyparsing.alphanums) + \ pyparsing.Suppress('*'))).setResultsName('wildcard') | \ pyparsing.Group(pyparsing.Combine(pyparsing.Word(pyparsing.alphanums+"._") + \ pyparsing.Word(':') + pyparsing.Group(pyparsing.Optional("\"") + \ pyparsing.Optional("<") + pyparsing.Optional(">") + pyparsing.Optional("=") + \ pyparsing.Optional("-") + pyparsing.Word(pyparsing.alphanums+"._/") + \ pyparsing.Optional("&") + pyparsing.Optional("<") + pyparsing.Optional(">") + \ pyparsing.Optional("=") + pyparsing.Optional("-") + \ pyparsing.Optional(pyparsing.Word(pyparsing.alphanums+"._/")) + \ pyparsing.Optional("\"")))).setResultsName('fields') | \ pyparsing.Group(pyparsing.Combine(pyparsing.Suppress('-')+ \ pyparsing.Word(pyparsing.alphanums+"."))).setResultsName('not_term') | \ pyparsing.Group(pyparsing.Word(pyparsing.alphanums)).setResultsName('term') parsed_or = pyparsing.Forward() parsed_quote_block = pyparsing.Forward() parsed_quote_block << ((parsed_term + parsed_quote_block) | parsed_term ) parsed_quote = pyparsing.Group(pyparsing.Suppress('"') + parsed_quote_block + \ pyparsing.Suppress('"')).setResultsName("quotes") | parsed_term parsed_parenthesis = pyparsing.Group((pyparsing.Suppress("(") + parsed_or + \ pyparsing.Suppress(")"))).setResultsName("parenthesis") | parsed_quote parsed_and = pyparsing.Forward() parsed_and << (pyparsing.Group(parsed_parenthesis + pyparsing.Suppress(pyparsing.Keyword("and")) + \ parsed_and).setResultsName("and") | \ pyparsing.Group(parsed_parenthesis + pyparsing.OneOrMore(~pyparsing.oneOf("or and") + \ parsed_and)).setResultsName("and") | parsed_parenthesis) parsed_or << (pyparsing.Group(parsed_and + pyparsing.Suppress(pyparsing.Keyword("or")) + \ parsed_or).setResultsName("or") | parsed_and) return parsed_or.parseString
def entry(): name = Word(alphas, alphas+'_') value = Word(nums, nums+".").setResultsName('nominal_value') uncert = Word(nums).setResultsName('std_dev') value_uncert = value + Suppress("(") + uncert + Suppress(")") return Dict( Group(name + Suppress("=") + value_uncert ) )
def parser(cls): lpar = pp.Suppress("(") rpar = pp.Suppress(")") mutate = pp.Suppress('mutate') col_name = pp.Word(pp.alphas, pp.alphanums + "_$") expr_evaluator = Evaluator(deferred_eval=True) col_expr = expr_evaluator.parser() mutation = col_name + pp.Suppress("=") + col_expr mutation.setParseAction(lambda x: {'col_name': x[0], 'col_expr': x[1]}) mutations = pp.Group(pp.delimitedList(mutation)) parser = mutate + lpar + mutations + rpar parser.setParseAction(lambda x: Mutate(mutations=x)) return parser
def parser(cls): rename = pp.Suppress("rename") rename_kwarg = common_parsers.column + pp.Suppress("=") + common_parsers.column rename_kwarg.setParseAction(lambda x: {x[0]: x[1]}) kwargs = pp.Group(pp.delimitedList(rename_kwarg)) kwargs.setParseAction(lambda x: {k: v for d in x for k, v in d.items()}) parser = rename + pp.Suppress("(") + kwargs + pp.Suppress(")") parser.setParseAction(lambda x: Rename(columns=x[0])) return parser
def parser(cls): select = pp.Suppress("select") column = common_parsers.column parser = select + pp.Suppress("(") + pp.Group(pp.delimitedList(column)) + pp.Suppress(")") parser.setParseAction(lambda x: Select(columns=x[0])) return parser
def parser(cls): remove = pp.Suppress("remove") column = common_parsers.column parser = remove + pp.Suppress("(") + pp.Group(pp.delimitedList(column)) + pp.Suppress(")") parser.setParseAction(lambda x: Remove(columns=x[0])) return parser
def parser(cls): unpack = pp.Suppress("flatten") column = common_parsers.column parser = unpack + pp.Suppress("(") + column + pp.Suppress(")") parser.setParseAction(lambda x: Flatten(column=x[0])) return parser
def parser(cls): drop_na = pp.Suppress("group_by") parser = drop_na + pp.Suppress("()") parser.setParseAction(lambda x: DropNa()) return parser # ============================================= # Manipulation Set # ---------------------------------------------
def parser(self): query_key = pp.Keyword("QUERY") query_value = pp.Suppress("|") + pp.SkipTo(pp.Suppress(";"), include=True) fields_key = pp.Keyword("FIELDS") field_name = common_parsers.column field_name_list = pp.Group(pp.delimitedList(field_name, delim=",")).setParseAction(lambda x: x.asList()) fields_block = (pp.Suppress(fields_key) + field_name_list) connector_name = pp.Word(pp.alphas, pp.alphanums + "_$") using_block = pp.Suppress("USING") + connector_name then_key = pp.Suppress("THEN") manipulation_set = pp.Suppress("|") + pp.SkipTo(pp.Suppress(";"), include=True) then_block = then_key + manipulation_set as_key = pp.Suppress("AS") node_name = pp.Word(pp.alphas, pp.alphanums + "_$") as_block = as_key + node_name query_node_block = (pp.Suppress(query_key) + query_value + pp.Optional(fields_block, default=None) + using_block + pp.Optional(then_block, default=None) + as_block) query_node_block.setParseAction(lambda x: self._add_query_node(query_value=x[0], connector_name=x[2], node_name=x[4], fields=x[1], manipulation_set=x[3])) single_query_node = query_node_block + pp.Optional(pp.Suppress("---")) retrieve_block = pp.OneOrMore(single_query_node) return retrieve_block
def parser(self): # Define punctuation as suppressed literals. lparen, rparen, lbrack, rbrack, lbrace, rbrace, colon = \ map(pp.Suppress, "()[]{}:") integer = pp.Combine(pp.Optional(pp.oneOf("+ -")) + pp.Word(pp.nums)) \ .setName("integer") \ .setParseAction(lambda toks: int(toks[0])) real = pp.Combine(pp.Optional(pp.oneOf("+ -")) + pp.Word(pp.nums) + "." + pp.Optional(pp.Word(pp.nums)) + pp.Optional(pp.oneOf("e E") + pp.Optional(pp.oneOf("+ -")) + pp.Word(pp.nums))) \ .setName("real") \ .setParseAction(lambda toks: float(toks[0])) _datetime_arg = (integer | real) datetime_args = pp.Group(pp.delimitedList(_datetime_arg)) _datetime = pp.Suppress(pp.Literal('datetime') + pp.Literal("(")) + datetime_args + pp.Suppress(")") _datetime.setParseAction(lambda x: self._make_datetime(x[0])) tuple_str = pp.Forward() list_str = pp.Forward() dict_str = pp.Forward() list_item = real | integer | _datetime | pp.quotedString.setParseAction(pp.removeQuotes) | \ pp.Group(list_str) | tuple_str | dict_str tuple_str << (pp.Suppress("(") + pp.Optional(pp.delimitedList(list_item)) + pp.Optional(pp.Suppress(",")) + pp.Suppress(")")) tuple_str.setParseAction(lambda toks : tuple(toks.asList())) list_str << (lbrack + pp.Optional(pp.delimitedList(list_item) + pp.Optional(pp.Suppress(","))) + rbrack) dict_entry = pp.Group(list_item + colon + list_item) dict_str << (lbrace + pp.Optional(pp.delimitedList(dict_entry) + pp.Optional(pp.Suppress(","))) + rbrace) dict_str.setParseAction(lambda toks: dict(toks.asList())) return list_item
def parse_list_of_lists(xs): """ Parse a list of list of integers using pyparsing """ enclosed = pa.Forward() # Parser to be defined later natural = pa.Word(pa.nums) # Natural Number # Nested Grammar nestedBrackets = pa.nestedExpr(pa.Suppress('['), pa.Suppress(']'), content=enclosed) enclosed << (natural | pa.Suppress(',') | nestedBrackets) try: rs = enclosed.parseString(xs).asList()[0] return list(map(lambda x: list(map(int, x)), rs)) except pa.ParseException: raise RuntimeError("Invalid Macro states Specification")
def parse_line(attribute, string): Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=')) + String('data') + Suppress(Literal(';') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() return [int_else_float_except_string(s) for s in result['data'].asList()]
def main(): word = Word(alphanums) command = Group(OneOrMore(word)) token = Suppress("->") device = Group(OneOrMore(word)) argument = Group(OneOrMore(word)) event = command + token + device + Optional(token + argument) gate = Gate() garage = Garage() airco = Aircondition() heating = Heating() boiler = Boiler() fridge = Fridge() tests = ('open -> gate', 'close -> garage', 'turn on -> aircondition', 'turn off -> heating', 'increase -> boiler temperature -> 20 degrees', 'decrease -> fridge temperature -> 6 degree') open_actions = {'gate':gate.open, 'garage':garage.open, 'aircondition':airco.turn_on, 'heating':heating.turn_on, 'boiler temperature':boiler.increase_temperature, 'fridge temperature':fridge.increase_temperature} close_actions = {'gate':gate.close, 'garage':garage.close, 'aircondition':airco.turn_off, 'heating':heating.turn_off, 'boiler temperature':boiler.decrease_temperature, 'fridge temperature':fridge.decrease_temperature} for t in tests: if len(event.parseString(t)) == 2: # no argument cmd, dev = event.parseString(t) cmd_str, dev_str = ' '.join(cmd), ' '.join(dev) if 'open' in cmd_str or 'turn on' in cmd_str: open_actions[dev_str]() elif 'close' in cmd_str or 'turn off' in cmd_str: close_actions[dev_str]() elif len(event.parseString(t)) == 3: # argument cmd, dev, arg = event.parseString(t) cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg) num_arg = 0 try: num_arg = int(arg_str.split()[0]) # extract the numeric part except ValueError as err: print("expected number but got: '{}'".format(arg_str[0])) if 'increase' in cmd_str and num_arg > 0: open_actions[dev_str](num_arg) elif 'decrease' in cmd_str and num_arg > 0: close_actions[dev_str](num_arg)
def main(): word = Word(alphanums) command = Group(OneOrMore(word)) token = Suppress("->") device = Group(OneOrMore(word)) argument = Group(OneOrMore(word)) event = command + token + device + Optional(token + argument) gate = Gate() garage = Garage() airco = Aircondition() heating = Heating() boiler = Boiler() fridge = Fridge() tests = ('open -> gate', 'close -> garage', 'turn on -> aircondition', 'turn off -> heating', 'increase -> boiler temperature -> 5 degrees', 'decrease -> fridge temperature -> 2 degrees') open_actions = {'gate': gate.open, 'garage': garage.open, 'aircondition': airco.turn_on, 'heating': heating.turn_on, 'boiler temperature': boiler.increase_temperature, 'fridge temperature': fridge.increase_temperature} close_actions = {'gate': gate.close, 'garage': garage.close, 'aircondition': airco.turn_off, 'heating': heating.turn_off, 'boiler temperature': boiler.decrease_temperature, 'fridge temperature': fridge.decrease_temperature} for t in tests: if len(event.parseString(t)) == 2: # ???? cmd, dev = event.parseString(t) cmd_str, dev_str = ' '.join(cmd), ' '.join(dev) if 'open' in cmd_str or 'turn on' in cmd_str: open_actions[dev_str]() elif 'close' in cmd_str or 'turn off' in cmd_str: close_actions[dev_str]() elif len(event.parseString(t)) == 3: # ??? cmd, dev, arg = event.parseString(t) cmd_str, dev_str, arg_str = ' '.join(cmd), ' '.join(dev), ' '.join(arg) num_arg = 0 try: num_arg = int(arg_str.split()[0]) # ?????? except ValueError as err: print("expected number but got: '{}'".format(arg_str[0])) if 'increase' in cmd_str and num_arg > 0: open_actions[dev_str](num_arg) elif 'decrease' in cmd_str and num_arg > 0: close_actions[dev_str](num_arg)
def parse_table(attribute, string): Line = OneOrMore(Float)('data') + Literal(';') + Optional(Comments, default='')('name') Grammar = Suppress(Keyword('mpc.{}'.format(attribute)) + Keyword('=') + Keyword('[') + Optional(Comments)) + OneOrMore(Group(Line)) + Suppress(Keyword(']') + Optional(Comments)) result, i, j = Grammar.scanString(string).next() _list = list() for r in result: _list.append([int_else_float_except_string(s) for s in r['data'].asList()]) return _list