diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 000000000..9b170d2fb --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,3 @@ +Be cordial or be on your way. + +https://www.kennethreitz.org/essays/be-cordial-or-be-on-your-way diff --git a/Readme.rst b/Readme.rst index bccb651b0..8dce3c079 100644 --- a/Readme.rst +++ b/Readme.rst @@ -3,6 +3,8 @@ Hitchhiker's Guide to Python **Python Best Practices Guidebook** +.. image:: https://farm1.staticflickr.com/628/33173824932_58add34581_k_d.jpg + ----------- **Work in progress. If you'd like to help, please do. There's a lot of work to @@ -16,7 +18,7 @@ basis. Topics include: -- Platform- and version-specific installations +- Platform and version-specific installations - Py2app, Py2exe, bbfreeze, pyInstaller - Pip - Numpy, scipy, statpy, pyplot, matplotlib diff --git a/docs/_static/ad.png b/docs/_static/ad.png new file mode 100644 index 000000000..7fb229c59 Binary files /dev/null and b/docs/_static/ad.png differ diff --git a/docs/_templates/hacks.html b/docs/_templates/hacks.html index f7f7093d7..6e8541514 100644 --- a/docs/_templates/hacks.html +++ b/docs/_templates/hacks.html @@ -1,3 +1,34 @@ + + + + + + + +

Join Mailing List.

+

Say Thanks!

+ + + + + +

O'Reilly Book

@@ -32,12 +39,15 @@

Other Projects

More Kenneth Reitz projects:

Contributors

@@ -62,6 +72,7 @@

Translations

  • English
  • French
  • Chinese
  • -
  • Japanese
  • +
  • Japanese
  • Korean
  • +
  • Brazilian Portuguese
  • diff --git a/docs/_templates/sidebarlogo.html b/docs/_templates/sidebarlogo.html index bea79f74c..2fbb9b99a 100644 --- a/docs/_templates/sidebarlogo.html +++ b/docs/_templates/sidebarlogo.html @@ -16,9 +16,15 @@

    Get Updates

    allowtransparency="true" frameborder="0" scrolling="0" width="200" height="20">

    Follow @kennethreitz

    - +

    Say Thanks!

    Join Mailing List.

    + + + + +
    +

    O'Reilly Book

    This guide is now available in tangible book form!

    @@ -27,16 +33,21 @@

    O'Reilly Book

    All proceeds are being directly donated to the DjangoGirls organization.

    + +

    Other Projects

    More Kenneth Reitz projects:

    Translations

    @@ -44,6 +55,7 @@

    Translations

  • English
  • French
  • Chinese
  • -
  • Japanese
  • +
  • Japanese
  • Korean
  • +
  • Brazilian Portuguese
  • diff --git a/docs/conf.py b/docs/conf.py index b9e1c63a7..f0bd46e19 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -109,7 +109,8 @@ 'github_user': 'kennethreitz', 'github_repo': 'python-guide', 'github_banner': True, - 'show_related': False + 'show_related': False, + 'note_bg': '#FFF59C' } # Add any paths that contain custom themes here, relative to this directory. diff --git a/docs/contents.rst.inc b/docs/contents.rst.inc index 22cbdc940..76049aab6 100644 --- a/docs/contents.rst.inc +++ b/docs/contents.rst.inc @@ -1,24 +1,50 @@ Getting Started with Python --------------------------- -New to Python? Let's properly setup up your Python environment. +New to Python? Let's properly setup up your Python environment: .. toctree:: :maxdepth: 2 starting/which-python -- Properly Install Python +- Properly Install Python on your system: .. toctree:: :maxdepth: 1 starting/installation + starting/install3/osx + starting/install3/win + starting/install3/linux starting/install/osx starting/install/win starting/install/linux +- Using Virtualenvs with Pipenv: + + .. toctree:: + :maxdepth: 2 + + dev/virtualenvs + + +Python Development Environments +------------------------------- + +This part of the guide focus on the Python development environment, +and the best-practice tools that are available for writing Python code. + +.. toctree:: + :maxdepth: 2 + + dev/env + dev/virtualenvs + dev/pip-virtualenv + + + Writing Great Python Code ------------------------- @@ -80,22 +106,6 @@ This part of the guide focuses on deploying your Python code. shipping/freezing -Python Development Environments -------------------------------- - -This part of the guide focus on the Python development environment, -and the best-practice tools that are available for writing Python code. - -.. toctree:: - :maxdepth: 2 - - dev/env - dev/virtualenvs - dev/pip-virtualenv - - - - Additional Notes ---------------- diff --git a/docs/dev/env.rst b/docs/dev/env.rst index d5870274a..e1e012970 100644 --- a/docs/dev/env.rst +++ b/docs/dev/env.rst @@ -1,6 +1,8 @@ Your Development Environment ============================ +.. image:: https://farm3.staticflickr.com/2930/33175624924_7febc46cc4_k_d.jpg + Text Editors :::::::::::: @@ -35,11 +37,11 @@ source files. There is also a handy syntax plugin called syntax_ featuring some improvements over the syntax file included in Vim 6.1. -These plugins supply you with a basic environment for developing in Python. -To get the most out of Vim, you should continually check your code for syntax -errors and PEP8 compliance. Luckily PEP8_ and Pyflakes_ will do this for you. -If your Vim is compiled with :option:`+python` you can also utilize some very -handy plugins to do these checks from within the editor. +These plugins supply you with a basic environment for developing in Python. To +get the most out of Vim, you should continually check your code for syntax +errors and PEP8 compliance. Luckily pycodestyle_ and Pyflakes_ will do this +for you. If your Vim is compiled with ``+python`` you can also utilize some +very handy plugins to do these checks from within the editor. For PEP8 checking and pyflakes, you can install vim-flake8_. Now you can map the function ``Flake8`` to any hotkey or action you want in Vim. The plugin will @@ -68,12 +70,12 @@ Python-mode Python-mode_ is a complex solution for working with Python code in Vim. It has: -- Asynchronous Python code checking (``pylint``, ``pyflakes``, ``pep8``, ``mccabe``) in any combination +- Asynchronous Python code checking (``pylint``, ``pyflakes``, ``pycodestyle``, ``mccabe``) in any combination - Code refactoring and autocompletion with Rope - Fast Python folding - Virtualenv support - Search through Python documentation and run Python code -- Auto PEP8_ error fixes +- Auto pycodestyle_ error fixes And more. @@ -86,7 +88,7 @@ using ```` key or any other customized keys. .. _indent: http://www.vim.org/scripts/script.php?script_id=974 .. _syntax: http://www.vim.org/scripts/script.php?script_id=790 .. _Pyflakes: http://pypi.python.org/pypi/pyflakes/ -.. _PEP8: http://pypi.python.org/pypi/pep8/ +.. _pycodestyle: https://pypi.python.org/pypi/pycodestyle/ .. _syntastic: https://github.com/scrooloose/syntastic .. _Python-mode: https://github.com/klen/python-mode .. _SuperTab: http://www.vim.org/scripts/script.php?script_id=1643 @@ -148,10 +150,10 @@ known for IntelliJ IDEA. Both share the same code base and most of PyCharm's features can be brought to IntelliJ with the free `Python Plug-In `_. There are two versions of PyCharm: Professional Edition (Free 30-day trial) and Community -Edition(Apache 2.0 License) with fewer features. +Edition (Apache 2.0 License) with fewer features. Python (on Visual Studio Code) ------------------------------ +------------------------------ `Python for Visual Studio `_ is an extension for the `Visual Studio Code IDE `_. This is a free, light weight, open source IDE, with support for Mac, Windows, and Linux. diff --git a/docs/dev/pip-virtualenv.rst b/docs/dev/pip-virtualenv.rst index 51927e689..f2457f1dc 100644 --- a/docs/dev/pip-virtualenv.rst +++ b/docs/dev/pip-virtualenv.rst @@ -3,6 +3,8 @@ Further Configuration of Pip and Virtualenv =========================================== +.. image:: https://farm4.staticflickr.com/3934/34018732105_f0e6758859_k_d.jpg + Requiring an active virtual environment for ``pip`` --------------------------------------------------- @@ -18,8 +20,8 @@ environment of the project. Over time this can result in a messy global package list. In order to make sure that you install packages to your active virtual -environment when you use ``pip install``, consider adding the following two -lines to your :file:`~/.bashrc` file: +environment when you use ``pip install``, consider adding the following +line to your :file:`~/.bashrc` file: .. code-block:: console diff --git a/docs/dev/virtualenvs.rst b/docs/dev/virtualenvs.rst index e6be87f89..c3b2102ca 100644 --- a/docs/dev/virtualenvs.rst +++ b/docs/dev/virtualenvs.rst @@ -1,28 +1,218 @@ .. _virtualenvironments-ref: -Virtual Environments -==================== +Pipenv & Virtual Environments +============================= -A Virtual Environment is a tool to keep the dependencies required by different -projects in separate places, by creating virtual Python environments for them. -It solves the "Project X depends on version 1.x but, Project Y needs 4.x" -dilemma, and keeps your global site-packages directory clean and manageable. +.. image:: https://farm5.staticflickr.com/4290/35294660055_42c02b2316_k_d.jpg -For example, you can work on a project which requires Django 1.10 while also -maintaining a project which requires Django 1.8. +This tutorial walks you through installing and using Python packages. -virtualenv +It will show you how to install and use the necessary tools and make strong +recommendations on best practices. Keep in mind that Python is used for a great +many different purposes, and precisely how you want to manage your dependencies +may change based on how you decide to publish your software. The guidance +presented here is most directly applicable to the development and deployment of +network services (including web applications), but is also very well suited to +managing development and testing environments for any kind of project. + +.. Note:: This guide is written for Python 3, however, these instructions + should work fine on Python 2.7—if you are still using it, for some reason. + + +Make sure you've got Python & pip +--------------------------------- + +Before you go any further, make sure you have Python and that it's available +from your command line. You can check this by simply running: + +.. code-block:: bash + + $ python --version + +You should get some output like ``3.6.2``. If you do not have Python, please +install the latest 3.x version from `python.org`_ or refer to the +`Installing Python`_ section of this guide. + +.. Note:: If you're newcomer and you get an error like this: + + .. code-block:: python + + >>> python + Traceback (most recent call last): + File "", line 1, in + NameError: name 'python' is not defined + + It's because this command is intended to be run in a *shell* (also called + a *terminal* or *console*). See the Python for Beginners + `getting started tutorial`_ for an introduction to using your operating + system's shell and interacting with Python. + +Additionally, you'll need to make sure you have :ref:`pip` available. You can +check this by running: + +.. code-block:: bash + + $ pip --version + +If you installed Python from source, with an installer from `python.org`_, or +via `Homebrew`_ you should already have pip. If you're on Linux and installed +using your OS package manager, you may have to `install pip `_ separately. + +.. _getting started tutorial: https://opentechschool.github.io/python-beginners/en/getting_started.html#what-is-python-exactly +.. _python.org: https://python.org +.. _Homebrew: https://brew.sh +.. _Installing Python: http://docs.python-guide.org/en/latest/starting/installation/ + + +Installing Pipenv +----------------- + +:ref:`Pipenv` is a dependency manager for Python projects. If you're familiar +with Node.js' `npm`_ or Ruby's `bundler`_, it is similar in spirit to those +tools. While :ref:`pip` can install Python packages, Pipenv is recommended as +it's a higher-level tool that simplifies dependency management for common use +cases. + +Use ``pip`` to install Pipenv: + +.. code-block:: python + + $ pip install --user pipenv + + +.. Note:: This does a `user installation`_ to prevent breaking any system-wide + packages. If ``pipenv`` isn't available in your shell after installation, + you'll need to add the `user base`_'s binary directory to your ``PATH``. + + On Linux and macOS you can find the user base binary directory by running + ``python -m site --user-base`` and adding ``bin`` to the end. For example, + this will typically print ``~/.local`` (with ``~`` expanded to the + absolute path to your home directory) so you'll need to add + ``~/.local/bin`` to your ``PATH``. You can set your ``PATH`` permanently by + `modifying ~/.profile`_. + + On Windows you can find the user base binary directory by running + ``py -m site --user-site`` and replacing ``site-packages`` with + ``Scripts``. For example, this could return + ``C:\Users\Username\AppData\Roaming\Python36\site-packages`` so you would + need to set your ``PATH`` to include + ``C:\Users\Username\AppData\Roaming\Python36\Scripts``. You can set your + user ``PATH`` permanently in the `Control Panel`_. You may need to log + out for the ``PATH`` changes to take effect. + +.. _npm: https://www.npmjs.com/ +.. _bundler: http://bundler.io/ +.. _user base: https://docs.python.org/3/library/site.html#site.USER_BASE +.. _user installation: https://pip.pypa.io/en/stable/user_guide/#user-installs +.. _modifying ~/.profile: https://stackoverflow.com/a/14638025 +.. _Control Panel: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776899(v=vs.85).aspx + +Installing packages for your project +------------------------------------ + +Pipenv manages dependencies on a per-project basis. To install packages, +change into your project's directory (or just an empty directory for this +tutorial) and run: + +.. code-block:: bash + + $ cd myproject + $ pipenv install requests + +Pipenv will install the excellent `Requests`_ library and create a ``Pipfile`` +for you in your project's directory. The :ref:`Pipfile` is used to track which +dependencies your project needs in case you need to re-install them, such as +when you share your project with others. You should get output similar to this +(although the exact paths shown will vary): + +.. code-block:: text + + Creating a Pipfile for this project... + Creating a virtualenv for this project... + Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6' + New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6 + Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python + Installing setuptools, pip, wheel...done. + + Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd + Installing requests... + Collecting requests + Using cached requests-2.18.4-py2.py3-none-any.whl + Collecting idna<2.7,>=2.5 (from requests) + Using cached idna-2.6-py2.py3-none-any.whl + Collecting urllib3<1.23,>=1.21.1 (from requests) + Using cached urllib3-1.22-py2.py3-none-any.whl + Collecting chardet<3.1.0,>=3.0.2 (from requests) + Using cached chardet-3.0.4-py2.py3-none-any.whl + Collecting certifi>=2017.4.17 (from requests) + Using cached certifi-2017.7.27.1-py2.py3-none-any.whl + Installing collected packages: idna, urllib3, chardet, certifi, requests + Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 + + Adding requests to Pipfile's [packages]... + P.S. You have excellent taste! ✨ 🍰 ✨ + +.. _Requests: https://python-requests.org + + +Using installed packages +------------------------ + +Now that Requests is installed you can create a simple ``main.py`` file to +use it: + +.. code-block:: python + + import requests + + response = requests.get('https://httpbin.org/ip') + + print('Your IP is {0}'.format(response.json()['origin'])) + +Then you can run this script using ``pipenv run``: + +.. code-block:: bash + + $ pipenv run python main.py + +You should get output similar to this: + +.. code-block:: text + + Your IP is 8.8.8.8 + +Using ``$ pipenv run`` ensures that your installed packages are available to +your script. It's also possible to spawn a new shell that ensures all commands +have access to your installed packages with ``$ pipenv shell``. + + +Next steps ---------- +Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨ + + + +Lower level: virtualenv +======================= + `virtualenv `_ is a tool to create -isolated Python environments. virtualenv creates a folder which contains all the -necessary executables to use the packages that a Python project would need. +isolated Python environments. virtualenv creates a folder which contains all the +necessary executables to use the packages that a Python project would need. + +It can be used standalone, in place of Pipenv. Install virtualenv via pip: .. code-block:: console $ pip install virtualenv + +Test your installation + +.. code-block:: console + + $ virtualenv --version Basic Usage ~~~~~~~~~~~ @@ -32,35 +222,40 @@ Basic Usage .. code-block:: console $ cd my_project_folder - $ virtualenv venv + $ virtualenv my_project -``virtualenv venv`` will create a folder in the current directory which will +``virtualenv my_project`` will create a folder in the current directory which will contain the Python executable files, and a copy of the ``pip`` library which you can use to install other packages. The name of the virtual environment (in this -case, it was ``venv``) can be anything; omitting the name will place the files +case, it was ``my_project``) can be anything; omitting the name will place the files in the current directory instead. This creates a copy of Python in whichever directory you ran the command in, -placing it in a folder named :file:`venv`. +placing it in a folder named :file:`my_project`. -You can also use a Python interpreter of your choice. +You can also use the Python interpreter of your choice (like +``python2.7``). .. code-block:: console - $ virtualenv -p /usr/bin/python2.7 venv + $ virtualenv -p /usr/bin/python2.7 my_project -This will use the Python interpreter in :file:`/usr/bin/python2.7` +or change the interpreter globally with an env variable in ``~/.bashrc``: + +.. code-block:: console + + $ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7 2. To begin using the virtual environment, it needs to be activated: .. code-block:: console - $ source venv/bin/activate + $ source my_project/bin/activate -The name of the current virtual environment will now appear on the left of -the prompt (e.g. ``(venv)Your-Computer:your_project UserName$)`` to let you know -that it's active. From now on, any package that you install using pip will be -placed in the ``venv`` folder, isolated from the global Python installation. +The name of the current virtual environment will now appear on the left of +the prompt (e.g. ``(my_project)Your-Computer:your_project UserName$)`` to let you know +that it's active. From now on, any package that you install using pip will be +placed in the ``my_project`` folder, isolated from the global Python installation. Install packages as usual, for example: @@ -78,8 +273,8 @@ Install packages as usual, for example: This puts you back to the system's default Python interpreter with all its installed libraries. -To delete a virtual environment, just delete its folder. (In this case, -it would be ``rm -rf venv``.) +To delete a virtual environment, just delete its folder. (In this case, +it would be ``rm -rf my_project``.) After a while, though, you might end up with a lot of virtual environments littered across your system, and its possible you'll forget their names or @@ -88,7 +283,7 @@ where they were placed. Other Notes ~~~~~~~~~~~ -Running ``virtualenv`` with the option :option:`--no-site-packages` will not +Running ``virtualenv`` with the option ``--no-site-packages`` will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default behavior for ``virtualenv`` 1.7 and later.] @@ -102,8 +297,8 @@ the current state of the environment packages. To do this, run This will create a :file:`requirements.txt` file, which contains a simple list of all the packages in the current environment, and their respective -versions. You can see the list of installed packages without the requirements -format using "pip list". Later it will be easier for a different developer +versions. You can see the list of installed packages without the requirements +format using "pip list". Later it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions: @@ -115,7 +310,7 @@ This can help ensure consistency across installations, across deployments, and across developers. Lastly, remember to exclude the virtual environment folder from source -control by adding it to the ignore list. +control by adding it to the ignore list (see :ref:`Version Control Ignores`). .. _virtualenvwrapper-ref: @@ -143,7 +338,7 @@ To install (make sure **virtualenv** is already installed): .. code-block:: console $ pip install virtualenvwrapper-win - + In Windows, the default path for WORKON_HOME is %USERPROFILE%\Envs Basic Usage @@ -153,18 +348,18 @@ Basic Usage .. code-block:: console - $ mkvirtualenv venv + $ mkvirtualenv my_project -This creates the :file:`venv` folder inside :file:`~/Envs`. +This creates the :file:`my_project` folder inside :file:`~/Envs`. 2. Work on a virtual environment: .. code-block:: console - $ workon venv + $ workon my_project Alternatively, you can make a project, which creates the virtual environment, -and also a project directory inside ``$PROJECT_HOME``, which is ``cd`` -ed into +and also a project directory inside ``$WORKON_HOME``, which is ``cd`` -ed into when you ``workon myproject``. .. code-block:: console diff --git a/docs/index.rst b/docs/index.rst index 30b73e759..843d663cb 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -20,6 +20,8 @@ This guide is **opinionated** in a way that is almost, but not quite, entirely available here. Rather, you'll find a nice concise list of highly recommended options. +.. note:: The use of **Python 3** is *highly* preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself *still* using Python 2 in production today. If you are using Python 3, congratulations — you are indeed a person of excellent taste. + —*Kenneth Reitz* Let's get started! But first, let's make sure you know where your towel is. diff --git a/docs/intro/community.rst b/docs/intro/community.rst index 754d53307..472c6d546 100644 --- a/docs/intro/community.rst +++ b/docs/intro/community.rst @@ -3,6 +3,8 @@ The Community ============= +.. image:: https://farm5.staticflickr.com/4225/34689432801_78d97ecec9_k_d.jpg + BDFL ---- diff --git a/docs/intro/documentation.rst b/docs/intro/documentation.rst index 1db5bcedb..a75aa3c03 100644 --- a/docs/intro/documentation.rst +++ b/docs/intro/documentation.rst @@ -1,6 +1,8 @@ Documentation ============= +.. image:: https://farm5.staticflickr.com/4178/33928823133_2f3d32cf32_k_d.jpg + Official Documentation ---------------------- diff --git a/docs/intro/duction.rst b/docs/intro/duction.rst index e18d146c2..8e9cc6f0f 100644 --- a/docs/intro/duction.rst +++ b/docs/intro/duction.rst @@ -1,6 +1,8 @@ Introduction ============ +.. image:: https://farm5.staticflickr.com/4180/34725946825_0f85497e60_k_d.jpg + From the `official Python website `_: Python is a general-purpose, high-level programming language similar diff --git a/docs/intro/learning.rst b/docs/intro/learning.rst index 823da5bab..2a7512ac6 100644 --- a/docs/intro/learning.rst +++ b/docs/intro/learning.rst @@ -1,6 +1,8 @@ Learning Python =============== +.. image:: https://farm3.staticflickr.com/2840/32800783863_11a00db52c_k_d.jpg + Beginner -------- @@ -23,6 +25,14 @@ At last it finishes off with tutorial "How to access MySQL db using python" `Python for beginners `_ +Learn Python for Data Science Interactively +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you prefer an online interactive environment to learn Python for Data Science, `this free python tutorial by DataCamp `_ is a great way to get started. If you're already somewhat advanced and interested in machine learning, check out this `course on Supervised Learning with scikit-learn `_, by one the core developers of scikit-learn. + + +`Python for data science `_ + Learn Python Interactive Tutorial ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -39,6 +49,15 @@ If you want a more traditional book, *Python For You and Me* is an excellent resource for learning all aspects of the language. `Python for You and Me `_ + `Learn Python Interactively with DataCamp! `_ + +Learn Python Step by Step +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Techbeamers.com provides step-by-step tutorials to teach Python. Each tutorial is supplemented with logically added coding snippets and equips with a follow-up quiz on the subject learned. There is a section for `Python interview questions `_ to help job seekers. You can also read essential `Python tips `_ and learn `best coding practices `_ for writing quality code. Here, you'll get the right platform to learn Python quickly. + +`Learn Python Basic to Advanced `_ + Online Python Tutor ~~~~~~~~~~~~~~~~~~~ @@ -97,7 +116,7 @@ Dive Into Python 3 is a good book for those ready to jump in to Python 3. It's a good read if you are moving from Python 2 to 3 or if you already have some experience programming in another language. - `Dive Into Python 3 `_ + `Dive Into Python 3 `_ Think Python: How to Think Like a Computer Scientist @@ -145,7 +164,7 @@ A free introductory book that teaches Python at the beginner level, it assumes no previous programming experience. `A Byte of Python for Python 2.x `_ - `A Byte of Python for Python 3.x `_ + `A Byte of Python for Python 3.x `_ Learn to Program in Python with Codeacademy @@ -210,6 +229,10 @@ and can make classes and objects behave in different and magical ways. `A Guide to Python's Magic Methods `_ +.. note:: The Rafekettler.com is currently down, you can go to their Github version directly. Here you can find a PDF version: + `A Guide to Python's Magic Methods (repo on GitHub) `_ + + For Engineers and Scientists ---------------------------- diff --git a/docs/intro/news.rst b/docs/intro/news.rst index 54f8ff70f..c3a019551 100644 --- a/docs/intro/news.rst +++ b/docs/intro/news.rst @@ -1,6 +1,8 @@ News ==== +.. image:: https://farm4.staticflickr.com/3804/33573767786_eececc5d27_k_d.jpg + Planet Python ~~~~~~~~~~~~~ @@ -16,6 +18,20 @@ Python-related news. `/r/python `_ +Talk Python Podcast +~~~~~~~~~~~~~~~~~~~ + +The #1 Python-focused podcast covering the people and ideas in Python. + + `Talk Python To Me `_ + +Python Bytes Podcast +~~~~~~~~~~~~~~~~~~~~ + +A short-form Python podcast covering recent developer headlines. + + `Python Bytes `_ + Pycoder's Weekly ~~~~~~~~~~~~~~~~ @@ -49,7 +65,7 @@ delivered in your inbox. Keep Your Python Programming Skills Updated. `Import Python Weekly Newsletter `_ Awesome Python Newsletter -~~~~~~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~~~~~~~~~~ A weekly overview of the most popular Python news, articles and packages. diff --git a/docs/notes/contribute.rst b/docs/notes/contribute.rst index 0d709f29f..b75f55926 100644 --- a/docs/notes/contribute.rst +++ b/docs/notes/contribute.rst @@ -1,6 +1,8 @@ Contribute ~~~~~~~~~~ +.. image:: https://farm3.staticflickr.com/2858/33573769116_49c1ef51e7_k_d.jpg + Python-guide is under active development, and contributors are welcome. If you have a feature request, suggestion, or bug report, please open a new diff --git a/docs/notes/license.rst b/docs/notes/license.rst index 5a7f54351..6df4ee6b8 100644 --- a/docs/notes/license.rst +++ b/docs/notes/license.rst @@ -2,4 +2,6 @@ License ======= -The Guide is licensed under the `Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license `_. \ No newline at end of file +.. image:: https://farm4.staticflickr.com/3762/32800805573_568d6b72fd_k_d.jpg + +The Guide is licensed under the `Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license `_. diff --git a/docs/notes/styleguide.rst b/docs/notes/styleguide.rst index 768240b0e..c2d4034ec 100644 --- a/docs/notes/styleguide.rst +++ b/docs/notes/styleguide.rst @@ -4,6 +4,8 @@ The Guide Style Guide ===================== +.. image:: https://farm4.staticflickr.com/3684/33573755856_7f43d43adf_k_d.jpg + As with all documentation, having a consistent format helps make the document more understandable. In order to make The Guide easier to digest, all contributions should fit within the rules of this style guide where diff --git a/docs/scenarios/admin.rst b/docs/scenarios/admin.rst index 8e738d9c0..ee7b5127b 100644 --- a/docs/scenarios/admin.rst +++ b/docs/scenarios/admin.rst @@ -1,6 +1,8 @@ Systems Administration ====================== +.. image:: https://farm5.staticflickr.com/4179/34435690580_3afec7d4cd_k_d.jpg + Fabric ------ @@ -131,7 +133,7 @@ State files can be written using YAML, the Jinja2 template system or pure Python Psutil ------ -`Psutil `_ is an interface to different +`Psutil `_ is an interface to different system information (e.g. CPU, memory, disks, network, users and processes). Here is an example to be aware of some server overload. If any of the diff --git a/docs/scenarios/ci.rst b/docs/scenarios/ci.rst index 16e0ea0df..457f2321f 100644 --- a/docs/scenarios/ci.rst +++ b/docs/scenarios/ci.rst @@ -1,6 +1,10 @@ Continuous Integration ====================== +.. image:: https://farm5.staticflickr.com/4173/33907150594_9abba7ad0a_k_d.jpg + +.. note:: + For advice on writing your tests, see :doc:`/writing/tests`. Why? ---- @@ -77,7 +81,7 @@ this example content:: This will get your project tested on all the listed Python versions by running the given script, and will only build the master branch. There are a lot more options you can enable, like notifications, before and after steps -and much more. The `travis-ci docs `_ +and much more. The `travis-ci docs `_ explain all of these options, and are very thorough. In order to activate testing for your project, go to `the travis-ci site `_ diff --git a/docs/scenarios/cli.rst b/docs/scenarios/cli.rst index ab357c2fd..b6c7e7376 100644 --- a/docs/scenarios/cli.rst +++ b/docs/scenarios/cli.rst @@ -1,6 +1,8 @@ Command-line Applications ========================= +.. image:: https://farm5.staticflickr.com/4272/34435690330_11930b5987_k_d.jpg + Command-line applications, also referred to as `Console Applications `_, are computer programs designed to be used from a text interface, such as a @@ -30,7 +32,7 @@ column printer, iterator based progress bars and implicit argument handling. Click ----- -`click `_ is an upcoming Python package for creating +`click `_ is a Python package for creating command-line interfaces in a composable way with as little code as possible. This “Command-line Interface Creation Kit” is highly configurable but comes with good defaults out of the box. diff --git a/docs/scenarios/clibs.rst b/docs/scenarios/clibs.rst index 82e041fe3..712d7f88e 100644 --- a/docs/scenarios/clibs.rst +++ b/docs/scenarios/clibs.rst @@ -1,6 +1,8 @@ Interfacing with C/C++ Libraries ================================ +.. image:: https://farm5.staticflickr.com/4173/34725951345_c8f5959a2e_k_d.jpg + C Foreign Function Interface ---------------------------- @@ -96,26 +98,27 @@ Example: Overloading __repr__ std::string getName(); }; + :file:`myclass.i` -.. code-block:: c++ +.. code-block:: idl :linenos: %include "string.i" - + %module myclass %{ #include #include "MyClass.h" %} - + %extend MyClass { std::string __repr__() { return $self->getName(); } } - + %include "MyClass.h" diff --git a/docs/scenarios/client.rst b/docs/scenarios/client.rst index 2dc7748cd..99c7ac08d 100644 --- a/docs/scenarios/client.rst +++ b/docs/scenarios/client.rst @@ -1,7 +1,7 @@ Network Applications ==================== - +.. image:: https://farm5.staticflickr.com/4251/34364815780_bea6614025_k_d.jpg HTTP :::: diff --git a/docs/scenarios/crypto.rst b/docs/scenarios/crypto.rst index e4b103a9d..191062afe 100644 --- a/docs/scenarios/crypto.rst +++ b/docs/scenarios/crypto.rst @@ -1,6 +1,8 @@ Cryptography ============ +.. image:: https://farm5.staticflickr.com/4220/33907152824_bf91078cc1_k_d.jpg + Cryptography ------------ diff --git a/docs/scenarios/db.rst b/docs/scenarios/db.rst index 58d9cd20b..8da9a203e 100644 --- a/docs/scenarios/db.rst +++ b/docs/scenarios/db.rst @@ -1,6 +1,8 @@ Databases ========= +.. image:: https://farm5.staticflickr.com/4225/33907152464_a99fdcc8de_k_d.jpg + DB-API ------ diff --git a/docs/scenarios/gui.rst b/docs/scenarios/gui.rst index f56b212b7..ed683d62e 100644 --- a/docs/scenarios/gui.rst +++ b/docs/scenarios/gui.rst @@ -1,6 +1,9 @@ GUI Applications ================ +.. image:: https://farm5.staticflickr.com/4250/33907143624_cd621b535c_k_d.jpg + + Alphabetical list of GUI Applications. Camelot @@ -85,6 +88,16 @@ Qt is widely used for developing software with a GUI but can also be used for non-GUI applications. +Toga +---- +`Toga `_ is a Python native, OS +native, cross platform GUI toolkit. Toga consists of a library of base +components with a shared interface to simplify platform-agnostic GUI +development. + +Toga is available on Mac OS, Windows, Linux (GTK), and mobile platforms such +as Android and iOS. + Tk -- Tkinter is a thin object-oriented layer on top of Tcl/Tk. **It has the advantage diff --git a/docs/scenarios/imaging.rst b/docs/scenarios/imaging.rst index 49d3af1eb..984d6dbf4 100644 --- a/docs/scenarios/imaging.rst +++ b/docs/scenarios/imaging.rst @@ -2,6 +2,8 @@ Image Manipulation ================== +.. image:: https://farm5.staticflickr.com/4157/34575689432_3de8e9a348_k_d.jpg + Most image processing and manipulation techniques can be carried out effectively using two libraries: Python Imaging Library (PIL) and OpenSource Computer Vision (OpenCV). diff --git a/docs/scenarios/json.rst b/docs/scenarios/json.rst index 860e79435..55de04ef7 100644 --- a/docs/scenarios/json.rst +++ b/docs/scenarios/json.rst @@ -1,6 +1,8 @@ JSON ==== +.. image:: https://farm5.staticflickr.com/4174/33928819683_97b5c6a184_k_d.jpg + The `json `_ library can parse JSON from strings or files. The library parses JSON into a Python dictionary or list. It can also convert Python dictionaries or lists into JSON strings. diff --git a/docs/scenarios/ml.rst b/docs/scenarios/ml.rst index 25cab8045..fde454276 100644 --- a/docs/scenarios/ml.rst +++ b/docs/scenarios/ml.rst @@ -2,6 +2,8 @@ Machine Learning ================ +.. image:: https://farm4.staticflickr.com/3954/34018729885_002ced9b54_k_d.jpg + Python has a vast number of libraries for data analysis, statistics and Machine Learning itself, making it a language of choice for many data scientists. Some widely used packages for Machine Learning and other Data Science applications are enlisted below. @@ -114,4 +116,4 @@ Since we're splitting randomly and the classifier trains on every iteration, the The first line contains the labels (i.e flower species) of the testing data as predicted by our classifier, and the second line contains the actual flower species as given in the dataset. We thus get an accuracy of 100% this time. -More on scikit-learn can be read in the `documentation `_. \ No newline at end of file +More on scikit-learn can be read in the `documentation `_. diff --git a/docs/scenarios/network.rst b/docs/scenarios/network.rst index dd8bfa67d..0aa5860c9 100644 --- a/docs/scenarios/network.rst +++ b/docs/scenarios/network.rst @@ -1,6 +1,8 @@ Networking ========== +.. image:: https://farm3.staticflickr.com/2892/34151833832_6bdfd930af_k_d.jpg + Twisted ------- diff --git a/docs/scenarios/scientific.rst b/docs/scenarios/scientific.rst index 92c8c801b..f81ecef1b 100644 --- a/docs/scenarios/scientific.rst +++ b/docs/scenarios/scientific.rst @@ -2,6 +2,8 @@ Scientific Applications ======================= +.. image:: https://farm3.staticflickr.com/2890/33925223870_97e44f5629_k_d.jpg + Context ::::::: diff --git a/docs/scenarios/scrape.rst b/docs/scenarios/scrape.rst index 889a93082..e32978266 100644 --- a/docs/scenarios/scrape.rst +++ b/docs/scenarios/scrape.rst @@ -1,6 +1,8 @@ HTML Scraping ============= +.. image:: https://farm3.staticflickr.com/2900/34268661876_442428e122_k_d.jpg + Web Scraping ------------ @@ -49,7 +51,7 @@ will focus on the former. XPath is a way of locating information in structured documents such as HTML or XML documents. A good introduction to XPath is on -`W3Schools `_ . +`W3Schools `_ . There are also various tools for obtaining the XPath of elements such as FireBug for Firefox or the Chrome Inspector. If you're using Chrome, you @@ -103,3 +105,5 @@ using Python or we can save it to a file and share it with the world. Some more cool ideas to think about are modifying this script to iterate through the rest of the pages of this example dataset, or rewriting this application to use threads for improved speed. + +If you want to learn how to import data using python - this `DataCamp course on Importing Data `_ is a great place to start. diff --git a/docs/scenarios/serialization.rst b/docs/scenarios/serialization.rst index ac0494f14..8a950bad9 100644 --- a/docs/scenarios/serialization.rst +++ b/docs/scenarios/serialization.rst @@ -2,6 +2,8 @@ Data Serialization ================== +.. image:: https://farm3.staticflickr.com/2927/33467946364_3e59bd376a_k_d.jpg + What is data serialization? --------------------------- diff --git a/docs/scenarios/speed.rst b/docs/scenarios/speed.rst index 2c775b880..18e84ec92 100644 --- a/docs/scenarios/speed.rst +++ b/docs/scenarios/speed.rst @@ -1,6 +1,8 @@ Speed ===== +.. image:: https://farm3.staticflickr.com/2826/33175625804_e225b90f3e_k_d.jpg + CPython, the most commonly used implementation of Python, is slow for CPU bound tasks. `PyPy`_ is fast. @@ -448,3 +450,4 @@ Multiprocessing .. _`threading`: https://docs.python.org/3/library/threading.html .. _`stackoverflow post`: http://stackoverflow.com/questions/26688424/python-threads-are-printing-at-the-same-time-messing-up-the-text-output .. _`data race`: https://en.wikipedia.org/wiki/Race_condition +.. _`Lock`: https://docs.python.org/3/library/threading.html#lock-objects diff --git a/docs/scenarios/web.rst b/docs/scenarios/web.rst index 9e07a59f5..7fbb8b7c4 100644 --- a/docs/scenarios/web.rst +++ b/docs/scenarios/web.rst @@ -1,6 +1,8 @@ -================ -Web Applications -================ +============================= +Web Applications & Frameworks +============================= + +.. image:: https://farm3.staticflickr.com/2891/34309496175_b82d104282_k_d.jpg As a powerful scripting language adapted to both fast prototyping and bigger projects, Python is widely used in web application @@ -10,6 +12,7 @@ Context ::::::: + WSGI ---- @@ -59,7 +62,7 @@ modules `_ that can be incorporated into a new project as-is, or customized to fit your needs. There are annual Django conferences `in the United States -`_ and `in Europe `_. +`_, `Europe `_, and `Australia `_. The majority of new Python web applications today are built with Django. @@ -221,19 +224,12 @@ how to set up your first application. Heroku is the recommended PaaS for deploying Python web applications today. -Gondor ------- - -`Gondor `_ is a PaaS specialized for deploying Django -and Pinax applications. Gondor recommends Django version 1.6 and supports any -WSGI application on Python version 2.7. Gondor can automatically configure your -Django site if you use :file:`local_settings.py` for site-specific configuration -information. - -Gondor has a guide on deploying `Django projects `_. +Eldarion +-------- -Gondor is run by a small company and focuses on helping businesses find success with -Python and Django. +`Eldarion `_ (formely known as Gondor) is a PaaS powered +by Kubernetes, CoreOS, and Docker. They support any WSGI application and have a +guide on deploying `Django projects `_. Templating :::::::::: @@ -372,7 +368,7 @@ into the corresponding block in the :file:`base.html` page. .. code-block:: html -
    @@ -464,7 +460,7 @@ frameworks. An example template in Mako looks like: -.. code-block:: html +.. code-block:: mako <%inherit file="base.html"/> <% diff --git a/docs/scenarios/xml.rst b/docs/scenarios/xml.rst index 0cc3cdc4c..8af9e416c 100644 --- a/docs/scenarios/xml.rst +++ b/docs/scenarios/xml.rst @@ -1,6 +1,8 @@ XML parsing =========== +.. image:: https://farm3.staticflickr.com/2808/33888714601_a1f7d020a2_k_d.jpg + untangle -------- diff --git a/docs/shipping/freezing.rst b/docs/shipping/freezing.rst index d42f2d61b..f0b73b41d 100644 --- a/docs/shipping/freezing.rst +++ b/docs/shipping/freezing.rst @@ -4,11 +4,13 @@ Freezing Your Code ================== +.. image:: https://farm5.staticflickr.com/4227/33907151034_e0a9e53402_k_d.jpg + "Freezing" your code is creating a single-file executable file to distribute to end-users, that contains all of your application code as well as the Python interpreter. -Applications such as 'Dropbox', 'Eve Online', 'Civilisation IV', and +Applications such as 'Dropbox', 'Eve Online', 'Civilization IV', and BitTorrent clients do this. The advantage of distributing this way is that your application will "just work", @@ -60,7 +62,7 @@ py2app no no yes yes MIT no yes yes .. note:: All solutions need MS Visual C++ dll to be installed on target machine, except py2app. Only Pyinstaller makes self-executable exe that bundles the dll when - passing :option:`--onefile` to :file:`Configure.py`. + passing ``--onefile`` to :file:`Configure.py`. Windows ------- @@ -70,7 +72,46 @@ bbFreeze Prerequisite is to install :ref:`Python, Setuptools and pywin32 dependency on Windows `. -.. todo:: Write steps for most basic .exe +1. Install :code:`bbfreeze`: + +.. code-block:: console + + $ pip install bbfreeze + +2. Write most basic :file:`bb_setup.py` + +.. code-block:: python + + from bbfreeze import Freezer + + freezer = Freezer(distdir='dist') + freezer.addScript('foobar.py', gui_only=True) + freezer() + +.. note:: + + This will work for the most basic one file scripts. For more advanced freezing you will have to provide + include and exclude paths like so + + .. code-block:: python + + freezer = Freezer(distdir='dist', includes=['my_code'], excludes=['docs']) + +3. (Optionally) include icon + +.. code-block:: python + + freezer.setIcon('my_awesome_icon.ico') + +4. Provide the Microsoft Visual C runtime DLL for the freezer. It might be possible to append your :code:`sys.path` +with Microsoft Visual Studio path but I find it easier to drop :file:`msvcp90.dll` in the same folder where your script +resides. + +5. Freeze! + +.. code-block:: console + + $ python bb_setup.py py2exe ~~~~~~ @@ -108,7 +149,7 @@ PyInstaller Prerequisite is to have installed :ref:`Python, Setuptools and pywin32 dependency on Windows `. - `Most basic tutorial `_ -- `Manual `_ +- `Manual `_ OS X @@ -163,7 +204,7 @@ To create a standalone windowed OS X application, use the :code:`--windowed` opt This creates a :code:`script.app` in the :code:`dist` folder. Make sure to use GUI packages in your Python code, like `PyQt `_ or `PySide `_, to control the graphical parts of the app. -There are several options in :code:`script.spec` related to Mac OS X app bundles `here `_. For example, to specify an icon for the app, use the :code:`icon=\path\to\icon.icns` option. +There are several options in :code:`script.spec` related to Mac OS X app bundles `here `_. For example, to specify an icon for the app, use the :code:`icon=\path\to\icon.icns` option. Linux diff --git a/docs/shipping/packaging.rst b/docs/shipping/packaging.rst index c67957529..7e55d4207 100644 --- a/docs/shipping/packaging.rst +++ b/docs/shipping/packaging.rst @@ -4,6 +4,8 @@ Packaging Your Code =================== +.. image:: https://farm5.staticflickr.com/4325/36137234682_be6898bf57_k_d.jpg + Package your code to share it with other developers. For example to share a library for other developers to use in their application, or for development tools like 'py.test'. @@ -101,7 +103,7 @@ pypiserver `Pypiserver `_ is a minimal PyPI compatible server. It can be used to serve a set of packages to easy_install or pip. It includes helpful features like an administrative command -(:option:`-U`) which will update all its packages to their latest versions +(``-U``) which will update all its packages to their latest versions found on PyPI. @@ -152,7 +154,7 @@ Also, if a distribution releases a new security update for Python, then your application will automatically start using that new version of Python. The bdist_rpm command makes `producing an RPM file `_ -for use by distributions like Red Hat or SuSE is trivially easy. +for use by distributions like Red Hat or SuSE trivially easy. However, creating and maintaining the different configurations required for each distribution's format (e.g. .deb for Debian/Ubuntu, .rpm for Red diff --git a/docs/starting/install/linux.rst b/docs/starting/install/linux.rst index aa56ec07d..de831b6e0 100644 --- a/docs/starting/install/linux.rst +++ b/docs/starting/install/linux.rst @@ -1,25 +1,29 @@ .. _install-linux: -Installing Python on Linux -========================== +Installing Python 2 on Linux +============================= -The latest versions of CentOS, Fedora, Redhat Enterprise (RHEL) and Ubuntu +.. image:: https://farm5.staticflickr.com/4268/34435688560_4cc2a7bcbb_k_d.jpg + +.. note:: + Check out our :ref:`guide for installing Python 3 on Linux`. + +The latest versions of CentOS, Red Hat Enterprise Linux (RHEL) and Ubuntu **come with Python 2.7 out of the box**. To see which version of Python you have installed, open a command prompt and run .. code-block:: console - $ python --version + $ python2 --version -Some older versions of RHEL and CentOS come with Python 2.4 which is -unacceptable for modern Python development. Fortunately, there are -`Extra Packages for Enterprise Linux`_ which include high -quality additional packages based on their Fedora counterparts. This -repository contains a Python 2.6 package specifically designed to install -side-by-side with the system's Python 2.4 installation. +However, with the growing popularity of Python 3, some distributions, such as +Fedora, don't come with Python 2 pre-installed. You can install the ``python2`` +package with your distribution package manager: + +.. code-block:: console -.. _Extra Packages for Enterprise Linux: http://fedoraproject.org/wiki/EPEL + $ sudo dnf install python2 You do not need to install or configure anything else to use Python. Having said that, I would strongly recommend that you install the tools and libraries @@ -32,11 +36,11 @@ Setuptools & Pip The two most crucial third-party Python packages are `setuptools `_ and `pip `_. -Once installed, you can download, install and uninstall any compliant Python software -product with a single command. It also enables you to add this network installation +Once installed, you can download, install and uninstall any compliant Python software +product with a single command. It also enables you to add this network installation capability to your own Python software with very little work. -Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include +Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include pip by default. To see if pip is installed, open a command prompt and run @@ -50,15 +54,15 @@ To install pip, `follow the official pip installation guide ` docs. +To start using this and see more information: :ref:`Virtual Environments ` docs. You can also use :ref:`virtualenvwrapper ` to make it easier to manage your virtual environments. diff --git a/docs/starting/install/osx.rst b/docs/starting/install/osx.rst index f459ef99e..f83bd4f07 100644 --- a/docs/starting/install/osx.rst +++ b/docs/starting/install/osx.rst @@ -1,17 +1,22 @@ .. _install-osx: -Installing Python on Mac OS X -============================= +Installing Python 2 on Mac OS X +=============================== -The latest version of Mac OS X, El Capitan, **comes with Python 2.7 out of the box**. +.. image:: https://farm5.staticflickr.com/4268/34435688560_4cc2a7bcbb_k_d.jpg -You do not need to install or configure anything else to use Python. Having -said that, I would strongly recommend that you install the tools and libraries -described in the next section before you start building Python applications -for real-world use. In particular, you should always install Setuptools, as it -makes it much easier for you to use other third-party Python libraries. +.. note:: + Check out our :ref:`guide for installing Python 3 on OS X`. + +The latest version of Mac OS X, High Sierra, **comes with Python 2.7 out of the box**. -The version of Python that ships with OS X is great for learning but it's not +You do not need to install or configure anything else to use Python. Having said +that, I would strongly recommend that you install the tools and libraries +described in the next section before you start building Python applications for +real-world use. In particular, you should always install Setuptools, as it makes +it much easier for you to install and manage other third-party Python libraries. + +The version of Python that ships with OS X is great for learning, but it's not good for development. The version shipped with OS X may be out of date from the `official current Python release `_, which is considered the stable production version. @@ -30,9 +35,14 @@ minimal but unofficial package. .. note:: - If you already have Xcode installed or plan to use Homebrew, do not install - OSX-GCC-Installer. In combination, the software can cause issues that are - difficult to diagnose. + If you already have XCode installed, do not install OSX-GCC-Installer. + In combination, the software can cause issues that are difficult to + diagnose. + +.. note:: + If you perform a fresh install of XCode, you will also need to add the + commandline tools by running ``xcode-select --install`` on the terminal. + While OS X comes with a large number of UNIX utilities, those familiar with Linux systems will notice one key component missing: a decent package manager. @@ -67,7 +77,16 @@ or Python 3: $ brew install python3 -This will take a minute or two. +This will take a minute or two. + +Homebrew names the executable ``python2`` so that you can still run the system Python via the executable ``python``. + + +.. code-block:: console + + $ python -V # system Python interpreter + $ python2 -V # Homebrew installed Python 2 interpreter + $ python3 -V # Homebrew installed Python 3 interpreter (if installed) Setuptools & Pip @@ -81,23 +100,29 @@ software over a network (usually the Internet) with a single command capability to your own Python software with very little work. ``pip`` is a tool for easily installing and managing Python packages, -that is recommended over ``easy_install``. It is superior to ``easy_install`` in `several ways `_, +that is recommended over ``easy_install``. It is superior to ``easy_install`` +in `several ways `_, and is actively maintained. +.. code-block:: console + + $ pip2 -V # pip pointing to the Homebrew installed Python 2 interpreter + $ pip3 -V # pip pointing to the Homebrew installed Python 3 interpreter (if installed) + + Virtual Environments -------------------- -A Virtual Environment is a tool to keep the dependencies required by different projects -in separate places, by creating virtual Python environments for them. It solves the -"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps +A Virtual Environment (commonly referred to as a 'virtualenv') is a tool to keep the dependencies required by different projects +in separate places, by creating virtual Python environments for them. It solves the +"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps your global site-packages directory clean and manageable. For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8. -To start using this and see more information: :ref:`Virtual Environments ` docs. - +To start using this and see more information: :ref:`Virtual Environments ` docs. -------------------------------- diff --git a/docs/starting/install/win.rst b/docs/starting/install/win.rst index 131ace663..e930b4683 100644 --- a/docs/starting/install/win.rst +++ b/docs/starting/install/win.rst @@ -1,10 +1,15 @@ .. _install-windows: -Installing Python on Windows -============================ +Installing Python 2 on Windows +============================== -First, download the `latest version `_ -of Python 2.7 from the official Website. If you want to be sure you are installing a fully +.. image:: https://farm5.staticflickr.com/4268/34435688560_4cc2a7bcbb_k_d.jpg + +.. note:: + Check out our :ref:`guide for installing Python 3 on Windows`. + +First, download the `latest version `_ +of Python 2.7 from the official website. If you want to be sure you are installing a fully up-to-date version, click the Downloads > Windows link from the home page of the `Python.org web site `_ . @@ -35,6 +40,8 @@ You can do this easily by running the following in ``powershell``: [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User") +This is also an option during the installation process. + The second (:file:`Scripts`) directory receives command files when certain packages are installed, so it is a very useful addition. You do not need to install or configure anything else to use Python. Having @@ -46,38 +53,36 @@ makes it much easier for you to use other third-party Python libraries. Setuptools + Pip ---------------- -The most crucial third-party Python software of all is Setuptools, which -extends the packaging and installation facilities provided by the distutils in -the standard library. Once you add Setuptools to your Python system you can -download and install any compliant Python software product with a single -command. It also enables you to add this network installation capability to -your own Python software with very little work. +The two most crucial third-party Python packages are `setuptools `_ and `pip `_. -To obtain the latest version of Setuptools for Windows, run the Python script -available here: `ez_setup.py `_ +Once installed, you can download, install and uninstall any compliant Python software +product with a single command. It also enables you to add this network installation +capability to your own Python software with very little work. +Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include +pip by default. + +To see if pip is installed, open a command prompt and run + +.. code-block:: console -You'll now have a new command available to you: **easy_install**. It is -considered by many to be deprecated, so we will install its replacement: -**pip**. Pip allows for uninstallation of packages, and is actively maintained, -unlike easy_install. + $ command -v pip -To install pip, run the Python script available here: -`get-pip.py `_ +To install pip, `follow the official pip installation guide `_ - this will automatically install the latest version of setuptools. Virtual Environments -------------------- -A Virtual Environment is a tool to keep the dependencies required by different projects -in separate places, by creating virtual Python environments for them. It solves the -"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps +A Virtual Environment is a tool to keep the dependencies required by different projects +in separate places, by creating virtual Python environments for them. It solves the +"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps your global site-packages directory clean and manageable. For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8. -To start using this and see more information: :ref:`Virtual Environments ` docs. +To start using this and see more information: :ref:`Virtual Environments ` docs. -------------------------------- diff --git a/docs/starting/install3/linux.rst b/docs/starting/install3/linux.rst new file mode 100644 index 000000000..1c6d67d35 --- /dev/null +++ b/docs/starting/install3/linux.rst @@ -0,0 +1,112 @@ +.. _install3-linux: + +Installing Python 3 on Linux +============================ + +.. image:: https://farm5.staticflickr.com/4276/34435689480_2e6f358510_k_d.jpg + +This document describes how to install Python 3.6 on Ubuntu Linux machines. + +To see which version of Python 3 you have installed, open a command prompt and run + +.. code-block:: console + + $ python3 --version + +If you are using Ubuntu 16.10 or newer, then you can easily install Python 3.6 with the following commands:: + + $ sudo apt-get update + $ sudo apt-get install python3.6 + +If you're using another version of Ubuntu (e.g. the latest LTS release), we recommend using the `deadsnakes PPA `_ to install Python 3.6:: + + $ sudo apt-get install software-properties-common + $ sudo add-apt-repository ppa:deadsnakes/ppa + $ sudo apt-get update + $ sudo apt-get install python3.6 + +If you are using other Linux distribution, chances are you already have Python 3 +pre-installed as well. If not, use your distribution's package manager. +For example on Fedora, you would use `dnf`: + +.. code-block:: console + + $ sudo dnf install python3 + +Note that if the version of the ``python3`` package is not recent enough +for you, there may be ways of installing more recent versions as well, +depending on you distribution. For example installing the ``python36`` package +on Fedora 25 to get Python 3.6. If you are a Fedora user, you might want +to read about `multiple Python versions available in Fedora`_. + +.. _multiple Python versions available in Fedora: https://developer.fedoraproject.org/tech/languages/python/multiple-pythons.html + + +Working with Python 3 +--------------------- + +At this point, you may have system Python 2.7 available as well. + +.. code-block:: console + + $ python + +This will launch the Python 2 interpreter. + +.. code-block:: console + + $ python3 + +This will launch the Python 3 interpreter. + +Setuptools & Pip +---------------- + +The two most crucial third-party Python packages are `setuptools `_ and `pip `_. + +Once installed, you can download, install and uninstall any compliant Python software +product with a single command. It also enables you to add this network installation +capability to your own Python software with very little work. + +Python 2.7.9 and later (on the python2 series), and Python 3.4 and later include +pip by default. + +To see if pip is installed, open a command prompt and run + +.. code-block:: console + + $ command -v pip + +To install pip, `follow the official pip installation guide `_ - this will automatically install the latest version of setuptools. + +Note that on some Linux distributions including Ubuntu and Fedora the ``pip`` +command is meant for Python 2, while the ``pip3`` command is meant for Python 3. + +.. code-block:: console + + $ command -v pip3 + +However, when using virtual environments (described below), you don't need to +care about that. + + +Pipenv & Virtual Environments +----------------------------- + +The next step is to install Pipenv, so you can install dependencies and manage virtual environments. + +A Virtual Environment is a tool to keep the dependencies required by different projects +in separate places, by creating virtual Python environments for them. It solves the +"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps +your global site-packages directory clean and manageable. + +For example, you can work on a project which requires Django 1.10 while also +maintaining a project which requires Django 1.8. + +So, onward! To the :ref:`Pipenv & Virtual Environments ` docs! + +-------------------------------- + +This page is a remixed version of `another guide `_, +which is available under the same license. + diff --git a/docs/starting/install3/osx.rst b/docs/starting/install3/osx.rst new file mode 100644 index 000000000..bd7ad7168 --- /dev/null +++ b/docs/starting/install3/osx.rst @@ -0,0 +1,124 @@ +:orphan: This article should not be added to a toctree for now + +.. _install3-osx: + +Installing Python 3 on Mac OS X +=============================== + +.. image:: https://farm5.staticflickr.com/4276/34435689480_2e6f358510_k_d.jpg + +The latest version of Mac OS X, High Sierra, **comes with Python 2.7 out of the box**. + +You do not need to install or configure anything else to use Python 2. These +instructions document the installation of Python 3. + +The version of Python that ships with OS X is great for learning, but it's not +good for development. The version shipped with OS X may be out of date from the +`official current Python release `_, +which is considered the stable production version. + +Doing it Right +-------------- + +Let's install a real version of Python. + +Before installing Python, you'll need to install GCC. GCC can be obtained +by downloading `XCode `_, the smaller +`Command Line Tools `_ (must have an +Apple account) or the even smaller `OSX-GCC-Installer `_ +package. + +.. note:: + If you already have XCode installed, do not install OSX-GCC-Installer. + In combination, the software can cause issues that are difficult to + diagnose. + +.. note:: + If you perform a fresh install of XCode, you will also need to add the + commandline tools by running ``xcode-select --install`` on the terminal. + +While OS X comes with a large number of UNIX utilities, those familiar with +Linux systems will notice one key component missing: a package manager. +`Homebrew `_ fills this void. + +To `install Homebrew `_, open :file:`Terminal` or +your favorite OSX terminal emulator and run + +.. code-block:: console + + $ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" + +The script will explain what changes it will make and prompt you before the +installation begins. +Once you've installed Homebrew, insert the Homebrew directory at the top +of your :envvar:`PATH` environment variable. You can do this by adding the following +line at the bottom of your :file:`~/.profile` file + +.. code-block:: console + + export PATH=/usr/local/bin:/usr/local/sbin:$PATH + +Now, we can install Python 3: + +.. code-block:: console + + $ brew install python3 + +This will take a minute or two. + + +Pip +--- + +Homebrew installs ``pip3`` for you. + +``pip3`` is the alias to ``pip`` pointing to the Homebrew'd Python 3. + +Working with Python 3 +--------------------- + +At this point, you have the system Python 2.7 available, potentially the +:ref:`Homebrew version of Python 2 ` installed, and the Homebrew +version of Python 3 as well. + +.. code-block:: console + + $ python + +will launch the system Python interpreter. + +.. code-block:: console + + $ python2 + +will launch the homebrew-installed Python 2 interpreter (if any). + +.. code-block:: console + + $ python3 + +will launch the homebrew-installed Python 3 interpreter. + +If the Homebrew version of Python 2 is installed then ``pip2`` will point to Python 2. +If the Homebrew version of Python 3 is installed then ``pip3`` will point to Python 3. + + +Pipenv & Virtual Environments +----------------------------- + +The next step is to install Pipenv, so you can install dependencies and manage virtual environments. + +A Virtual Environment is a tool to keep the dependencies required by different projects +in separate places, by creating virtual Python environments for them. It solves the +"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps +your global site-packages directory clean and manageable. + +For example, you can work on a project which requires Django 1.10 while also +maintaining a project which requires Django 1.8. + +So, onward! To the :ref:`Pipenv & Virtual Environments ` docs! + +-------------------------------- + +This page is a remixed version of `another guide `_, +which is available under the same license. diff --git a/docs/starting/install3/win.rst b/docs/starting/install3/win.rst new file mode 100644 index 000000000..74fe7d0a4 --- /dev/null +++ b/docs/starting/install3/win.rst @@ -0,0 +1,95 @@ +.. _install3-windows: + +Installing Python 3 on Windows +============================== + +.. image:: https://farm5.staticflickr.com/4276/34435689480_2e6f358510_k_d.jpg + +First, download the `latest version `_ +of Python 3.6 from the official website. If you want to be sure you are installing a fully +up-to-date version, click the Downloads > Windows link from the home page of the +`Python.org web site `_ . + +By design, Python installs to a directory with the version number embedded, +e.g. Python version 3.6 will install at :file:`C:\\Python36\\`, so that you can +have multiple versions of Python on the +same system without conflicts. Of course, only one interpreter can be the +default application for Python file types. It also does not automatically +modify the :envvar:`PATH` environment variable, so that you always have control over +which copy of Python is run. + +Typing the full path name for a Python interpreter each time quickly gets +tedious, so add the directories for your default Python version to the :envvar:`PATH`. +Assuming that your Python installation is in :file:`C:\\Python36\\`, add this to your +:envvar:`PATH`: + +.. code-block:: console + + C:\Python36\;C:\Python36\Scripts\ + +You can do this easily by running the following in ``powershell``: + +.. code-block:: console + + [Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python36\;C:\Python36\Scripts\", "User") + +This is also an option during the installation process. + +The second (:file:`Scripts`) directory receives command files when certain +packages are installed, so it is a very useful addition. +You do not need to install or configure anything else to use Python. Having +said that, I would strongly recommend that you install the tools and libraries +described in the next section before you start building Python applications for +real-world use. In particular, you should always install Setuptools, as it +makes it much easier for you to use other third-party Python libraries. + +Working with Python 3 +--------------------- + +At this point, you may also have Python 2.7 installed. + +.. code-block:: console + + $ python + +This will launch the Python 2 interpreter. + +.. code-block:: console + + $ python3 + +This will launch the Python 3 interpreter. + + +Setuptools + Pip +---------------- + +The two most crucial third-party Python packages are `setuptools `_ and `pip `_, +which let you download, install and uninstall any compliant Python software +product with a single command. It also enables you to add this network installation +capability to your own Python software with very little work. + +All supported versions of Python 3 include pip, so just make sure it's up to date:: + + python -m pip install -U pip + + +Pipenv & Virtual Environments +----------------------------- + +The next step is to install Pipenv, so you can install dependencies and manage virtual environments. + +A Virtual Environment is a tool to keep the dependencies required by different projects +in separate places, by creating virtual Python environments for them. It solves the +"Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps +your global site-packages directory clean and manageable. + +For example, you can work on a project which requires Django 1.10 while also +maintaining a project which requires Django 1.8. + +So, onward! To the :ref:`Pipenv & Virtual Environments ` docs! + +-------------------------------- + +This page is a remixed version of `another guide `_, +which is available under the same license. diff --git a/docs/starting/installation.rst b/docs/starting/installation.rst index 5df0035f3..bb84c2715 100644 --- a/docs/starting/installation.rst +++ b/docs/starting/installation.rst @@ -1,6 +1,10 @@ +.. _installation: + Properly Installing Python ========================== +.. image:: https://farm5.staticflickr.com/4303/36137232412_fdcb0f84eb_k_d.jpg + There's a good chance that you already have Python on your operating system. If so, you do not need to install or configure anything else to use Python. @@ -10,12 +14,26 @@ applications for real-world use. In particular, you should always install Setuptools, Pip, and Virtualenv — they make it much easier for you to use other third-party Python libraries. +.. note:: The use of **Python 3** is *highly* preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself *still* using Python 2 in production today. If you are using Python 3, congratulations — you are indeed a person of excellent taste. + —*Kenneth Reitz* + + Installation Guides ------------------- -These guides go over the proper installation of :ref:`Python 2.7 ` -for development purposes, as well as setuptools, pip, and virtualenv setup. +These guides go over the proper installation of :ref:`Python ` +for development purposes, as well as setuptools, pip and virtualenv. + +Python 3 Installation Guides +//////////////////////////// + +- :ref:`Python 3 on MacOS `. +- :ref:`Python 3 on Windows `. +- :ref:`Python 3 on Linux `. + +Legacy Python 2 Installation Guides +/////////////////////////////////// -- :ref:`Mac OS X `. -- :ref:`Microsoft Windows `. -- :ref:`Linux `. +- :ref:`Python 2 on MacOS `. +- :ref:`Python 2 on Microsoft Windows `. +- :ref:`Python 2 on Linux `. diff --git a/docs/starting/which-python.rst b/docs/starting/which-python.rst index 26732e34e..155eadb0c 100644 --- a/docs/starting/which-python.rst +++ b/docs/starting/which-python.rst @@ -1,57 +1,53 @@ -Picking an Interpreter -====================== +Picking an Python Interpreter (3 vs. 2) +======================================= + +.. image:: https://farm5.staticflickr.com/4265/34484834733_5b80f65ab1_k_d.jpg .. _which-python: -The State of Python (2 vs 3) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The State of Python (3 & 2) +~~~~~~~~~~~~~~~~~~~~~~~~~~~ When choosing a Python interpreter, one looming question is always present: -"Should I choose Python 2 or Python 3"? The answer is not as obvious as +"Should I choose Python 2 or Python 3"? The answer is a bit more subtle than one might think. The basic gist of the state of things is as follows: -1. Python 2.7 has been the standard for a *long* time. -2. Python 3 introduced major changes to the language, which many developers are unhappy with. -3. Python 2.7 will receive necessary security updates until 2020 [#pep373_eol]_. -4. Python 3 is continually evolving, like Python 2 did in years past. - -So, you can now see why this is not such an easy decision. - +1. Most production applications today use Python 2.7. +2. Python 3 is ready for the production deployment of applications today. +3. Python 2.7 will only receive necessary security updates until 2020 [#pep373_eol]_. +4. The brand name "Python" encapsulates both Python 3 and Python 2. Recommendations ~~~~~~~~~~~~~~~ -I'll be blunt: - - -**Use Python 3 if...** -- You don't care. -- You love Python 3. -- You are indifferent towards 2 vs 3. -- You don't know which one to use. -- You embrace change. +.. note:: The use of **Python 3** is *highly* preferred over Python 2. Consider upgrading your applications and infrastructure if you find yourself *still* using Python 2 in production today. If you are using Python 3, congratulations — you are indeed a person of excellent taste. + —*Kenneth Reitz* -**Use Python 2 if...** - -- You love Python 2 and are saddened by the future being Python 3. -- The stability requirements of your software would be improved by a language and runtime that never changes. -- Software that you depend on requires it. +I'll be blunt: +- Use Python 3 for new Python applications. +- If you're learning Python for the first time, familiarizing yourself with Python 2.7 will be very + useful, but not more useful than learning Python 3. +- Learn both. They are both "Python". +- Software that is already built often depends on Python 2.7. +- If you are writing a new open source Python library, it's best to write it for both Python 2 and 3 + simultaneously. Only supporting Python 3 for a new library you want to be widely adopted is a + political statement and will alienate many of your users. This is not a problem — slowly, over the next three years, this will become less the case. So.... 3? ~~~~~~~~~ -If you're choosing a Python interpreter to use, and aren't opinionated, then I +If you're choosing a Python interpreter to use, I recommend you use the newest Python 3.x, since every version brings new and -improved standard library modules, security and bug fixes. Progress is progress. +improved standard library modules, security and bug fixes. -Given such, only use Python 2 if you have a strong reason to, such as a Python 2 -exclusive library which has no adequate Python 3 ready alternative, or you -(like me) absolutely love and are inspired by Python 2. +Given such, only use Python 2 if you have a strong reason to, such as a +pre-existing code-base, a Python 2 exclusive library, simplicity/familiarity, +or, of course, you absolutely love and are inspired by Python 2. No harm in that. Check out `Can I Use Python 3? `_ to see if any software you're depending on will block your adoption of Python 3. @@ -62,7 +58,9 @@ It is possible to `write code that works on Python 2.6, 2.7, and Python 3 `_. This ranges from trivial to hard depending upon the kind of software you are writing; if you're a beginner there are far more important things to -worry about. +worry about. Note that Python 2.6 is end-of-life upstream, so you shouldn't +try to write 2.6-compatible code unless you're being paid specifically to +do that. Implementations ~~~~~~~~~~~~~~~ diff --git a/docs/writing/documentation.rst b/docs/writing/documentation.rst index bdd39e13e..d251a0b99 100644 --- a/docs/writing/documentation.rst +++ b/docs/writing/documentation.rst @@ -1,6 +1,8 @@ Documentation ============= +.. image:: https://farm5.staticflickr.com/4279/35620636012_f66aa88f93_k_d.jpg + Readability is a primary focus for Python developers, in both project and code documentation. Following some simple best practices can save both you and others a lot of time. @@ -205,8 +207,8 @@ more information about a function, what it does, any exceptions it may raise, what it returns, or relevant details about the parameters. For more detailed documentation of code a popular style is the one used for the -Numpy project, often called `Numpy style`_ docstrings. While it can take up a -few more lines the previous example, it allows the developer to include a lot +Numpy project, often called `Numpy style`_ docstrings. While it can take up more +lines than the previous example, it allows the developer to include a lot more information about a method, function, or class. :: def random_number_generator(arg1, arg2): diff --git a/docs/writing/gotchas.rst b/docs/writing/gotchas.rst index f8e2e2377..cfbb08a12 100644 --- a/docs/writing/gotchas.rst +++ b/docs/writing/gotchas.rst @@ -1,6 +1,8 @@ Common Gotchas ============== +.. image:: https://farm5.staticflickr.com/4163/34435688380_b5a740762b_k_d.jpg + For the most part, Python aims to be a clean and consistent language that avoids surprises. However, there are a few cases that can be confusing to newcomers. @@ -76,6 +78,7 @@ signal that no argument was provided (:py:data:`None` is often a good choice). to.append(element) return to +Do not forget, you are passing a *list* object as the second argument. When the Gotcha Isn't a Gotcha ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -229,13 +232,29 @@ Here's nice trick for removing all of these files, if they already exist:: Run that from the root directory of your project, and all ``.pyc`` files will suddenly vanish. Much better. +.. _version_control_ignores: +Version Control Ignores +~~~~~~~~~~~~~~~~~~~~~~~ +If you still need the ``.pyc`` files for performance reasons, you can always add them +to the ignore files of your version control repositories. Popular version control +systems have the ability to use wildcards defined in a file to apply special +rules. +An ignore file will make sure the matching files don't get checked into the repository. +Git_ uses ``.gitignore`` while Mercurial_ uses ``.hgignore``. +.. _Git: https://git-scm.com/ +.. _Mercurial: https://www.mercurial-scm.org/ +At the minimum your ignore files should look like this. +:: + syntax:glob # This line is not needed for .gitignore files. + *.py[cod] # Will match .pyc, .pyo and .pyd files. + __pycache__/ # Exclude the whole folder - - +You may wish to include more files and directories depending on your needs. +The next time you commit to the repository, these files will not be included. diff --git a/docs/writing/license.rst b/docs/writing/license.rst index 6c42df767..dd5fe3bb8 100644 --- a/docs/writing/license.rst +++ b/docs/writing/license.rst @@ -1,6 +1,8 @@ Choosing a License ================== +.. image:: https://farm5.staticflickr.com/4228/33907149294_82d7535a6c_k_d.jpg + Your source publication *needs* a license. In the US, if no license is specified, users have no legal right to download, modify, or distribute. Furthermore, people can't contribute to your code unless you tell them what diff --git a/docs/writing/logging.rst b/docs/writing/logging.rst index 9b41abadf..184e9e8b6 100644 --- a/docs/writing/logging.rst +++ b/docs/writing/logging.rst @@ -1,6 +1,8 @@ Logging ======= +.. image:: https://farm5.staticflickr.com/4246/35254379756_c9fe23f843_k_d.jpg + The :mod:`logging` module has been a part of Python's Standard Library since version 2.3. It is succinctly described in :pep:`282`. The documentation is notoriously hard to read, except for the `basic logging tutorial`_. @@ -55,17 +57,8 @@ this in your ``__init__.py`` .. code-block:: python - # Set default logging handler to avoid "No handler found" warnings. import logging - try: # Python 2.7+ - from logging import NullHandler - except ImportError: - class NullHandler(logging.Handler): - def emit(self, record): - pass - - logging.getLogger(__name__).addHandler(NullHandler()) - + logging.getLogger(__name__).addHandler(logging.NullHandler()) Logging in an Application diff --git a/docs/writing/reading.rst b/docs/writing/reading.rst index 97342250a..0b6486d51 100644 --- a/docs/writing/reading.rst +++ b/docs/writing/reading.rst @@ -1,6 +1,8 @@ Reading Great Code ================== +.. image:: https://farm5.staticflickr.com/4221/34689452831_93d7fd0571_k_d.jpg + One of the core tenets behind the design of Python is creating readable code. The motivation behind this design is simple: The number one thing that Python programmers do is read code. diff --git a/docs/writing/structure.rst b/docs/writing/structure.rst index a156fba7f..b0dc9cadf 100644 --- a/docs/writing/structure.rst +++ b/docs/writing/structure.rst @@ -1,6 +1,8 @@ Structuring Your Project ======================== +.. image:: https://farm5.staticflickr.com/4203/33907151224_0574e7dfc2_k_d.jpg + By "structure" we mean the decisions you make concerning how your project best meets its objective. We need to consider how to best leverage Python's features to create clean, effective code. @@ -172,6 +174,8 @@ Test Suite :::::::::: +*For advice on writing your tests, see :doc:`writing/tests`.* + .. csv-table:: :widths: 20, 40 @@ -200,7 +204,7 @@ it. You can do this a few ways: package properly. I highly recommend the latter. Requiring a developer to run -`setup.py `__ develop to test an actively changing +``setup.py develop`` to test an actively changing codebase also requires them to have an isolated environment setup for each instance of the codebase. @@ -211,7 +215,7 @@ file: import os import sys - sys.path.insert(0, os.path.abspath('..')) + sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) import sample @@ -391,7 +395,17 @@ folder named :file:`my` which is not the case. There is an dot notation should be used in the Python docs. If you'd like you could name your module :file:`my_spam.py`, but even our -friend the underscore should not be seen often in module names. +friend the underscore should not be seen often in module names. However, using other +characters (spaces or hyphens) in module names will prevent importing +(- is the subtract operator), so try to keep module names short so there is +no need to separate words. And, most of all, don't namespace with underscores, use submodules instead. + +.. code-block:: python + + # OK + import library.plugin.foo + # not OK + import library.foo_plugin Aside from some naming restrictions, nothing special is required for a Python file to be a module, but you need to understand the import mechanism in order @@ -633,7 +647,7 @@ with the class approach: class CustomOpen(object): def __init__(self, filename): - self.file = open(filename) + self.file = open(filename) def __enter__(self): return self.file @@ -762,7 +776,7 @@ compute x + 1, you have to create another integer and give it a name. my_list = [1, 2, 3] my_list[0] = 4 - print my_list # [4, 2, 3] <- The same list as changed + print my_list # [4, 2, 3] <- The same list has changed x = 6 x = x + 1 # The new x is another object @@ -785,7 +799,12 @@ its parts, it is much more efficient to accumulate the parts in a list, which is mutable, and then glue ('join') the parts together when the full string is needed. One thing to notice, however, is that list comprehensions are better and faster than constructing a list in a loop -with calls to ``append()``. +with calls to ``append()``. + +One other option is using the map function, which can 'map' a function +('str') to an iterable ('range(20)'). This results in a map object, +which you can then ('join') together just like the other examples. +The map function can be even faster than a list comprehension in some cases. **Bad** @@ -794,7 +813,7 @@ with calls to ``append()``. # create a concatenated string from 0 to 19 (e.g. "012..1819") nums = "" for n in range(20): - nums += str(n) # slow and inefficient + nums += str(n) # slow and inefficient print nums **Good** @@ -804,16 +823,24 @@ with calls to ``append()``. # create a concatenated string from 0 to 19 (e.g. "012..1819") nums = [] for n in range(20): - nums.append(str(n)) + nums.append(str(n)) print "".join(nums) # much more efficient -**Best** +**Better** .. code-block:: python # create a concatenated string from 0 to 19 (e.g. "012..1819") nums = [str(n) for n in range(20)] print "".join(nums) + +**Best** + +.. code-block:: python + + # create a concatenated string from 0 to 19 (e.g. "012..1819") + nums = map(str, range(20)) + print "".join(nums) One final thing to mention about strings is that using ``join()`` is not always best. In the instances where you are creating a new string from a pre-determined diff --git a/docs/writing/style.rst b/docs/writing/style.rst index 7454e9465..abd0af49f 100644 --- a/docs/writing/style.rst +++ b/docs/writing/style.rst @@ -3,6 +3,8 @@ Code Style ========== +.. image:: https://farm5.staticflickr.com/4223/33907150054_5ee79e8940_k_d.jpg + If you ask Python programmers what they like most about Python, they will often cite its high readability. Indeed, a high level of readability is at the heart of the design of the Python language, following the @@ -459,23 +461,23 @@ best to adhere to the guidelines laid out within this document. Some project may sway from it from time to time, while others may `amend its recommendations `_. -That being said, conforming your Python code to PEP 8 is generally a good -idea and helps make code more consistent when working on projects with other -developers. There is a command-line program, `pep8 `_, -that can check your code for conformance. Install it by running the following -command in your terminal: +That being said, conforming your Python code to PEP 8 is generally a good idea +and helps make code more consistent when working on projects with other +developers. There is a command-line program, `pycodestyle `_ +(previously known as ``pep8``), that can check your code for conformance. +Install it by running the following command in your terminal: .. code-block:: console - $ pip install pep8 + $ pip install pycodestyle Then run it on a file or series of files to get a report of any violations. .. code-block:: console - $ pep8 optparse.py + $ pycodestyle optparse.py optparse.py:69:11: E401 multiple imports on one line optparse.py:77:1: E302 expected 2 blank lines, found 1 optparse.py:88:5: E301 expected 1 blank line, found 0 diff --git a/docs/writing/tests.rst b/docs/writing/tests.rst index b8b7ca022..32c6e552e 100644 --- a/docs/writing/tests.rst +++ b/docs/writing/tests.rst @@ -1,6 +1,8 @@ Testing Your Code ================= +.. image:: https://farm5.staticflickr.com/4166/34435687940_8f73fc1fa6_k_d.jpg + Testing your code is very important. Getting used to writing testing code and running this code in parallel is now @@ -185,24 +187,41 @@ and then running the `py.test` command is far less work than would be required for the equivalent functionality with the unittest module! - `py.test `_ - + `py.test `_ -Nose ----- -nose extends unittest to make testing easier. +Hypothesis +---------- +Hypothesis is a library which lets you write tests that are parametrized by +a source of examples. It then generates simple and comprehensible examples +that make your tests fail, letting you find more bugs with less work. .. code-block:: console - $ pip install nose + $ pip install hypothesis + +For example, testing lists of floats will try many examples, but report the +minimal example of each bug (distinguished exception type and location): + +.. code-block:: python + + @given(lists(floats(allow_nan=False, allow_infinity=False), min_size=1)) + def test_mean(xs): + mean = sum(xs) / len(xs) + assert min(xs) <= mean(xs) <= max(xs) + +.. code-block:: none -nose provides automatic test discovery to save you the hassle of manually -creating test suites. It also provides numerous plugins for features such as -xUnit-compatible test output, coverage reporting, and test selection. + Falsifying example: test_mean( + xs=[1.7976321109618856e+308, 6.102390043022755e+303] + ) - `nose `_ +Hypothesis is practical as well as very powerful, and will often find bugs +that escaped all other forms of testing. It integrates well with py.test, +and has a strong focus on usability in both simple and advanced scenarios. + + `hypothesis `_ tox @@ -218,7 +237,8 @@ multiple interpreter configurations tox allows you to configure complicated multi-parameter test matrices via a simple ini-style configuration file. - `tox `_ + `tox `_ + Unittest2 ---------