1

I've started a Python project. It's not a web app or GUI app, just a simple command line tool. I generally use the same kind of project structure, which is:

tspviz
│   .gitignore
│   requirements.txt
│   setup.py
│
├───data
│       prices.txt
│       shops.txt
│
├───tests
└───tspviz
    │   tspviz.py
    │   __init__.py
    │
    ├───algorithm
    │   │   genetic.py
    │   │   params.py
    │   │   __init__.py
    │   │
    │   └───__pycache__
    │           genetic.cpython-37.pyc
    │           params.cpython-37.pyc
    │           __init__.cpython-37.pyc

So it is quite self-explanatory, the main file is the tspviz.py in tspviz directory. Now this is a program which will solve the Travelling Salesman Problem, so I would need to make some classes like City, Path etc. Then the problem is: where do I put them? I would make a separate directory in the tspviz dir (not the root one). Then, well, how do I name this folder without confusing my colleagues? I don't know, "classes", "types", "misc"? So my question would be: Should I place them all in a directory inside tspviz as they would probably be used only from there and secondly: is there a standardized way to name a folder like this? (with classes, that you use all over the place)

1 Answer 1

0

given the classes names (City, Path etc) it looks like your classes belong to the problem domain, so a models.py module might make sense. Note that you don't necessarily need a folder here, Python is not Java and doesn't forces you into this "one file per class" non-sense - only use a folder (package) if you have other reasons to split your module in submodules.

As to "where to put it" (wheter a module or package), well, at the same level as your other modules and packages obviously (in the tspviz/tspviz subfolder, along tspviz.py and the algorithm package).

is there a standardized way to name a folder like this?

No.

(with classes, that you use all over the place)

Python's classes are objects, just like functions and everything else, so the fact there are (or not) classes defined in your module / package is totally irrelevant, it would be the same with functions etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, that's great, thank you. And do you think I should actually get rid of the algorithm folder, where genetic.py defines a class to solve TSP with genetic algorithm and the params.py is just a file with one dataclass?
@Frynio not necessarily. There's no "one size fits all" solution and no golden rule here, except the usual ones: good naming, high cohesion and a low coupling. oh and yes, the technical fact that imported modules are only recompiled (at import time) if the .pyc is outdated, while the main script is always recompiled, so the less code in your main script the better (wrt/ startup time I mean).
Oh wow, I actually didn't know that. So I could potentially in main just do if __name__==..., the run some function from other imported file, and that's how I actually gain some time, right?
yes exactly - your program's main script should only import the minimal required modules, parse arguments, setup things like logging, configuration etc and run. Everything else belongs to modules/packages. Well, unless it's a small utility script you want to keep as standalone, of course.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.