def pop_keyerror(n): o = OrderedDict() o[3] = 4 try: return o.pop(n) except KeyError: return 500
def delitem(n): o = OrderedDict() o[2] = 3 o[3] = 4 del o[n] vals = o.values() return vals[0] * 10 + len(vals)
class W_HashObject(W_Object): classdef = ClassDef("Hash", W_Object.classdef) def __init__(self, space): W_Object.__init__(self, space) self.contents = OrderedDict(space.eq_w, space.hash_w) @classdef.singleton_method("allocate") def method_allocate(self, space): return W_HashObject(space) @classdef.method("[]") def method_subscript(self, space, w_key): return self.contents.get(w_key, space.w_nil) @classdef.method("[]=") def method_subscript_assign(self, w_key, w_value): self.contents[w_key] = w_value return w_value @classdef.method("delete") def method_delete(self, space, w_key): return self.contents.pop(w_key, space.w_nil) @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 """) @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)
def merge_dicts(n): if n: o = OrderedDict() o[5] = 10 else: o = OrderedDict() o[2] = 20 o[3] = 30 return o[3]
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]
def get_set_object(n): x = Simple(n) o = OrderedDict() o[x] = x return o[x].x
def create(): OrderedDict() return 0
def simple_get_set(): o = OrderedDict() o["a"] = 2 return o["a"]
def pop_default(n, d): o = OrderedDict() o[1] = 12 o[2] = 3 return (o.pop(n, d) * 10) + len(o)
def get(n): o = OrderedDict() o[4] = 3 return o.get(n, 123)
def custom_eq_hash(n): o = OrderedDict(Simple.eq, Simple.hash) o[Simple(n)] = 23 return o[Simple(n)]
def pop(n): o = OrderedDict() o[1] = 12 o[2] = 3 return (o.pop(n) * 10) + len(o)
def values(n): o = OrderedDict() o[4] = 1 o[5] = 2 o[4] = 3 return o.values()[n]
def iteritems_next_method(n): o = OrderedDict() o[n] = 5 it = o.iteritems() return it.next()[1]
def keys_object(n): o = OrderedDict() o[Simple(1)] = None o[Simple(2)] = None o[Simple(3)] = None return o.keys()[n].x
def keys(n): o = OrderedDict() o[4] = 1 o[5] = 2 o[4] = 2 return o.keys()[n]
def grow(n): o = OrderedDict() for i in xrange(n): o[i] = -20 return o[3]
def contains(n): o = OrderedDict() o[4] = 5 return n in o
def len(n): o = OrderedDict() for i in xrange(n): o[i] = i return len(o)
def __init__(self, space): W_Object.__init__(self, space) self.contents = OrderedDict(space.eq_w, space.hash_w)