There are at least two questions being asked here (arguably a lot more, so I'll have to ignore many of the little ones), but the solution to both comes down to writing a class that effectively encapsulates retrieving (your) data from (your) files. Since you specifically asked whether it should be a class or something else, in this language classes are the main data abstraction that's capable of remembering things, which is obviously necessary if you want to avoid reading the same file over and over, so that's why this should be a class.
Since you haven't told me what any of the functions in module 2 actually do after calling func1, I'm going to make up some fairly generic requirements. Let's pretend func1 reads a comma-separated value file of numbers. Say func2 gets the sum of all numbers. Say func3 gets the average and standard distribution of each row.
What you want to do is write a class that handles reading and parsing these files, and exposes methods to retrieve certain aspects of the data it finds. For instance, you might have the class' constructor take a single filename/filepath argument, so the file gets read exactly once during construction, and all method calls on the object merely manipulate the in-memory representation of the data you've already read. The important thing is that no other module should know (exactly) how this class gets its data. They should see getNums() and getStdDev() and getTotal() methods, but they probably shouldn't know whether these values come from a .csv file or an Excel spreadsheet or a website or wherever, and they certainly should not know whether that file/site is being cached or not.
If you do this right, it will be trivial to add getEigenvectors() and whatnot to module2 whenever you feel like it without even thinking about what module1 is doing. That is always the main goal of reducing coupling. This applies to pretty much every language that supports OOP.
If you need more specific help you'll have to explain more about what these functions actually do and why the generic advice I just gave is insufficient.
I have a similar system but have a different concept:
- I use
cron
to auto-start processes
- Check if related process is running
- If it is not running, then start the process.
The module uses psutil
package to get a list of running processes, search for the related process and returns whether is it in the list or not. That may sound practical or not according to your use case though:
import psutil
class ProcessControl(object):
"""
This program checks whether the given python file is running or not. If check_params flag is set as True, then
it will be checked if an instance of the python file is running with all of the given paramters. If check_params
flag sets as False, then the check will be made with only using the python file name and instances of the file
running with different parameters will also be count.
"""
def __init__(self, filename, *args):
self.filename = filename
self.args = args
self.arg_num = len(args)
def process_count(self, check_params):
process_count = 0
# Examine the process list to check if given process is running or not...
for _prs in psutil.process_iter():
try:
_cmdline = _prs.cmdline()
except TypeError:
_cmdline = _prs.cmdline
if len(_cmdline) >= 2 and "python" in _cmdline[0] and self.filename in _cmdline[1] and _prs.is_running():
# We found an instance of the process that is running. Since This control function is triggered when
# we run the python code, There would be at least one process (this one) which is running. Counting 2
# or more processes means there is another instance which is still running when we trigger the python
# code file
if check_params:
# We also will check the parameters for the complete similarity
if len(_cmdline) == 2 + self.arg_num and all(str(_arg) in _cmdline[2:] for _arg in self.args):
process_count += 1
else:
# This is no match...
pass
else:
process_count += 1
else:
# This is no match....
pass
return process_count
def is_running(self, check_params=True):
return self.process_count(check_params) > 1
I have little python files which have similar code as below:
Ex:
myFile.py
class MyCodeClass:
def run_code(self):
...
if __name__ == "__main__":
my_code = MyCodeClass()
try:
if ProcessControl(__file__).is_running():
print "Already Running"
else:
my_code.run_code()
except Exception as e:
print e
And finally I have lines that trigger this file in my cron
:
* * * * * python myFile.py
Logic:
cron
triggers this file every x
minutes. ProcessControl
checks whether file is running or not. When calling is_running
method, you can pass a bool value so the controller will ignore the parameters passed while running the python file. Like if check_params
is True
following two commands will be accepted as different and both will be triggered:
python myOtherFile.py 127.0.0.1 220
python myOtherFile.py 127.0.0.1 250
and disabling check_params
will evaluate the second call as the same program and will not trigger it
Best Answer
A python module is nothing but a package to encapsulate reusable code. Modules usually, but not always, reside in a folder with a
__init__.py
file inside of it. Modules can contain functions but also classes. Modules are imported using theimport
keyword.Learn more about Python modules at these links:
https://docs.python.org/2/tutorial/modules.html (Python 2) https://docs.python.org/3/tutorial/modules.html (Python 3)
Classes, in the other hand, can be defined in your main application code or inside modules imported by your application. Classes are the code of Object Oriented Programming and can contain properties and methods.
Learn more about Python classes at these links:
https://docs.python.org/2/tutorial/classes.html (Python 2) https://docs.python.org/3/tutorial/classes.html (Python 3)