tags:

views:

50

answers:

1

In order to keep my code clean and organized, I split my classes up into a bunch of different files and folders, here is what a typical project structure will look like for me:

> Project
    __init__.py
    main.py
    ui.py
    > lib
        foo.py
        bar.py

In my ui.py file, I usually define some sort of info function if the application is just a command line application. That usually looks something like this:

def info(message, level=1):
    if level == 1:
        token = "[+] "
    elif level == 2:
        token = "\t[-] "
    print token + str(message)

Now the question is, if I am doing a lot of the work in main.py, and have therefore created a ui object in it by importing it in, what is the best way then to use the same info function in foo.py or bar.py?

+1  A: 

import project.ui or from project import ui should do the trick. Don't tell anyone I told you about the second option. The parent directory of project needs to be on your python path.

aaronasterling
Would this even work if I was updating a elements on a gui window with the ui class?
bball
as long as the parent of `project` is on the python path and there is an `__init__.py` file in `project`, then yes.
aaronasterling
I see. I figured there was something other than an import to this but I guess I was wrong. Thanks.
bball
well, you could always explicitly pass the info function into any class constructors or other function that use it in `foo` and `bar` from main. This has the disadvantage of being complicated and the advantage of allowing you to send in null info functions or wrapped info functions to specific parts of your program.
aaronasterling