|
1 | 1 | Structuring Your Project |
2 | 2 | ======================== |
3 | 3 |
|
4 | | -Structuring your project properly is extremely important. |
| 4 | +By "structure" we mean the decisions you make concerning |
| 5 | +how your project best meets its objective. We need to consider how to |
| 6 | +best leverage Python's features to create clean, effective code. |
| 7 | +In practical terms, "structure" means making clean code whose logic and |
| 8 | +dependencies are clear as well as how the files and folders are organized |
| 9 | +in the filesystem. |
| 10 | + |
| 11 | +Which functions should go into which modules? How does data flow through |
| 12 | +the project? What features and functions can be grouped together and |
| 13 | +isolated? By answering questions like these you can begin to plan, in |
| 14 | +a broad sense, what your finished product will look like. |
| 15 | + |
| 16 | +In this section we take a closer look at Python's module and import |
| 17 | +systems as they are the central element to enforcing structure in your |
| 18 | +project. We then discuss various perspectives on how to build code which |
| 19 | +can be extended and tested reliably. |
5 | 20 |
|
6 | | -.. todo:: Fill in "Structuring Your Project" stub |
7 | 21 |
|
8 | 22 | Structure is Key |
9 | 23 | ---------------- |
@@ -75,9 +89,22 @@ As soon as you use `import` statements you use modules. These can be either buil |
75 | 89 | modules such as `os` and `sys`, third-party modules you have installed in your |
76 | 90 | environment, or your project's internal modules. |
77 | 91 |
|
78 | | -Nothing special is required for a Python file to be a module, but the import |
79 | | -mechanism needs to be understood in order to use this concept properly and avoid |
80 | | -some issues. |
| 92 | +To keep in line with the style guide, keep module names short, lowercase, and |
| 93 | +be sure to avoid using special symbols like the dot (.) or question mark (?). |
| 94 | +So a file name like `my.spam.py` is one you should try to avoid! Naming this way |
| 95 | +will interfere with the way python looks for modules. |
| 96 | + |
| 97 | +In this example python expects to find a "spam.py" file in a folder named "my" |
| 98 | +which is not the case. There is an |
| 99 | +`example <http://docs.python.org/tutorial/modules.html#packages>`_ |
| 100 | + of how the dot should be used available in the python docs. |
| 101 | + |
| 102 | +If you'd like you could name it as `my_spam.py` but even our friend the |
| 103 | +underscore should not be seen often in module names. |
| 104 | + |
| 105 | +Aside for some naming restrictions, nothing special is required for a Python file |
| 106 | +to be a module, but the import mechanism needs to be understood in order to use |
| 107 | +this concept properly and avoid some issues. |
81 | 108 |
|
82 | 109 | Concretely, the `import modu` statement will look for the proper file, which is |
83 | 110 | `modu.py` in the same directory as the caller if it exists. If it is not |
@@ -276,7 +303,7 @@ clearer and thus preferred. |
276 | 303 | # Do something |
277 | 304 | # bar() is decorated |
278 | 305 |
|
279 | | -Using this mechanism is useful for separating concerns and avoiding |
| 306 | +This mechanism is useful for separating concerns and avoiding |
280 | 307 | external un-related logic 'polluting' the core logic of the function |
281 | 308 | or method. A good example of a piece of functionality that is better handled |
282 | 309 | with decoration is memorization or caching: you want to store the results of an |
|
0 commit comments