2

I'm pretty new to Python as to OOP in general which is probably be the reason that I can't figure out the following:
I'm writing a python script which opens a text file and subsequently translates it into HTML, maintaining it's "own" mirrored directory-trees for the edit files and the html files. Since directory creation and deletion is done automatically depending on, among other criteria, whether the file existed before or not, I need some kind of automatic and dynamic path adjustment. The script has to do several checks for files and associated directories before it can set the corresponding paths and start the processing.

I decided to put most of the more general functions (check file existence, show dialogs for duplicate filenames if found, etc) in a separate module, since they are quite specific and not depending on any state. Actually they create the state (path variables), so a class would not make sense if this is not a misconception.

On the other hand I'm using a class for the pure getting and setting the paths since I need the paths accessible from every module, so it's basically a global access point for paths. This class is instantiated in the main module.

Now my problem is that I can't figure out how to manipulate the paths (using the path setters) of that instance in the main module from a function inside the tools module. Importing the class instance or the main module into the tools module doesn't seem to work.

Generally speaking, is it possible to use a class instance across all module files and is this the way to go, or am I missing the point somehow?

I paste the relevant bits of code for illustration:

Setter/Getter class inside the main module

class SetEnv():
def __init__(self):
    pass

def set_path_srcfile(self, path_srcfile):
    self.path_srcfile = path_srcfile 

def set_path_htmlfile(self):
    self.path_htmlfile = self.path_srcfile.replace('source', 'html', 1).replace('.txt', '.html', 1)

def get_path_srcfile(self):
    return self.path_srcfile

def get_path_htmlfile(self):
    return self.path_htmlfile

Later in main_module:

env = SetEnv()

Part of tools module (inside a def acting upon user input):

import main_module as mm
path_srcfile = dict[int(user_option)][1] # dictionary holding the path we want to set
mm.env.set_path_srcfile(path_srcfile)
mm.env.set_path_htmlfile()
loxosceles
  • 347
  • 5
  • 21

1 Answers1

3

I might be misinterpreting your question, correct me if I am. As I understand it, you are using one single instance of a SetEnv object across an entire project to store and modify some path configuration.

If you really want a singleton like settings object, then use a module instead of a class.

# env.py
_src = ''
_html = ''

def set_path_srcfile(path_srcfile):
    global _src
    _src = path_srcfile

def get_path_srcfile():
    return _src

...

Then everywhere you need it you can use import env; env.set_path_srcfile(myfile) and know that all other functions / modules / classes will be aware of the update.

If you don't want a singleton, then making a settings object available in the main module somewhere (as you have done) is a fine solution.

kalhartt
  • 3,999
  • 20
  • 25
  • Thanks, that cleared up my doubts, good suggestion about the singleton character of a module. Actually importing the class instance in the main module (like any other) DOES work, contrary to what I said before. – loxosceles Jan 03 '14 at 23:00