<commit_before>import os
import random
import zlib
from collections import MutableSequence, Iterable
from numbers import Real
import operator
from functools import reduce

import numpy as np
import bottleneck as bn

from .instance import *
from Orange.data import io

class RowInstance(Instance):
    def __init__(self, table, row_index):
        super().__init__(table.domain)
        self._x = table._X[row_index]
        self._y = table._Y[row_index]
        self._values = np.hstack((self._x, self._y))
        self._metas = table._metas[row_index]
        self.row_index = row_index
        self.table = table

    def get_class(self):
        self._check_single_class()
        if self.table.domain.class_var:
            return Value(self.table.domain.class_var, self._y[0])

    def set_class(self, value):
        self._check_single_class()
        if not isinstance(value, Real):
            self._y[0] = self.table.domain.class_var.to_val(value)
        else:
            self._y[0] = value
        self._values[len(self.table.domain.attributes)] = self._y[0]

    def get_classes(self):
        return (Value(var, value) for var, value in
            zip(self.table.domain.class_vars, self._y))

    def set_weight(self, weight):
        if self.table._W is None:
            self.table.set_weights()
        self.table._W[self.row_index] = weight

    def get_weight(self):
        if not self.table._W:
            raise ValueError("Instances in the referenced table have no weights")
        return self.table._W[self.row_index]

    def __setitem__(self, key, value):
        if not isinstance(key, int):
            key = self.domain.index(key)
        if isinstance(value, str):
            var = self.domain[key]
            value = var.to_val(value)
        if key >= 0:
            if not isinstance(value, Real):
                raise TypeError("Expected primitive value, got '%s'" %
                                type(value).__name__)
            if key < len(self._x):
                self._values[key] = self._x[key] = value
            else:
                self._values[key] = self._y[key - len(self._x)] = value
        else:
            self._metas[-1-key] = value


class Columns:
    def __init__(self, domain):
        for v in domain:
            setattr(self, v.name.replace(" ", "_"), v)
        for v in domain.metas:
            setattr(self, v.name.replace(" ", "_"), v)

class Table(MutableSequence):
    def __new__(cls, *args, **argkw):
        self = None
        if not args:
            if not args and not argkw:
                self = super().__new__(cls)
            elif "filename" in argkw:
                self = cls.read_data(argkw["filename"])
        elif len(args) == 1:
            arg = args[0]
            if isinstance(arg, str):
                self = Table.read_data(args[0])
            elif isinstance(arg, Domain):
                self = Table.new_from_domain(arg)
        elif 2 <= len(args) <= 5:
            if isinstance(args[0], Domain) and \
               all(isinstance(arg, np.ndarray) for arg in args[1:]):
                self = Table.new_from_numpy(*args)
        if self is None:
            raise ValueError("Invalid arguments for Table.__new__")
        self.clear_cache()
        return self

    @staticmethod
    def new_from_domain(domain, n_rows=0, weights=True):
        self = Table.__new__(Table)<commit_msg>Refactoring.<commit_after>import os
import random
import zlib
from collections import MutableSequence, Iterable
from itertools import chain
from numbers import Real
import operator
from functools import reduce

import numpy as np
import bottleneck as bn

from .instance import *
from Orange.data import io

class RowInstance(Instance):
    def __init__(self, table, row_index):
        super().__init__(table.domain)
        self._x = table._X[row_index]
        self._y = table._Y[row_index]
        self._values = np.hstack((self._x, self._y))
        self._metas = table._metas[row_index]
        self.row_index = row_index
        self.table = table

    def get_class(self):
        self._check_single_class()
        if self.table.domain.class_var:
            return Value(self.table.domain.class_var, self._y[0])

    def set_class(self, value):
        self._check_single_class()
        if not isinstance(value, Real):
            self._y[0] = self.table.domain.class_var.to_val(value)
        else:
            self._y[0] = value
        self._values[len(self.table.domain.attributes)] = self._y[0]

    def get_classes(self):
        return (Value(var, value) for var, value in
            zip(self.table.domain.class_vars, self._y))

    def set_weight(self, weight):
        if self.table._W is None:
            self.table.set_weights()
        self.table._W[self.row_index] = weight

    def get_weight(self):
        if not self.table._W:
            raise ValueError("Instances in the referenced table have no weights")
        return self.table._W[self.row_index]

    def __setitem__(self, key, value):
        if not isinstance(key, int):
            key = self.domain.index(key)
        if isinstance(value, str):
            var = self.domain[key]
            value = var.to_val(value)
        if key >= 0:
            if not isinstance(value, Real):
                raise TypeError("Expected primitive value, got '%s'" %
                                type(value).__name__)
            if key < len(self._x):
                self._values[key] = self._x[key] = value
            else:
                self._values[key] = self._y[key - len(self._x)] = value
        else:
            self._metas[-1-key] = value


class Columns:
    def __init__(self, domain):
        for v in chain(domain, domain.metas):
            setattr(self, v.name.replace(" ", "_"), v)

class Table(MutableSequence):
    def __new__(cls, *args, **argkw):
        if not args and not argkw:
            return super().__new__(cls)
        if "filename" in argkw:
           return cls.read_data(argkw["filename"])
        try:
            if isinstance(args[0], str):
                return cls.read_data(args[0])
            if isinstance(args[0], Domain):
                return cls.new_from_domain(args[0])
            if isinstance(args[0], Domain) and \
               all(isinstance(arg, np.ndarray) for arg in args[1:]):
                return cls.new_from_numpy(*args)
        except IndexError:
            pass
        raise ValueError("Invalid arguments for Table.__new__")

    @staticmethod
    def new_from_domain(domain, n_rows=0, weights=True):
        self = Table.__new__(Table)
{'fraction_non_alphanumeric': 0.10895295902883156, 'fraction_numerical': 0.00409711684370258, 'mean_word_length': 2.0784680056048574, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7179370', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1932, 'n_words': 490}