Python parse 模块,compile() 实例源码

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

项目:kubeface    作者:hammerlab    | 项目源码 | 文件源码
def __init__(self, name, template, valid_values={}):
        self.template_pieces = []
        self.field_names = []
        parsed = FORMATTER.parse(template)
        for (literal_text, field_name, format_spec, conversion) in parsed:
            assert not conversion
            self.template_pieces.append((literal_text, field_name))
            if field_name not in self.field_names:
                self.field_names.append(field_name)

        self.name = name
        self.template = template
        self.compiled_template = parse.compile(template)
        self.tuple_class = collections.namedtuple(
            self.name,
            list(self.field_names))

        self.valid_values = dict(valid_values)
        for key in self.valid_values:
            assert key in self.field_names
项目:stashpy    作者:afroisalreadyinu    | 项目源码 | 文件源码
def __init__(self, spec):
        spec, pattern_types = grok_re_preprocess(spec)
        self.type_collection = TypeCollection(pattern_types)
        if is_named_re(spec):
            self.re = regex.compile(spec)
            self.parse = None
        else:
            self.re = None
            self.parse = parse.compile(spec)
项目:can2mqtt    作者:jgeisler0303    | 项目源码 | 文件源码
def __init__(self, name, canid, subscriptions, pack_template, var_names, topic_template, payload_template):
        self.name= name

        if not isinstance(canid, basestring) and not isinstance(canid, int):
            raise ValueError("Parameter canid must be a string or an int")
        if isinstance(canid, basestring):
            try:
                canid= int(canid, 0)
            except:
                pass

        self.subscriptions= testForStringList(subscriptions, "subscriptions")

        if not isinstance(pack_template, basestring):
            raise ValueError("Parameter pack_template must be a string")
        self.canid= canid
        self.pack_template= pack_template
        self.var_names= testForStringList(var_names, "var_names")

        if topic_template:
            if not isinstance(topic_template, basestring):
                raise ValueError("Parameter topic_template must be a string")
            try:
                self.topic_template= parse.compile(topic_template)
            except BaseException as e:
                raise ValueError("Error compiling topic_template: %s" % e)
        else:
            self.topic_template= None

        if not isinstance(payload_template, basestring):
            raise ValueError("Parameter payload_template must be a string")
        try:
            self.payload_template= parse.compile(payload_template)
        except BaseException as e:
            raise ValueError("Error compiling payload_template: %s" % e)

        self.error_count= 0