#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0

    How to shorten many "def" ?


    How to shorten a module with many "def", but in the same time peanut, love_spaghetti, grooming_my_dog, arnold_terminator, eat_that_chocolate, caribbean_summer, list_with_my_fv_movies to open the appropriate files, read and close them, also they have to remain defined because I use them.

    Code:
    def bt(butter_file):
        with codecs.open("butter.txt", mode="r", encoding="utf8") as a:
            return a.read()
            return a.close()
    peanut_butter = bt("butter.txt")
    
    
    def sp(spaghetti_file):
        with codecs.open("spaghetti.txt", mode="r", encoding="utf8") as b:
            return b.read()
            return b.close()
    love_spaghetti = sp("spaghetti.txt")
    
    
    def gr(grooming_file):
        with codecs.open("grooming.txt", mode="r", encoding="utf8") as c:
            return c.read()
            return c.close()
    grooming_my_dog = gr("grooming.txt")
    
    
    def at(terminator_file):
        with codecs.open("terminator.txt", mode="r", encoding="utf8") as d:
            return d.read()
            return d.close()
    arnold_terminator = at("terminator.txt")
    
    
    def ch(chocolate_file):
        with codecs.open("chocolate.txt", mode="r", encoding="utf8") as e:
            return e.read()
            return e.close()
    eat_that_chocolate = ch("chocolate.txt")
    
    
    def sm(summer_vacation):
        with codecs.open("summer.txt", mode="r", encoding="utf8") as f:
            return f.read()
            return f.close()
    caribbean_summer = sm("summer.txt")
    
    
    def fv(favourite_movie):
        with codecs.open("movies.txt", mode="r", encoding="utf8") as g:
            return g.read()
            return g.close()
    list_with_my_fv_movies = fv("movies.txt")
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Let's start by examining one of the functions.
    Code:
    def at(terminator_file):
        with codecs.open("terminator.txt", mode="r", encoding="utf8") as d:
            return d.read()
            return d.close()
    • The statement "return d.close()" is never executed because you returned from the function on previous line.
    • The file context incorporated by the with statement closes the file for you.
    • The variable "d" is local to function at. It can have the same name as similar variables in the other functions without conflict.
    • The argument terminator_file is not used within the function. Remove it.
    The following simplification
    • does not repeat file names.
    • reduces duplicate code. We abhor duplicate code.
    • the __call__ method makes FixedFileReader objects callable.
    Code:
    # Untested.  My untested programs almost always fail.
    
    class FixedFileReader:
    
        def __init__(self, filename):
            self.filename = filename
    
        def __call__(self):
            with codecs.open(self.filename, mode="r", encoding="utf8") as a:
                return a.read()
    
        def __str__(self):
            return self.filename
    
    
    sp = FixedFileReader("spaghetti.txt")
    gr = FixedFileReader("grooming.txt")
    #etceteras
    
    love_spaghetti = sp()
    grooming_my_dog = gr()
    #etc
    Hard to believe that you need to reread the files. Without knowing more, I'd cache the content in a dictionary. Your operating system will probably cache the files for you in RAM; this issue might not matter a whole lot. Avoid disk io. More importantly, avoid paging.
    Last edited by b49P23TIvg; September 12th, 2013 at 09:41 AM. Reason: expanded possible tabs.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    Originally Posted by b49P23TIvg
    ...
    Wow, wow, wow, wow. It works out of the box. Thank you so much b49P23TIvg, now I am certaint that if I ever need a help will seek you. Thanks again, im crying from happines.

IMN logo majestic logo threadwatch logo seochat tools logo