<reponame>RGL-83/classify<filename>classify.py<gh_stars>0


class builder():
    def __init__(self,class_name,attributes):
        self._class_name = str()
        self._attributes = tuple()
        
        self.set_initial_attributes(class_name,attributes)
        self.build()

    def set_initial_attributes(self,class_name,attributes):
        self.set_class_name(class_name)
        self.set_attributes(attributes)

    def build(self):
        file_name = f'{self.get_class_name()}.py'
        file_content = '# class generated by classify.builder #\n\n'
        file_content += f'class {self.get_class_name()}():\n'
        file_content += f'\tdef __init__(self,{self.attribute_names()}):\n'
        for attrb in self.get_attributes():
            file_content += f'\t\tself._{attrb[0]} = {attrb[1]}\n'
        file_content += f'\n\t\tself._set_initial_attributes({self.attribute_names()})\n\n'

        file_content += f'\tdef _set_initial_attributes(self,{self.attribute_names()}):\n'
        for attrb in self.get_attributes():
            file_content += f'\t\tself.set_{attrb[0]}({attrb[0]})\n'

        for attrb in self.get_attributes():
            file_content += f'\n\tdef get_{attrb[0]}(self):\n\t\treturn self._{attrb[0]}\n'

        for attrb in self.get_attributes():
            file_content += f'\n\tdef set_{attrb[0]}(self,{attrb[0]}):\n'
            file_content += f'\t\tif type({attrb[0]}) != type(self.get_{attrb[0]}()): raise TypeError\n'
            file_content += f'\t\tself._{attrb[0]} = {attrb[0]}\n'

        with open(file_name,'w') as f:
            f.writelines(file_content)

        print(f"Class: {self.get_class_name()} has been built --> {file_name}")

    def attribute_names(self):
        names = [n[0] for n in self.get_attributes()]
        names = ",".join(names)
        return names

    def get_class_name(self):
        return self._class_name

    def get_attributes(self):
        return self._attributes

    def set_class_name(self,class_name):
        if type(class_name) != type(self.get_class_name()): raise TypeError
        self._class_name = class_name

    def set_attributes(self,attributes):
        if type(attributes) != type(self.get_attributes()): raise TypeError
        self._attributes = attributes

if __name__ == "__main__":
    b = builder('test_class',(('name','str()'),('feature','str()')))
class builder():
    def __init__(self,class_name,attributes):
        self._class_name = str()
        self._attributes = tuple()
        
        self.set_initial_attributes(class_name,attributes)
        self.build()

    def set_initial_attributes(self,class_name,attributes):
        self.set_class_name(class_name)
        self.set_attributes(attributes)

    def build(self):
        file_name = f'{self.get_class_name()}.py'
        file_content = '# class generated by classify.builder #\n\n'
        file_content += f'class {self.get_class_name()}():\n'
        file_content += f'\tdef __init__(self,{self.attribute_names()}):\n'
        for attrb in self.get_attributes():
            file_content += f'\t\tself._{attrb[0]} = {attrb[1]}\n'
        file_content += f'\n\t\tself._set_initial_attributes({self.attribute_names()})\n\n'

        file_content += f'\tdef _set_initial_attributes(self,{self.attribute_names()}):\n'
        for attrb in self.get_attributes():
            file_content += f'\t\tself.set_{attrb[0]}({attrb[0]})\n'

        for attrb in self.get_attributes():
            file_content += f'\n\tdef get_{attrb[0]}(self):\n\t\treturn self._{attrb[0]}\n'

        for attrb in self.get_attributes():
            file_content += f'\n\tdef set_{attrb[0]}(self,{attrb[0]}):\n'
            file_content += f'\t\tif type({attrb[0]}) != type(self.get_{attrb[0]}()): raise TypeError\n'
            file_content += f'\t\tself._{attrb[0]} = {attrb[0]}\n'

        with open(file_name,'w') as f:
            f.writelines(file_content)

        print(f"Class: {self.get_class_name()} has been built --> {file_name}")

    def attribute_names(self):
        names = [n[0] for n in self.get_attributes()]
        names = ",".join(names)
        return names

    def get_class_name(self):
        return self._class_name

    def get_attributes(self):
        return self._attributes

    def set_class_name(self,class_name):
        if type(class_name) != type(self.get_class_name()): raise TypeError
        self._class_name = class_name

    def set_attributes(self,attributes):
        if type(attributes) != type(self.get_attributes()): raise TypeError
        self._attributes = attributes

if __name__ == "__main__":
    b = builder('test_class',(('name','str()'),('feature','str()')))