Skip to content

Commit 9e7428b

Browse files
cleaned up the modules and packaging docs
1 parent 2ca4a2d commit 9e7428b

5 files changed

Lines changed: 189 additions & 98 deletions

File tree

‎source/index.rst‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Setting up your Environment
4040
4141
4242
Exercise:
43-
..........
43+
.........
4444

4545
.. toctree::
4646
:maxdepth: 1
@@ -57,8 +57,7 @@ Basic Python
5757
modules/BasicPython
5858
modules/Functions
5959

60-
Exercises:
61-
..........
60+
.. rubric:: Exercises:
6261

6362
.. toctree::
6463
:maxdepth: 1
@@ -231,6 +230,13 @@ Advanced Argument Passing
231230
modules/AdvancedArgumentPassing
232231
modules/MoreOnMutability
233232

233+
Exercises:
234+
235+
.. toctree::
236+
:maxdepth: 1
237+
238+
exercises/args_kwargs_lab
239+
234240

235241
Intro to Object Oriented Programing
236242
-----------------------------------
@@ -287,8 +293,8 @@ Exercises:
287293
.. toctree::
288294
:maxdepth: 1
289295

290-
exercises/circle/circle_class.rst
291-
296+
exercises/circle/circle_class
297+
exercises/sparse_array
292298

293299
Subclassing
294300
-----------
@@ -329,6 +335,15 @@ Introduction to Functional Programming
329335
modules/MapFilterReduce
330336
modules/IPythonParallel
331337

338+
Exercises
339+
.........
340+
341+
.. toctree::
342+
:maxdepth: 1
343+
344+
exercises/trapezoid
345+
346+
332347
Advanced Testing
333348
----------------
334349

@@ -352,14 +367,14 @@ The following are some extra topics that might be of interest
352367
modules/Unicode
353368

354369
modules/Lambda
370+
exercises/lambda_magic
355371
modules/IteratorsAndGenerators
356372

357373
modules/Closures
358374

359375
modules/Decorators
360376
modules/ContextManagers
361-
modules/Recursion
362-
377+
exercises/context-managers-exercise.rst
363378

364379
modules/MetaProgramming
365380

@@ -372,9 +387,10 @@ The following are some extra topics that might be of interest
372387

373388

374389
modules/Concurrency
375-
modules/ThreadingMultiprocessing
376-
modules/Coroutines
377390
modules/Async
391+
modules/Coroutines
392+
modules/ThreadingMultiprocessing
393+
exercises/threaded_downloader
378394

379395
modules/Profiling
380396

‎source/modules/Documentation.rst‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ The first place we will see is in the definition of functions.
7272
As you know, to define a function you use the ``def`` keyword.
7373

7474
If a "string literal" is the first thing in the function block following the
75-
header, it is a "docstring":
75+
``def`` line, it is a "docstring":
7676

7777
.. code-block:: python
7878
7979
def complex_function(arg1, arg2, kwarg1='banana'):
8080
"""Return a value resulting from a complex calculation."""
8181
# code block here
8282
83-
You can then read this in an interpreter as the ``__doc__`` attribute of the
84-
function object. Docstrings can also be read and processed by documentation systems and IDEs like iPython.
83+
You can then read this in the interpreter as the ``__doc__`` attribute of the
84+
function object. Docstrings can also be read and processed by documentation systems and IDEs like iPython and Sphinx.
8585

8686
A Function Docstring Should:
8787
............................
@@ -101,9 +101,8 @@ A Function Docstring Should:
101101
* Be enclosed with triple-quotes.
102102

103103
* This allows for easy expansion if required at a later date.
104-
* Always close on the same line if the docstring is only one line.
105104

106-
For any functions that are less than trivial, and particulalry if they take multiple parameters, the parameters should be described in the docstring:
105+
For any functions that are less than trivial, and particularly if they take multiple parameters, the parameters should be described in the docstring:
107106

108107
.. code-block:: python
109108
@@ -126,6 +125,6 @@ The ``:param arg1:`` notation is "restructured text" -- very handy if you want y
126125
The docstring PEP
127126
.................
128127

129-
For the full "official" recomendations about docstrings, see `PEP 257: Docstring Conventions <http://legacy.python.org/dev/peps/pep-0257/>`_.
128+
For the full "official" recommendations about docstrings, see `PEP 257: Docstring Conventions <http://legacy.python.org/dev/peps/pep-0257/>`_.
130129

131130

‎source/modules/Modules.rst‎

Lines changed: 79 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Code Structure, Modules, and Namespaces
55
#######################################
66

77

8-
**How to get what you want when you want it.**
8+
.. centered:: **How to get what you want when you want it**
99

1010

1111
Code Structure
@@ -57,14 +57,12 @@ An indent *could* be:
5757

5858
If you want anyone to take you seriously as a Python developer:
5959

60-
**Always use four spaces -- really!**
61-
62-
`(PEP 8) <http://legacy.python.org/dev/peps/pep-0008/>`_
60+
.. centered:: **Always use four spaces -- really!** (`PEP 8 <https://www.python.org/dev/peps/pep-0008/>`_)
6361

6462
.. note::
6563
If you *do* use tabs (and really, don't do that!) python interprets them as the equivalent of *eight* spaces. Text editors can display tabs as any number of spaces, and most modern editors default to four -- so this can be *very* confusing! so again:
6664

67-
**Never mix tabs and spaces in Python code**
65+
.. centered:: **Never mix tabs and spaces in Python code**
6866

6967

7068
Spaces Elsewhere
@@ -86,7 +84,7 @@ But you should strive for proper style. Isn't this easier to read?
8684
x = (3 * 4) + (12 / func(x, y, z))
8785
8886
89-
**Read PEP 8 and install a linter in your editor.**
87+
.. centered:: **Read** `**PEP 8** <https://www.python.org/dev/peps/pep-0008/>`_ **and install a linter in your editor.**
9088

9189

9290
Modules and Packages
@@ -98,10 +96,10 @@ Python is all about *namespaces* -- the "dots"
9896

9997
The "dot" indicates that you are looking for a name in the *namespace* of the given object. It could be:
10098

101-
* a name in a module
102-
* a module in a package
103-
* an attribute of an object
104-
* a method of an object
99+
* A name in a module
100+
* A module in a package
101+
* An attribute of an object
102+
* A method of an object
105103

106104
The only way to know is to know what type of object the name refers to. But in all cases, it is looking up a name in the namespace of the object.
107105

@@ -120,15 +118,69 @@ So you can think of the files you write that end in ``.py`` as modules.
120118

121119
When a module is imported, the code in that file is run, and any names defined in that file are now available in the module namespace.
122120

121+
For a really simple example, if you have the following in the ``trivial.py`` file:
122+
123+
.. code-block:: python
124+
:linenos:
125+
126+
x = 1
127+
y = 2
128+
129+
def do_nothing(a, b, c):
130+
print("do_nothing was called with:", a, b, c)
131+
132+
print("at the end of the trivial module")
133+
134+
What do you think happens when you import that module? What will get printed?
135+
136+
What names will be defined in that module?
137+
138+
How would you access those names?
139+
140+
Before running this code, think about it a bit. Recall what happens when you import a module:
141+
142+
* The code is run in the module, top to bottom.
143+
* The names defined in the module (its global namespace) are made available in the modules namespace.
144+
145+
So: Lines 1-2 assign two names, ``x`` and ``y``. lines 4-5 define a function, named ``do_nothing``. Line 7 prints something.
146+
147+
So: when run, there are three names defined, and one print function run.
148+
149+
Now try it:
150+
151+
.. code-block:: python
152+
153+
>>> import trivial
154+
at the end of the trivial module
155+
156+
yes, we got that print function run.
157+
158+
Let's see if the names are there:
159+
160+
.. code-block:: python
161+
162+
>>> trivial.x
163+
1
164+
>>> trivial.y
165+
2
166+
167+
.. code-block:: python
168+
169+
170+
>>> trivial.do_nothing(3,4,5)
171+
do_nothing was called with: 3 4 5
172+
173+
yes, there are, in the ``trivial`` namespace.
174+
123175

124176
Packages
125177
--------
126178

127179
A package is a module with other modules in it.
128180

129-
On a filesystem, this is represented as a directory that contains one or more ``.py`` files, one of which **must** be called ``__init__.py``.
181+
On a filesystem, this is represented as a directory that contains one or more ``.py`` files, one of which **must** be called ``__init__.py``. The ``__init__.py`` file can be empty (and often is) -- but it must be there.
130182

131-
When you have a package, you can import only the package, or any of the modules inside it. When a package is imported, the code in the ``__init__.py`` file is run, and any names defined in that file are available in the *package namespace*.
183+
When there is a package available, you can import only the package, or any of the modules inside it. When a package is imported, the code in the ``__init__.py`` file is run, and any names defined in that file are available in the *package namespace*.
132184

133185
Here we define about the simplest package possible:
134186

@@ -181,7 +233,7 @@ What about the module?
181233
182234
AttributeError: module 'my_package' has no attribute 'a_module'
183235
184-
the a_module name does not exist. It must be imported explicitly:
236+
the ``a_module`` name does not exist. It must be imported explicitly:
185237

186238
.. code-block:: ipython
187239
@@ -206,6 +258,7 @@ Note that you can also put a package inside a package. So you can create arbitra
206258

207259
So don't overdo it -- only go as deep as you really need to to keep your code organized.
208260

261+
209262
Importing modules
210263
-----------------
211264

@@ -236,18 +289,22 @@ If you want only a few names in a module, and don't want to type the module name
236289
237290
This brings only the names specified (``this``, ``that``) into the global namespace. All the code in the module is run, but the module's name is not available. But the explicitly imported names are directly available.
238291

292+
Sometimes you want the entire module, but maybe not want to type its entire name eadh time you use. So you can rename a module when you import it. (you may also want to do this if a module has the same name as a variable you want to use...)
293+
239294
.. code-block:: python
240295
241296
import modulename as a_new_name
242297
243-
This imports the module, and gives it a new name in the global namespace. This is done to avoid a name conflict, or to give the module a shorter name. For example, the numpy module is usually imported as:
298+
This imports the module, and gives it a new name in the global namespace. For example, the numpy package is usually imported as:
244299

245300
.. code-block:: python
246301
247302
import numpy as np
248303
249304
Because numpy has a LOT of names, some of which may conflict with builtins or other modules, and users want to be able to reference them without too much typing.
250305

306+
You can also import a name within a module and rename it at the same time:
307+
251308
.. code-block:: python
252309
253310
from modulename import this as that
@@ -282,6 +339,7 @@ You can play with some of this with the standard library:
282339
In [8]: cosine(1.2)
283340
Out[8]: 0.3623577544766736
284341
342+
285343
My rules of thumb
286344
-----------------
287345

@@ -308,15 +366,15 @@ Or import it with a nice short name:
308366
import \* ?
309367
-----------
310368

311-
**Warning:**
369+
.. centered:: **Warning:**
312370

313371
You can also import all the names in a module with:
314372

315373
.. code-block:: python
316374
317375
from modulename import *
318376
319-
But this leads to name conflicts, and a cluttered namespace. It is NOT recommended practice.
377+
But this leads to name conflicts, and a cluttered namespace. It is NOT recommended practice anymore.
320378

321379

322380
Importing from packages
@@ -336,22 +394,23 @@ http://effbot.org/zone/import-confusion.htm
336394

337395
And :ref:`packaging` goes into more detail about creating (and distributing!) your own package.
338396

397+
339398
What does ``import`` actually do?
340399
---------------------------------
341400

342-
When you import a module, or a symbol from a module, the Python code is *compiled* to **bytecode**.
401+
When you import a module, or a symbol from a module, the Python code is *compiled* to *bytecode*.
343402

344403
The result is a ``module.pyc`` file.
345404

346-
Then after compiling, all the code in the module is run **at the module scope**.
405+
Then after compiling, all the code in the module is run **at the module scope** -- that is, in the namespace of the module.
347406

348-
For this reason, it is good to avoid module-scope statements that have global side-effects.
407+
For this reason, it is good to avoid module-scope statements that have global side-effects. This includes things as simple as a ``print()`` -- it will only print the first time the module is imported.
349408

350409

351410
Re-import
352411
----------
353412

354-
The code in a module is NOT re-run when imported again. This makes it efficient to import the same module multiple places in a program. But it means that if you change the code in a module after importing it, that change will not be reflected when it is imported again.
413+
The code in a module is *not* re-run when imported again. This makes it efficient to import the same module multiple places in a program. But it means that if you change the code in a module after importing it, that change will not be reflected when it is imported again.
355414

356415
If you DO want a change to be reflected, you can explicitly reload a module:
357416

@@ -427,14 +486,3 @@ Running ``mod2.py`` results in::
427486
You can see that when ``mod2`` changed the value of ``mod1.x``, that changed the value everywhere that ``mod1`` is imported. You want to be very careful about this.
428487

429488
If you are writing ``mod2.py``, and did not write ``mod3`` (or wrote it long enough ago that you don't remember its details), you might be very surprised that a value in ``mod1`` changes simply because you imported ``mod3``. This is known as a "side effect", and you generally want to avoid them!
430-
431-
432-
433-
434-
435-
436-
437-
438-
439-
440-

0 commit comments

Comments
 (0)