• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python ordereddict.OrderedDict类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中topaz.utils.ordereddict.OrderedDict的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict类的具体用法?Python OrderedDict怎么用?Python OrderedDict使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了OrderedDict类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: pop_keyerror

 def pop_keyerror(n):
     o = OrderedDict()
     o[3] = 4
     try:
         return o.pop(n)
     except KeyError:
         return 500
开发者ID:Perth68,项目名称:topaz,代码行数:7,代码来源:test_ordereddict.py


示例2: update

 def update(n):
     o = OrderedDict()
     o[3] = 4
     v = OrderedDict()
     v[n] = 5
     o.update(v)
     return o[3]
开发者ID:Perth68,项目名称:topaz,代码行数:7,代码来源:test_ordereddict.py


示例3: delitem

 def delitem(n):
     o = OrderedDict()
     o[2] = 3
     o[3] = 4
     del o[n]
     vals = o.values()
     return vals[0] * 10 + len(vals)
开发者ID:Perth68,项目名称:topaz,代码行数:7,代码来源:test_ordereddict.py


示例4: iteritems

 def iteritems(n):
     o = OrderedDict()
     o[0] = 10
     o[2] = 15
     o[3] = 12
     r = []
     for k, v in o.iteritems():
         r.append((k, v))
     p = r[n]
     return p[0] * 100 + p[1]
开发者ID:Perth68,项目名称:topaz,代码行数:10,代码来源:test_ordereddict.py


示例5: popitem

 def popitem(n):
     o = OrderedDict()
     if n:
         o[n] = n
     try:
         key, val = o.popitem()
     except KeyError:
         return 400
     else:
         return key * 10 + val
开发者ID:Perth68,项目名称:topaz,代码行数:10,代码来源:test_ordereddict.py


示例6: __init__

 def __init__(self, cache, matches=None):
     self.cache = cache
     self._matches = OrderedDict()
     for match in (matches or []):
         self.append_match(match)
开发者ID:bitprophet,项目名称:topaz,代码行数:5,代码来源:glob.py


示例7: Glob

class Glob(object):
    def __init__(self, cache, matches=None):
        self.cache = cache
        self._matches = OrderedDict()
        for match in (matches or []):
            self.append_match(match)

    def matches(self):
        return self._matches.keys()

    def append_match(self, match):
        self._matches[match] = None

    def is_constant(self, part, flags):
        special_chars = "?*["
        if not (flags & FNM_NOESCAPE):
            special_chars += "\\"
        for ch in part:
            if ch in special_chars:
                return False
        return True

    def single_compile(self, glob, flags=0):
        parts = path_split(glob)

        if parts[-1] == "/":
            last = DirectoriesOnly(None, flags)
        else:
            file = parts.pop()
            if self.is_constant(file, flags):
                last = ConstantEntry(None, flags, file)
            else:
                last = EntryMatch(None, flags, file)

        while parts:
            sep_parts = []
            while parts and parts[-1] == "/":
                sep_parts.append(parts.pop())
            last.separator = "".join(sep_parts)
            if not parts:
                last = RootDirectory(last, flags)
            else:
                dir = parts.pop()
                if dir == "**":
                    if parts:
                        last = RecursiveDirectories(last, flags)
                    else:
                        last = StartRecursiveDirectories(last, flags)
                elif self.is_constant(dir, flags):
                    last = ConstantDirectory(last, flags, dir)
                else:
                    last = DirectoryMatch(last, flags, dir)

        return last

    def run(self, node):
        node.call(self, None)

    def glob(self, pattern, flags):
        if "{" in pattern:
            patterns = self.compile(pattern, flags)
            for node in patterns:
                self.run(node)
        else:
            node = self.single_compile(pattern, flags)
            if node:
                self.run(node)

    def process_braces(self, pattern, flags, i=0):
        should_escape = flags & FNM_NOESCAPE == 0
        patterns = []

        escaped = False
        pattern_start = i
        segments = [""]
        while i < len(pattern):
            ch = pattern[i]
            if ch == "\\" and should_escape and not escaped:
                escaped = True
            elif ch == ",":
                if escaped:
                    escaped = False
                else:
                    suffix = pattern[pattern_start:i]
                    patterns.extend(combine_segments(segments, suffix))
                    segments = [""]
                    pattern_start = i + 1
            elif ch == "}":
                if escaped:
                    escaped = False
                else:
                    suffix = pattern[pattern_start:i]
                    patterns.extend(combine_segments(segments, suffix))
                    return i, patterns
            elif ch == "{":
                if escaped:
                    escaped = False
                else:
                    suffix = pattern[pattern_start:i]
                    i, new_segs = self.process_braces(pattern, flags, i + 1)
#.........这里部分代码省略.........
开发者ID:bitprophet,项目名称:topaz,代码行数:101,代码来源:glob.py


示例8: W_HashObject

class W_HashObject(W_Object):
    classdef = ClassDef("Hash", W_Object.classdef, filepath=__file__)
    classdef.include_module(Enumerable)

    def __init__(self, space, klass=None):
        W_Object.__init__(self, space, klass)
        self.contents = OrderedDict(space.eq_w, space.hash_w)
        self.w_default = space.w_nil
        self.default_proc = None

    @classdef.singleton_method("allocate")
    def method_allocate(self, space, args_w):
        return W_HashObject(space, self)

    @classdef.singleton_method("try_convert")
    def method_try_convert(self, space, w_obj):
        if not space.is_kind_of(w_obj, space.w_hash):
            w_obj = space.convert_type(w_obj, space.w_hash, "to_hash", raise_error=False)
        return w_obj

    @classdef.method("initialize")
    def method_initialize(self, space, w_default=None, block=None):
        if w_default is not None:
            self.w_default = w_default
        if block is not None:
            self.default_proc = block

    @classdef.method("default")
    def method_default(self, space, w_key=None):
        if self.default_proc is not None and w_key is not None:
            return space.invoke_block(self.default_proc, [self, w_key])
        else:
            return self.w_default

    @classdef.method("default=")
    @check_frozen()
    def method_set_default(self, space, w_defl):
        self.default_proc = None
        self.w_default = w_defl

    @classdef.method("default_proc")
    def method_default_proc(self, space):
        if self.default_proc is None:
            return space.w_nil
        return space.newproc(self.default_proc)

    @classdef.method("[]")
    def method_subscript(self, space, w_key):
        try:
            return self.contents[w_key]
        except KeyError:
            return space.send(self, space.newsymbol("default"), [w_key])

    @classdef.method("fetch")
    def method_fetch(self, space, w_key, w_value=None, block=None):
        try:
            return self.contents[w_key]
        except KeyError:
            if w_value is not None:
                return w_value
            elif block is not None:
                return space.invoke_block(block, [w_key])
            else:
                raise space.error(space.w_KeyError, "key not found: %s" % space.send(w_key, space.newsymbol("inspect")))

    @classdef.method("store")
    @classdef.method("[]=")
    @check_frozen()
    def method_subscript_assign(self, space, w_key, w_value):
        if space.is_kind_of(w_key, space.w_string) and not space.is_true(space.send(w_key, space.newsymbol("frozen?"))):

            w_key = space.send(w_key, space.newsymbol("dup"))
            w_key = space.send(w_key, space.newsymbol("freeze"))
        self.contents[w_key] = w_value
        return w_value

    @classdef.method("length")
    @classdef.method("size")
    def method_size(self, space):
        return space.newint(len(self.contents))

    @classdef.method("empty?")
    def method_emptyp(self, space):
        return space.newbool(not bool(self.contents))

    @classdef.method("delete")
    @check_frozen()
    def method_delete(self, space, w_key, block):
        w_res = self.contents.pop(w_key, None)
        if w_res is None:
            if block:
                return space.invoke_block(block, [w_key])
            w_res = space.w_nil
        return w_res

    @classdef.method("clear")
    @check_frozen()
    def method_clear(self, space):
        self.contents.clear()
        return self
#.........这里部分代码省略.........
开发者ID:emi1337,项目名称:topaz,代码行数:101,代码来源:hashobject.py


示例9: copy

 def copy(n):
     o = OrderedDict(Simple.eq, Simple.hash)
     o[Simple(n)] = n
     d = o.copy()
     return d.values()[0] * 10 + len(d)
开发者ID:Perth68,项目名称:topaz,代码行数:5,代码来源:test_ordereddict.py


示例10: clear

 def clear():
     o = OrderedDict()
     o[3] = 4
     o.clear()
     return len(o)
开发者ID:Perth68,项目名称:topaz,代码行数:5,代码来源:test_ordereddict.py


示例11: keys

 def keys(n):
     o = OrderedDict()
     o[4] = 1
     o[5] = 2
     o[4] = 2
     return o.keys()[n]
开发者ID:Perth68,项目名称:topaz,代码行数:6,代码来源:test_ordereddict.py


示例12: __init__

 def __init__(self, space, klass=None):
     W_Object.__init__(self, space, klass)
     self.contents = OrderedDict(space.eq_w, space.hash_w)
     self.w_default = space.w_nil
     self.default_proc = None
开发者ID:Alexis-D,项目名称:topaz,代码行数:5,代码来源:hashobject.py


示例13: pop_default

 def pop_default(n, d):
     o = OrderedDict()
     o[1] = 12
     o[2] = 3
     return (o.pop(n, d) * 10) + len(o)
开发者ID:Perth68,项目名称:topaz,代码行数:5,代码来源:test_ordereddict.py


示例14: pop

 def pop(n):
     o = OrderedDict()
     o[1] = 12
     o[2] = 3
     return (o.pop(n) * 10) + len(o)
开发者ID:Perth68,项目名称:topaz,代码行数:5,代码来源:test_ordereddict.py


示例15: iteritems_next_method

 def iteritems_next_method(n):
     o = OrderedDict()
     o[n] = 5
     it = o.iteritems()
     return it.next()[1]
开发者ID:Perth68,项目名称:topaz,代码行数:5,代码来源:test_ordereddict.py


示例16: get

 def get(n):
     o = OrderedDict()
     o[4] = 3
     return o.get(n, 123)
开发者ID:Perth68,项目名称:topaz,代码行数:4,代码来源:test_ordereddict.py


示例17: keys_object

 def keys_object(n):
     o = OrderedDict()
     o[Simple(1)] = None
     o[Simple(2)] = None
     o[Simple(3)] = None
     return o.keys()[n].x
开发者ID:Perth68,项目名称:topaz,代码行数:6,代码来源:test_ordereddict.py


示例18: values

 def values(n):
     o = OrderedDict()
     o[4] = 1
     o[5] = 2
     o[4] = 3
     return o.values()[n]
开发者ID:Perth68,项目名称:topaz,代码行数:6,代码来源:test_ordereddict.py


示例19: W_HashObject

class W_HashObject(W_Object):
    classdef = ClassDef("Hash", W_Object.classdef, filepath=__file__)
    classdef.include_module(Enumerable)

    def __init__(self, space, klass=None):
        W_Object.__init__(self, space, klass)
        self.contents = OrderedDict(space.eq_w, space.hash_w)
        self.w_default = space.w_nil
        self.default_proc = None

    @classdef.singleton_method("allocate")
    def method_allocate(self, space, args_w):
        return W_HashObject(space, self)

    @classdef.singleton_method("[]")
    def singleton_method_subscript(self, space, w_obj=None):
        if w_obj is None:
            return W_HashObject(space)
        w_res = space.convert_type(w_obj, space.w_hash, "to_hash", raise_error=False)
        if w_res is space.w_nil:
            raise NotImplementedError
        assert isinstance(w_res, W_HashObject)
        result = W_HashObject(space)
        result.contents.update(w_res.contents)
        return result

    @classdef.method("initialize")
    def method_initialize(self, space, w_default=None, block=None):
        if w_default is not None:
            self.w_default = w_default
        if block is not None:
            self.default_proc = block

    @classdef.method("default")
    def method_default(self, space, w_key=None):
        if self.default_proc is not None:
            return space.invoke_block(self.default_proc, [self, w_key])
        else:
            return self.w_default

    @classdef.method("[]")
    def method_subscript(self, space, w_key):
        try:
            return self.contents[w_key]
        except KeyError:
            return space.send(self, space.newsymbol("default"), [w_key])

    @classdef.method("fetch")
    def method_fetch(self, space, w_key, w_value=None, block=None):
        try:
            return self.contents[w_key]
        except KeyError:
            if w_value is not None:
                return w_value
            elif block is not None:
                return space.invoke_block(block, [w_key])
            else:
                raise space.error(space.w_KeyError, "key not found: %s" % space.send(w_key, space.newsymbol("inspect")))

    @classdef.method("store")
    @classdef.method("[]=")
    def method_subscript_assign(self, w_key, w_value):
        self.contents[w_key] = w_value
        return w_value

    @classdef.method("length")
    @classdef.method("size")
    def method_size(self, space):
        return space.newint(len(self.contents))

    @classdef.method("empty?")
    def method_emptyp(self, space):
        return space.newbool(not bool(self.contents))

    @classdef.method("delete")
    @check_frozen()
    def method_delete(self, space, w_key, block):
        w_res = self.contents.pop(w_key, None)
        if w_res is None:
            if block:
                return space.invoke_block(block, [w_key])
            w_res = space.w_nil
        return w_res

    @classdef.method("clear")
    @check_frozen()
    def method_clear(self, space):
        self.contents.clear()
        return self

    @classdef.method("shift")
    @check_frozen()
    def method_shift(self, space):
        if not self.contents:
            return space.send(self, space.newsymbol("default"))
        w_key, w_value = self.contents.popitem()
        return space.newarray([w_key, w_value])

    @classdef.method("initialize_copy")
    @classdef.method("replace")
#.........这里部分代码省略.........
开发者ID:Alexis-D,项目名称:topaz,代码行数:101,代码来源:hashobject.py


示例20: W_HashObject

class W_HashObject(W_Object):
    classdef = ClassDef("Hash", W_Object.classdef)
    classdef.include_module(Enumerable)

    def __init__(self, space, klass=None):
        W_Object.__init__(self, space, klass)
        self.contents = OrderedDict(space.eq_w, space.hash_w)
        self.w_default = space.w_nil
        self.default_proc = None

    @classdef.singleton_method("allocate")
    def method_allocate(self, space):
        return W_HashObject(space, self)

    @classdef.method("initialize")
    @check_frozen()
    def method_initialize(self, space, w_default=None, block=None):
        if w_default is not None:
            if block is not None:
                raise space.error(space.w_ArgumentError, "wrong number of arguments")
            self.w_default = w_default
        if block is not None:
            self.default_proc = block
        return self

    @classdef.method("default")
    def method_default(self, space, w_key=None):
        if self.default_proc is not None and w_key is not None:
            return space.invoke_block(self.default_proc, [self, w_key])
        else:
            return self.w_default

    @classdef.method("default=")
    @check_frozen()
    def method_set_default(self, space, w_defl):
        self.default_proc = None
        self.w_default = w_defl

    @classdef.method("default_proc")
    def method_default_proc(self, space):
        if self.default_proc is None:
            return space.w_nil
        return self.default_proc

    @classdef.method("default_proc=")
    def method_set_default_proc(self, space, w_proc):
        w_new_proc = space.convert_type(w_proc, space.w_proc, "to_proc")
        assert isinstance(w_new_proc, W_ProcObject)
        arity = space.int_w(space.send(w_new_proc, "arity"))
        if arity != 2 and space.is_true(space.send(w_new_proc, "lambda?")):
            raise space.error(space.w_TypeError, "default_proc takes two arguments (2 for %s)" % arity)
        self.default_proc = w_new_proc
        self.w_default = space.w_nil
        return w_proc

    @classdef.method("[]")
    def method_subscript(self, space, w_key):
        try:
            return self.contents[w_key]
        except KeyError:
            return space.send(self, "default", [w_key])

    @classdef.method("fetch")
    def method_fetch(self, space, w_key, w_value=None, block=None):
        try:
            return self.contents[w_key]
        except KeyError:
            if block is not None:
                return space.invoke_block(block, [w_key])
            elif w_value is not None:
                return w_value
            else:
                raise space.error(space.w_KeyError, "key not found: %s" % space.send(w_key, "inspect"))

    @classdef.method("store")
    @classdef.method("[]=")
    @check_frozen()
    def method_subscript_assign(self, space, w_key, w_value):
        if space.is_kind_of(w_key, space.w_string) and not space.is_true(space.send(w_key, "frozen?")):

            w_key = space.send(w_key, "dup")
            w_key = space.send(w_key, "freeze")
        self.contents[w_key] = w_value
        return w_value

    @classdef.method("length")
    @classdef.method("size")
    def method_size(self, space):
        return space.newint(len(self.contents))

    @classdef.method("empty?")
    def method_emptyp(self, space):
        return space.newbool(not bool(self.contents))

    @classdef.method("delete")
    @check_frozen()
    def method_delete(self, space, w_key, block):
        w_res = self.contents.pop(w_key, None)
        if w_res is None:
            if block:
#.........这里部分代码省略.........
开发者ID:kachick,项目名称:topaz,代码行数:101,代码来源:hashobject.py



注:本文中的topaz.utils.ordereddict.OrderedDict类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python util.get_const_tuple函数代码示例发布时间:2022-05-27
下一篇:
Python stringobject.W_StringObject类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap