Search code examples
pythonfunctionfileclassparameters

Problem with functions from a class not taking arguments


I made a class, and I made functions for that class for me to use later (the functions have parameters), but I'm having a problem where one of my functions won't take an argument. I think this might be because it is nested into another function within the class (both are defined within the class) but I'm pretty new to programming so I'm quite confused.

class Encoder():
    def __init__(self):
        self.chars = " " + string.punctuation + string.digits + string.ascii_letters
        self.chars = list(self.chars)
        self.key = self.chars.copy()
        random.shuffle(self.key)
    
    def create_files(self):
        with open("key.txt", 'a') as key_file:
                key_file.write(str(self.key))  

        with open("encrypted.txt", 'a') as encrypted_file:
            encrypted_file.write("new file")
        
    def file_to_chars(Encoder, file_name):
        with open(file_name, 'r') as user_file:
                words = user_file.readlines()
                chars_list = []
                for word in words:
                    for char in list(word):
                        chars_list.append(char)
                while "\n" in chars_list:
                    chars_list.remove("\n")
                    
        return chars_list            


    def encode(self, user_input):
        original = self.file_to_chars(Encoder, user_input)
        encryption = ""
        for letter in original:
            index = self.chars.index(letter)
            encryption += self.key[index]
        
        with open("encrypted.txt", 'w') as encrypted_file:
            encrypted_file.write(str(encryption))
        try:
            return open("encrypted.txt", 'r')
        except:
            print("Error, try again. ")

When I try to pass in an argument for "user_input" in the encode() function, I get an error stating that the files_to_chars() has no argument for file_name.

from encoder import Encoder

ui = input('name of file: ')
f=Encoder.encode(Encoder, ui)
print(f.read())

Here is an example of how it's used.

I tried putting Encoder, as the first parameter of the files_to_chars function but that didn't seem to work and I just got the same error. I'm pretty lost and any help would be great.


Solution

  • class Encoder:
    def __init__(self):
        self.chars = " " + string.punctuation + string.digits + string.ascii_letters
        self.chars = list(self.chars)
        self.key = self.chars.copy()
        random.shuffle(self.key)
    
    def create_files(self):
        with open("key.txt", "a") as key_file:
            key_file.write(str(self.key))
    
        with open("encrypted.txt", "a") as encrypted_file:
            encrypted_file.write("new file")
    
    def file_to_chars(self, file_name): # Change Encoder to self
        with open(file_name, "r") as user_file:
            words = user_file.readlines()
            chars_list = []
            for word in words:
                for char in list(word):
                    chars_list.append(char)
            while "\n" in chars_list:
                chars_list.remove("\n")
    
        return chars_list
    
    def encode(self, user_input):
        original = self.file_to_chars(user_input) # Not need to pass Encoder
        encryption = ""
        for letter in original:
            index = self.chars.index(letter)
            encryption += self.key[index]
    
        with open("encrypted.txt", "w") as encrypted_file:
            encrypted_file.write(str(encryption))
        try:
            return open("encrypted.txt", "r")
        except:
            print("Error, try again. ")
    

    I have do some changes.