<commit_before>from topaz.module import ClassDef, check_frozen
from topaz.modules.enumerable import Enumerable
from topaz.objects.objectobject import W_Object
from topaz.utils.ordereddict import OrderedDict


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 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("[]=")
    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")<commit_msg>fix hash specs<commit_after>from topaz.module import ClassDef, check_frozen
from topaz.modules.enumerable import Enumerable
from topaz.objects.objectobject import W_Object
from topaz.utils.ordereddict import OrderedDict


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 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("[]=")
    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"), [space.w_nil])
        w_key, w_value = self.contents.popitem()
        return space.newarray([w_key, w_value])

    @classdef.method("initialize_copy")
{'fraction_non_alphanumeric': 0.10487927565392353, 'fraction_numerical': 0.0, 'mean_word_length': 2.714619336758524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1852222', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2455, 'n_words': 528}