Python convention for separating files [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Im not positive on the terminology here, so that may explain why searching on my own yielded no results.
I was just curious if there is a widely accepted, general method to writing a module in python. Obviosuly people prefer splitting things into segmented .py scripts, importing when needed, and packing it all into a folder.
What I want to know: Is there a general method to how/when/why we stop writing things together in one .py and begin a new one (And i mean other than obvious things like... one script .py for the main job, and then a preferences.py to handle reading/writing prefs)

You should split your code into multiple modules when it begins to be unwieldy to keep it all in one module. This is to some extent a matter of taste. Note that it may unwieldy for the code author (i.e., file is too big to navigate easily) or for the user of the library (e.g., too many unrelated functions/classes jammed together in the same namespace, hard to keep track of them).

Related

Numbering your code filenames when possible : Do's and Don't? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
TL,DR: Is it a sacrilege to number your code source files in their executing orders ?
I'm developing a very small program in Python, which execution is very sequential.
Kinda looks like a script.
I thought about numbered the source files and librairies by their order of execution, when possible. To me it brings the same clarity in the folder organization, exactly as you would number the chapters of a book...
For transversal functions that can be called at any step of the program they are in files named by their functionalities.
Is it an absolute don't do? Why ?
here is an example of how my project folder looks:
Project_0_main.py
Project_1_step_yyy.py
Project_2_step_xxxx.py
Project_3_step_zzzzz.py
Project_math_functions.py
Project_do_that_function.py
Thanks
The only question that matters is about maintainance. If you are sure that you could possibly adapt steps but never add a new step between 2 other ones, then numbering is fine, even if is is not the most common use(*). It you cannot be sure of that, then you have just find the don't and the reason why.
It is not a absolute don't, but naming scripts with sensible names related to the real actions is a form of minimal, implicit and immediate documentation. Using numbers will lose that, and would require an annex file to document what is done in each numbered step.

Is it worth placing functions, variables, settings etc in different files? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have this script I'm working on which is some 2,000 lines. At the top I have a list of settings to be used in the script, then all the functions, then the script itself.
I found myself going back and forth from the actual script to the functions (and the settings) so I thought it might be a good idea to place them in separate files, to be imported in the main script, with the practical benefit of being able to open them in different windows, for ease of access.
Is this approach of any value? or, is there any problem with this, maybe in the long term, when the script grows even more? or is it all down to personal preference?
(if it matters, I'm using Python.)
Let's put it this way:
If you are making a big jig saw puzzle, you start dividing the pieces into sections; clouds with clouds, water with water, grass with grass etc. Subsequently, you don't mix the pieces, but put each of the sections into a different corner on the table.
Likewise, if you have bigger coding projects, you start separating data from code, create functions and classes to organize your code in a way that makes sense, according to their functionality. If the project is big enough, put different sections into different files (modules).
This is not only to limit the scrolling through possibly thousands of lines, but also to keep your mind clean for the section you are working on, and to make the project maintainable.
Last but not least; working this way, most likely at a certain point you will find yourself writing modules, to be reused in other projects.

How should I structure a larger program using python instead of just one big file with code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Sorry if the title is confusing.
What I am trying to say is this:
I have worked with Python before, but I'm by no means an expert. So far everything I have done has just been 'somefile.py' with lots of methods and code in it, but it doesn't really have any organizational structure. In Java (which I am more familiar with than Python), there are usually different classes that each have methods and are called from each other. How do you make a file full of code organized and structured when working on a large project? Break them up into files by class?
Hopefully this is clearer. Let me know if this needs clarification.
In Python, the file unit is called module. Modules are organized in packages.
You usually put your classes each in a module and also use modules to group related code that doesn't belong to any class. Related modules are grouped in packages (physically represented by directories) which effectively create namespaces.
Then you use the import command to import the desired pieces of the code into other modules.
You can read about modules, packages and import in the Python documentation here.
Logically, it isn't much different than Java or other languages.

Why make more than one python file for the same program? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have been making a games using pygame and python and I don't need to make more, separate python files to run my game, but when I look at other peoples programs, they have different files that all work in conjunction with each other for their program to work, why not have all or most of the coding in one file?
I use python 3.2.5 and the corresponding pygame
--- Me in the future --- I apologise for asking this question I didn't know exactly what this website was for and how to ask questions ect. I know know that this website inst for opinions.
People do this because it makes the program more readable.
While writing some code you have to understand the difficulty of the people who will maintain your code.
The main advantages of making it in separate files are:
modularity- By using separate files different modules of the program can be separated into different files. This has many advantages. Whenever a problem occur in some part of the code you can look straight into that file.
Readability- Splitting program into different files helps in making it more readable and beautiful.
easy to maintain- Similar to the advantage told in modularity. Whenever some part of the program needs to be updated it can be done easily.
code reuse- If the program is split carefully into different modules, you can use the same code later when a similar problem has to be solved

Python: modularizing large file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a python source file with a few classes in it. These classes require roughly the same imports, and rely on each other: each references some of the others in its methods.
Semantically speaking, these classes form a single module. They belong in the same file.
The problem is that they have grown. Each class now spans well over a hundred lines, and that's after refactoring to keep their methods simple. The file is slow to navigate, often modified at different points, and hard to keep track of mentally.
If I break all this into smaller files, I'll find that each file has the same imports, and that they must import each other circularly.
How can I modularize this code?
Note: the fact that these classes are large is not outright bad design. They are message dispatchers that have one method for each kind of message they can handle. There is a hierarchy in place that allows for code reuse via inheritance wherever possible.

Categories