from classifier.exceptions import sistemhatasi as system, gecersizError as inv
import item, copy

class Instance:
    def __init__(self):
        self.klass_value, self.attrs, self.classified_klass = None, None, None
        
    def is_valid(self, klass, attributes):
      
        return attributes.has_values(self.attrs)
    
    def value(self, attribute):
        
        if attribute.is_continuous():
            return float(self.attrs[attribute.index])
        return self.attrs[attribute.index]
    
    def values(self, attributes):
      
        return [self.attrs[attribute.index] for attribute in attributes]

    def discretise(self, discretised_attributes):
       
        for discretised_attribute in discretised_attributes:
            index = discretised_attribute.index
            self.attrs[index] = discretised_attribute.mapping(float(self.attrs[index]))
    
    def remove_attributes(self, attributes):
     
        to_be_removed = [attribute.index for attribute in attributes]
        to_be_removed.sort()
        to_be_removed.reverse()
        for r in to_be_removed:
            self.attrs.__delitem__(r)
            
    def convert_to_float(self, indices):
    
        for index in indices:
            self.attrs[index] = float(self.attrs[index])
    
    def __eq__(self, other):
        if other is None: return False
        if self.__class__ != other.__class__: return False
        if self.klass_value == other.klass_value and self.attrs == other.attrs and self.classified_klass == other.classified_klass: return True
        return False
    
    def str_klassified_klass(self):
     
        return self.__check_none(self.classified_klass)

    def __check_none(self, var):
        if var is None: 
            return ' '
        return var.__str__()

    def str_class(self):
      
        return self.__check_none(self.klass_value)

    def str_attrs(self):
       
        return ','.join([self.__check_none(each) for each in self.attrs])
    
    def __str__(self):
        return '[' + ';'.join(self.as_str()) + ']'
    
    def as_str(self):
     
        return [self.str_attrs()]