0
votes

In the Python program structure below there are two modules with functions.

Problem description:

  1. Functions are used inside other functions all over the place. Strong coupling (dependency). For example func1() in module 2 that reads from file is called many many times because it's called in almost every other function in both modules. That's resource and performance intensive, yet alone a bad practice.
  2. Reading from file so many times (each time funct1() is called) can be costly.

Question: What is a good way to improve this design?

For example, passing functions from helper module 2 to functions in module 1 AS PARAMETERS? [ If I do that, I still have functions in module 2 dependent upon each other (ie funct4() uses funcr1(), funct2(), funct3(), etc) ]

My paradigm I started with: I was thinking: "Ah, I'm going to have a main module where the main thing happens. Then I will have a side module for functions that do some prep work and clean-up and called them from module 1." (Well, now I have this mess.) I wonder if a good design is to make functions less dependent on each other? Each function does one thing and DOESN'T call other functions?? Should I use classes? How? Is it about when to use classes versus modules? Functional versus OO design?

How would you redesign this so as little coupling as possible and file is read only once (or as few times as possible)?

Thanks for any tips.

module 1

(main module, program execution happens here)

from module2 import *

f1()
- uses funct1() 4 times
- uses funct2() 2 times

f2()
- uses func1() 2 times
- uses func2() 2 times

f3()
- uses func1() 1 time

f4()
- uses func2() 3 times
- uses func3() 3 times
- uses func4() 1 time

f5()
- uses func1() 2 times
- uses func2() 2 times
- uses func4() 4 times

f6()
- uses func1() 3 times
- uses func2() 3 times
- uses func4() 2 times

module 2

(has helper functions that provide clean up and other functionality to module 1)

func1()
reads from file (with open('x.txt', 'r') af f: ...

func2()
- uses func1 1 time

func3()
- uses func1 1 time

func4()
- uses func1() 1 time
- uses func2() 2 times - uses func3() 1 time

1
Some of these questions are very subjective and all will be hard to answer without more information. If you are just asking about these ideas in general programmers.stackexchange.com might be a better place to ask. If you have a specific problem in mind, then how large is the file? Do all of these functions need the full content of the file? Is there some structure to the file (i.e. csv or json)? Are you only reading from the file? What does the program do? - Matt
It's XML parser written in Python. There is one XML file which the program reads and then processes. Module 1 does main checks for syntax validity. Module 2 provides some helper functionality for example gets all tags as a list, counts angle brackets, gets tag names, data content between tgs etc. The application is 2 modules long, as you see it in the question (about 10 functions in module 1 and about 6 in module 2). XML file that is read can be any size. - user1552294
Most functions do need to access the XML file. The problem I have is that in all those functions I simply call get_xml_file() method which means that file is read many times. That seems to me a problem by itself and I don't know how to change it.Further to that I use function that calls get_xml_file() in another function that does something else, so the file is read twice (get_xml_file() is called twice) without needing to. There must be a better design...? - user1552294
You'll probably want to make your xml reader a class if you haven't already. Then you can pass the file object in to the constructor (take a look at pythons's CSV module). If the file contents fit in memory, you could then just read the file into a class member variable. Better would probably be to keep the file object and only read in as much as you need at any one time (use the seek method to move back to the start of the file). This will also let you access the file from other class methods as just self.file rather than calling a function. - Matt

1 Answers

0
votes

Assuming that the file is not being written to, I have created a very general outline of one way to cut down on the number of times that the file is being opened and closed. As well as the number of times the other functions are being called. They should only run one time each on the initialization of Helpers(). Although, this doesn't really decrease the amount of coupling, because of the nature of what a helper is.

module2:

class Helpers(object):
    def __init__(self, fp=None):
        self.contents = self.func1(self.fp)
        self.func2_results = self.func2()
        self.func3_results = self.func3()
        self.func4_results = self.func4()
        self.results = {'name2':self.func2_results,
                        'name3':self.func3_results,
                        'name4':self.func4_results
                       }

    def func1(self):
        # read file
        with open(...) as f:
            contents = f.read()
        return contents

    def func2(self):
        # doStuff(self.contents)
        return something

    def func3(self):
        # doStuff(self.contents)
        return something

    def func4(self):
        # func2 and func3 were already run once in init
        # so lets use the results from them
        # self.func2_results
        # self.func2_results
        # self.func3_results
        return something       

module1:

from module2 import Helpers

class Main(object):
    def __init__(self, fp=None):
        self.helper = Helpers(self.fp)
        self.contents = self.helper.contents
        self.name2 = self.helper.results['name2']
        self.name3 = self.helper.results['name3']
        self.name4 = self.helper.results['name4']
    def f1(self):
        # doStuff(self.contents)
        # doStuff(self.name2)
        return thing1
    def f2(self):
        # doStuff(self.contents)
        # doStuff(self.name2)
        return thing2
    def f3(self):
        # doStuff(self.helper.contents)
        return thing3
    def f4(self):
        # doStuff(self.name2)
        # doStuff(self.name3)
        # doStuff(self.name4)
        return thing4
    def f5(self):
        # doStuff(self.contents)
        # doStuff(self.name2)
        # doStuff(self.name4)
        return thing5
    def f6(self):
        # doStuff(self.contents)
        # doStuff(self.name2)
        # doStuff(self.name4)
        return thing6

And then it could be run like:

from module1 import Main

here = Main(fp="/some/file.xml")
here2 = Main(fp="/some/file2.xml")
print(here.name2)
print(here.name3)
print(here.name4)
# each method will have only run once 
f1 = here.f1()
f2 = here.f2()
f3 = here.f3()
f4 = here.f4()
f5 = here.f5()
f6 = here.f6()
# these will run the helpers again from within `Helpers()`
# say for example if the file contents have been updated
h1 = here.helper.func1()
h2 = here.helper.func2()
h3 = here.helper.func3()
h4 = here.helper.func4()