Python pyparsing 模块,delimitedList() 实例源码

我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用pyparsing.delimitedList()

项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
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
项目:bank_wrangler    作者:tmerr    | 项目源码 | 文件源码
def _build_parser():
    date_literal = pp.Regex(r'(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})') \
                     .setParseAction(lambda s,l,t: schema.Date(t.year, t.month, t.day))
    dollars_literal = pp.Regex(r'\$\d+(\.\d{2})') \
                        .setParseAction(lambda s,l,t: schema.Dollars(t[0]))
    string_literal = (pp.QuotedString('"', escChar='\\') | pp.QuotedString("'", escChar='\\')) \
                     .setParseAction(lambda s,l,t: schema.String(t[0]))
    literal = date_literal | dollars_literal | string_literal

    ident = pp.Word(pp.alphas)

    match_op = pp.oneOf(operator_map.keys())
    match = ident + match_op + literal

    assign_op = pp.Literal('=')
    assign = ident + assign_op + literal

    part = (match | assign).setParseAction(lambda s,l,t: [t])
    rule = pp.delimitedList(part) + pp.LineEnd()

    return rule
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
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
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def expression(self):
        expression = pyparsing.Forward()

        # (1 + (2 + 3))
        nested_expression = pyparsing.nestedExpr(
            "(", ")", expression).setParseAction(self._combine_lists)

        # FOO(2 , 3)
        function_call = (
            _TOKEN().setResultsName("function")
            + _OPEN_PARENTHESIS()
            + pyparsing.delimitedList(
                pyparsing.Combine(expression, adjacent=False, joinString=" "),
                delim=",").setResultsName("func_args")
            + _CLOSE_PARENTHESIS()
        )

        expression << pyparsing.OneOrMore(
            function_call.setParseAction(self._is_known_function)
            | pyparsing.Group(nested_expression)
            | _TOKEN()
            | _NOT_TOKEN()
        )

        return pyparsing.Combine(expression, adjacent=False, joinString=" ")
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def _type_name_with_fields(self):
        """Detect type name definitions.

        e.g. int v1;
                 type_t v2, v3;
        type refs ^^^^  ^^^ type_instances

        Returns:
         a list of CField() instances
        """
        return (
            self._type_reference()("type_definition")
            + self._maybe_attributes()("attributes")
            + pyparsing.delimitedList(
                self._type_instance()
            )("field")
        ).setParseAction(self._create_type_name_with_fields)
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def _maybe_attributes(self):
        """Possibly match some attributes.

        The syntax of attributes is described here:
        https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html
        """
        return pyparsing.Group(
            pyparsing.ZeroOrMore(
                _ATTRIBUTE
                + _DOUBLE_OPEN_PARENTHESIS
                + pyparsing.delimitedList(
                    pyparsing.Group(
                        self._identifier()("name")
                        + pyparsing.Optional(
                            _OPEN_PARENTHESIS
                            + parsers.anything_beetween("()")("args")
                            + _CLOSE_PARENTHESIS
                        )
                    )
                )
                + _DOUBLE_CLOSE_PARENTHESIS
            ).setParseAction(self._make_attribute)
        )("attributes")
项目:deb-oslo.utils    作者:openstack    | 项目源码 | 文件源码
def split_by_commas(value):
    """Split values by commas and quotes according to api-wg

    :param value: value to be split

    .. versionadded:: 3.17
    """
    word = (pp.QuotedString(quoteChar='"', escChar='\\')
            | pp.Word(pp.printables, excludeChars='",'))
    grammar = pp.stringStart + pp.delimitedList(word) + pp.stringEnd

    try:
        return list(grammar.parseString(value))
    except pp.ParseException:
        raise ValueError("Invalid value: %s" % value)
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
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
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
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
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
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
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
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
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
def parser(cls):
        unpack = pp.Suppress("unpack")

        packed_col_name = common_parsers.column
        dict_key = pp.Suppress("[") + pp.QuotedString(quoteChar="'") + pp.Suppress("]")
        dict_key_grp = pp.Group(pp.OneOrMore(dict_key))
        new_col_name = common_parsers.column

        unpack_arg = new_col_name + pp.Suppress("=") + packed_col_name + dict_key_grp
        unpack_arg.setParseAction(lambda x: {'packed_col': x[1], 'key_list': x[2], 'new_col_name': x[0]})

        parser = unpack + pp.Suppress("(") + pp.delimitedList(unpack_arg) + pp.Suppress(")")
        parser.setParseAction(lambda x: Unpack(unpack_list=x))
        return parser
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
def parser(self):
        manipulation = (Unpack.parser() | Mutate.parser() | Flatten.parser() |
                        Select.parser() | Remove.parser() | GroupedSummary.parser() |
                        DropNa.parser())
        manipulation_set = pp.delimitedList(manipulation, delim='>>')
        return manipulation_set
项目:querygraph    作者:peter-woyzbun    | 项目源码 | 文件源码
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
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def _arguments(self):
        return pyparsing.Group(
            pyparsing.Optional(
                pyparsing.delimitedList(self.expression())))
项目:rvmi-rekall    作者:fireeye    | 项目源码 | 文件源码
def _define_function_like(self):
        return (
            (_IDENTIFIER.setResultsName("name")
             + _OPEN_PARENTHESES).leaveWhitespace()
            + pyparsing.Optional(
                pyparsing.delimitedList(
                    _IDENTIFIER
                    | pyparsing.Literal("...")  # vararg macro.
                )).setResultsName("arguments")
            + _CLOSE_PARENTHESES
            + pyparsing.restOfLine.setResultsName("replacement")
        ).setParseAction(self._add_function_like)