Search code examples
pythonclasscode-reusereusability

Converting Python functions to classes for reusability


I have a piece of written code,

class Corpus:
    def __init__(self):
        return self

    def __iter__(self):
        return self

    def __next__(self):
        return self

    def getwords():
        pass
if __name__ == "__main__":

    texts = []
    if len(sys.argv) >= 2:
        for filename in sys.argv[1:]:
            texts.append(str(filename))
    else:
        print("Error!", sys.stderr())

    removables = [".", ",", "!", "?", "(", ")"]

    text = ""
    for filename in texts:
        with open(filename) as f:
            for line in f:
                text += line
    words = text.lower().split()

    allwords = {}
    for word in words:
        for removable in removables:
            if removable in word:
                word = word.replace(removable, "")
        if word in allwords:
            allwords[word] += 1
        else:
            allwords[word] = 1
    print(allwords)

    wordsearch = input("Enter word to search : ")
    filesearch = input("Enter file to search in: ")
    with open(filesearch) as f2:
        for line in f2:
            if wordsearch in line:
                print(line)

    wordsunique = len(allwords)
    wordtotal = len(words)
    filetotal = len(texts)


    print("Total number of words: ", wordtotal)
    print("Total number of unique words: ", wordsunique)
    print("Total number of files: ", filetotal)

It's a simple wordcounter. I would like to, however, rather put the sections that determine the filename and word to search for into a class so that the code os reusable for a bigger project I'm working on. Could anyone point me in the right direction?


Solution

  • The short answer is one method per task e.g.

    class WordCounter(object):
    
        def __call__(*filenames):
    
            self.validate_arguments(filenames)
    
            all_words = self.get_words_from_files(filenames)
    
            counted_words = self.count_all_words()
    
            word_to_search_for = self.get_word_from_user()
    

    That along with giving everything sensible names tends to make life easier for reuse. Sensible names makes it easy to know what the method does, one method per task makes it easy to reuse. If a method does a lot of work then it becomes very specialized and is unlikely to be able to be reused a lot.

    I recommend checking out a book like Code Complete if you want to read more on the subject or check out some google answers.