
I learned very early the difference between knowing the name of something and knowing something - Richard Feynman.
Python code snippets
Background
​
The idea of this project goes back to a year ago, when I decided to refresh my Python knowledge and I bought the latest editions of the massive tomes written by Mark Lutz, Learning Python and Programming Python. It took me about 8 months for finishing reading Learning Python (over 1600 pages!) and I can really recommend it to anyone wishing to learn the fundamentals of Python programming. But be warned that it is neither a quick and dirty introduction nor an easy read, it really requires perseverance and commitment, if you want to learn from it.
​
Back to the project. Whenever I read a book about a new programming language or programming topic, I always try out some of the features in little programs, or I just keep adding the new things that I learn in a huge single source file, adding some comments and print statements for helping me pick up those features again should I need them sometime in the future.
​
So this is exactly what I did this time as well, but I also felt the need to aim at something better, something more user friendly, that could be of some use to someone looking for how to apply a particular language feature or just for refreshing his or her knowledge. I started to create separate modules for each language feature that I wanted to experiment with, which I call in the project code snippets, and I added a code snippets runner for letting the user choose which code snippet to run. When I learned about Sphinx I decided to add it to the mix, in order to have the whole thing look more professional. This is how the idea of this project was finally born.
​
Repository
​
The repository is freely available on Git, and it includes:
-
the collection of code snippets,
-
the documentation,
-
the Sphinx project for rebuilding the documentation.
Once cloned, it should look like the following on your side:
​

Have a look at the documentation first by opening the file index.html inside the doc/html folder in your preferred browser:

The Installation and Running the codesnippets pages will give you enough information to keep you going. You can click on any of the features listed for reading a short explanation and have some code shown to you about how to use it. For example, if you click on List comprehension, the following page should appear in your browser:

You can run this particular code snippet by choosing feature number 8 when prompted by the Python code snippets runner from the console:

and inspect its source code by opening the module feature8.py inside the package codesnippets with your favourite Python editor:

All the code snippets actually document themselves by printing the explanation and code on the console formatted in reStructuredText markup. This output is copied and pasted as the module docstring, which in turn is parsed by Sphinx and used for creating the corresponding webpage documenting the feature. Details about how this works are given in Add your own code snippet in the documentation, where step by step instructions are provided if you wish to add your own code snippet.
​
We hope that you might find this information useful for your own endeavours. Do not hesitate to contact us should you have any questions!
​
​