0

first of all thanks and i know stack overflow is full of this argument (but we can consider a review question). I have some question about to organize a Python project.

from this link i had find this nice schema

/myproject
   /__init__.py # This makes it a Python package and importable.
   /evaluate.py # Contains the code to actually do calculations.
   /main.py # Starts the application
   /ui.py # Contains the code to make a pretty interface

from this web site __init__.py is normally empty

my question are:

  1. suppose you have a function (def foo). The foo is located in evaluate.py or in main.py?
  2. if foo is located in main.py (or evaluate.py) what evaluate.py ( or main.py) contains?
  3. sometimes i find utilities.py and/or module.py. What utilities.py/module.py contains?

Thanks in advance

Community
  • 1
  • 1
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131
  • I like to believe form follows from function. What your project does will dictate how to organize it. Your example has two components, an ui and a calculation, so there are 2 py files for these and the rest is glue to put it together. As for where your foo goes, is it a calculation foo or an ui foo. Finally, helper functions that are used everywhere are often in a common.py or utilities.py. – kalhartt Jan 08 '13 at 15:29
  • 4
    I don't think that example it to be taken literally. It serves to illustrate that one doesn't create modules to hold individual classes or something, but modules to contain logical groups. –  Jan 08 '13 at 15:30
  • 1
    Sorry, yes calculation -> evaluate – kalhartt Jan 08 '13 at 15:31
  • To expand on delnans comment, if it calculates stuff, evaluate.py. If it's related to showing things to the user, ui.py. If it's related to starting everything up or cleanup, main.py. – dutt Jan 08 '13 at 15:32
  • i have not clear main.py. Someone know a good link to look inside main.py? thanks – Gianni Spear Jan 08 '13 at 15:38
  • 3
    Typically in `main` you parse command line arguments, read the environment, parse configuration files, open input files, connect to servers etc., then hand off the work to the actual processing modules. – Fred Foo Jan 08 '13 at 16:14

1 Answers1

1

From my coding style: main.py: always do simple things about initialize variables from environ outside and load config file. And wrap actual logic things. For example:

from evaluate import actual_things
def run():
    ...
    actual_things()
    ...

if __name__ == '__main__':
    init()
    load_from_config()
    run()

Q: sometimes i find utilities.py and/or module.py. What utilities.py/module.py contains? A:utilities.py: or utils.py often is helper. Such as encode and decode, encrypt etc.

Q:if foo is located in main.py (or evaluate.py) what evaluate.py ( or main.py) contains? A:You mush point out what foo do and according to the function of foo, we locate foo to somewhere. If foo is a business relatively function, foo should be located in evaluate.py.

Q:suppose you have a function (def foo). The foo is located in evaluate.py or in main.py? A:See above.

At last, I think the best approach to know is to learn a project overall. You will know what QA can't help.

wang wynn
  • 174
  • 8