本文整理汇总了Python中topaz.utils.ordereddict.OrderedDict.keys方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict.keys方法的具体用法?Python OrderedDict.keys怎么用?Python OrderedDict.keys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类topaz.utils.ordereddict.OrderedDict
的用法示例。
在下文中一共展示了OrderedDict.keys方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: W_HashObject
# 需要导入模块: from topaz.utils.ordereddict import OrderedDict [as 别名]
# 或者: from topaz.utils.ordereddict.OrderedDict import keys [as 别名]
#.........这里部分代码省略.........
@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")
@check_frozen()
def method_replace(self, space, w_hash):
assert isinstance(w_hash, W_HashObject)
self.contents.clear()
self.contents.update(w_hash.contents)
return self
@classdef.method("keys")
def method_keys(self, space):
return space.newarray(self.contents.keys())
@classdef.method("values")
def method_values(self, space):
return space.newarray(self.contents.values())
@classdef.method("to_hash")
def method_to_hash(self, space):
return self
classdef.app_method("""
def each
iter = Topaz::HashIterator.new(self)
while true
begin
key, value = iter.next()
rescue StopIteration
return
end
yield key, value
end
end
alias each_pair each
def each_key
each { |k, v| yield k }
end
""")
@classdef.method("key?")
@classdef.method("has_key?")
@classdef.method("member?")
@classdef.method("include?")
def method_includep(self, space, w_key):
return space.newbool(w_key in self.contents)
classdef.app_method("""
def ==(other)
if self.equal?(other)
return true
end
if !other.kind_of?(Hash)
return false
end
if self.size != other.size
return false
end
self.each do |key, value|
if !other.has_key?(key) || other[key] != value
return false
end
end
return true
end
""")
示例2: Glob
# 需要导入模块: from topaz.utils.ordereddict import OrderedDict [as 别名]
# 或者: from topaz.utils.ordereddict.OrderedDict import keys [as 别名]
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)
#.........这里部分代码省略.........
示例3: W_HashObject
# 需要导入模块: from topaz.utils.ordereddict import OrderedDict [as 别名]
# 或者: from topaz.utils.ordereddict.OrderedDict import keys [as 别名]
#.........这里部分代码省略.........
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
@classdef.method("shift")
@check_frozen()
def method_shift(self, space):
if not self.contents:
return space.send(self, space.newsymbol("default"), [space.w_nil])
w_key, w_value = self.contents.popitem()
return space.newarray([w_key, w_value])
@classdef.method("initialize_copy")
@classdef.method("replace")
@check_frozen()
def method_replace(self, space, w_hash):
assert isinstance(w_hash, W_HashObject)
self.contents.clear()
self.contents.update(w_hash.contents)
return self
@classdef.method("keys")
def method_keys(self, space):
return space.newarray(self.contents.keys())
@classdef.method("values")
def method_values(self, space):
return space.newarray(self.contents.values())
@classdef.method("to_hash")
def method_to_hash(self, space):
return self
@classdef.method("key?")
@classdef.method("has_key?")
@classdef.method("member?")
@classdef.method("include?")
def method_includep(self, space, w_key):
return space.newbool(w_key in self.contents)
示例4: keys_object
# 需要导入模块: from topaz.utils.ordereddict import OrderedDict [as 别名]
# 或者: from topaz.utils.ordereddict.OrderedDict import keys [as 别名]
def keys_object(n):
o = OrderedDict()
o[Simple(1)] = None
o[Simple(2)] = None
o[Simple(3)] = None
return o.keys()[n].x
示例5: keys
# 需要导入模块: from topaz.utils.ordereddict import OrderedDict [as 别名]
# 或者: from topaz.utils.ordereddict.OrderedDict import keys [as 别名]
def keys(n):
o = OrderedDict()
o[4] = 1
o[5] = 2
o[4] = 2
return o.keys()[n]
示例6: W_HashObject
# 需要导入模块: from topaz.utils.ordereddict import OrderedDict [as 别名]
# 或者: from topaz.utils.ordereddict.OrderedDict import keys [as 别名]
#.........这里部分代码省略.........
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:
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, "default", [space.w_nil])
w_key, w_value = self.contents.popitem()
return space.newarray([w_key, w_value])
@classdef.method("initialize_copy")
@classdef.method("replace")
@check_frozen()
def method_replace(self, space, w_hash):
w_hash = space.convert_type(w_hash, space.w_hash, "to_hash")
assert isinstance(w_hash, W_HashObject)
self.contents.clear()
self.contents.update(w_hash.contents)
self.w_default = w_hash.w_default
self.default_proc = w_hash.default_proc
return self
@classdef.method("keys")
def method_keys(self, space):
return space.newarray(self.contents.keys())
@classdef.method("values")
def method_values(self, space):
return space.newarray(self.contents.values())
@classdef.method("to_hash")
def method_to_hash(self, space):
return self
@classdef.method("key?")
@classdef.method("has_key?")
@classdef.method("member?")
@classdef.method("include?")
def method_includep(self, space, w_key):
return space.newbool(w_key in self.contents)