mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 14:50:19 +00:00
docs/**: Specify language for code-blocks (syntax highlighting! mmm…)
This commit is contained in:
@@ -21,7 +21,7 @@ latter will ssh into each server, cd to our project directory, activate the
|
||||
virtual environment, pull the newest codebase, and restart the application
|
||||
server.
|
||||
|
||||
::
|
||||
.. code-block:: python
|
||||
|
||||
from fabric.api import cd, env, prefix, run, task
|
||||
|
||||
|
||||
@@ -20,9 +20,9 @@ SQLAlchemy
|
||||
Unlike many database libraries it not only provides an ORM layer but also a
|
||||
generalized API for writing database-agnostic code without SQL.
|
||||
|
||||
::
|
||||
.. code-block:: console
|
||||
|
||||
pip install sqlalchemy
|
||||
$ pip install sqlalchemy
|
||||
|
||||
Django ORM
|
||||
----------
|
||||
|
||||
@@ -23,6 +23,6 @@ the instructions for your platform `here <https://pypi.python.org/pypi/Pillow/2.
|
||||
|
||||
After that, it's straightforward:
|
||||
|
||||
.. code-block:: bash
|
||||
.. code-block:: console
|
||||
|
||||
$ pip install Pillow
|
||||
|
||||
@@ -56,7 +56,7 @@ After a quick analysis, we see that in our page the data is contained in
|
||||
two elements - one is a div with title 'buyer-name' and the other is a
|
||||
span with class 'item-price':
|
||||
|
||||
::
|
||||
.. code-block:: html
|
||||
|
||||
<div title="buyer-name">Carson Busses</div>
|
||||
<span class="item-price">$29.95</span>
|
||||
|
||||
+17
-12
@@ -8,9 +8,9 @@ Using a slightly modified version of `David Beazleys`_ CPU bound test code
|
||||
(added loop for multiple tests), you can see the difference between CPython
|
||||
and PyPy's processing.
|
||||
|
||||
::
|
||||
.. code-block:: console
|
||||
|
||||
PyPy
|
||||
# PyPy
|
||||
$ ./pypy -V
|
||||
Python 2.7.1 (7773f8fc4223, Nov 18 2011, 18:47:10)
|
||||
[PyPy 1.7.0 with GCC 4.4.3]
|
||||
@@ -21,9 +21,9 @@ and PyPy's processing.
|
||||
0.0440690517426
|
||||
0.0695300102234
|
||||
|
||||
::
|
||||
.. code-block:: console
|
||||
|
||||
CPython
|
||||
# CPython
|
||||
$ ./python -V
|
||||
Python 2.7.1
|
||||
$ ./python measure2.py
|
||||
@@ -72,9 +72,10 @@ Cython
|
||||
with which you are able to write C and C++ modules for Python. Cython also
|
||||
allows you to call functions from compiled C libraries. Using Cython allows
|
||||
you to take advantage of Python's strong typing of variables and operations.
|
||||
Here is an example of strong typing with Cython:
|
||||
|
||||
.. code-block:: python
|
||||
Here's an example of strong typing with Cython:
|
||||
|
||||
.. code-block:: cython
|
||||
|
||||
def primes(int kmax):
|
||||
"""Calculation of prime numbers with additional
|
||||
@@ -128,7 +129,7 @@ Notice that in the Cython version you declare integers and integer arrays for
|
||||
to be compiled into C types while also creating a Python list:
|
||||
|
||||
|
||||
.. code-block:: python
|
||||
.. code-block:: cython
|
||||
|
||||
def primes(int kmax):
|
||||
"""Calculation of prime numbers with additional
|
||||
@@ -190,18 +191,22 @@ The `pyximport` module allows you to import `pyx` files (e.g., `primesCy.pyx`) w
|
||||
The `pyximport.install()` command allows the Python interpreter to start the Cython compiler directly to generate C-code,
|
||||
which is automatically compiled to a `*.so` C-library. Cython is able to import this library for you in your Python-code.
|
||||
Very easy and very efficient. With the `time.time()` function you are able to compare the time between this 2 different calls to find 500 prime numbers.
|
||||
On a standard notebook (dual core AMD E-450 1.6 GHz), the measured values are:
|
||||
|
||||
On a standard notebook (dualcore AMD E-450 1,6 GHz) the measured values are:
|
||||
.. code-block:: console
|
||||
|
||||
Cython time: 0.0054 seconds
|
||||
|
||||
Python time: 0.0566 seconds
|
||||
|
||||
Cython time: 0.0054 seconds
|
||||
|
||||
Python time: 0.0566 seconds
|
||||
|
||||
And here the output of an embedded `ARM beaglebone <http://beagleboard.org/Products/BeagleBone>`_ machine:
|
||||
.. code-block:: console
|
||||
|
||||
Cython time: 0.0196 seconds
|
||||
Cython time: 0.0196 seconds
|
||||
|
||||
Python time: 0.3302 seconds
|
||||
Python time: 0.3302 seconds
|
||||
|
||||
Pyrex
|
||||
-----
|
||||
|
||||
@@ -59,6 +59,8 @@ Prerequisite is to install :ref:`Python on Windows <install-windows>`.
|
||||
|
||||
2. Write setup.py (`List of configuration options <http://www.py2exe.org/index.cgi/ListOfOptions>`_)::
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from distutils.core import setup
|
||||
import py2exe
|
||||
|
||||
@@ -70,7 +72,9 @@ Prerequisite is to install :ref:`Python on Windows <install-windows>`.
|
||||
|
||||
4. (Optionally) `one-file mode <http://stackoverflow.com/questions/112698/py2exe-generate-single-executable-file#113014>`_
|
||||
|
||||
5. Generate `.exe` into `dist` directory::
|
||||
5. Generate ``.exe`` into ``dist`` directory:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ python setup.py py2exe
|
||||
|
||||
|
||||
@@ -53,7 +53,9 @@ line at the bottom of your ``~/.bashrc`` file
|
||||
|
||||
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
|
||||
|
||||
Now, we can install Python 2.7: ::
|
||||
Now, we can install Python 2.7:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ brew install python
|
||||
|
||||
|
||||
@@ -25,9 +25,13 @@ tedious, so add the directories for your default Python version to the PATH.
|
||||
Assuming that your Python installation is in ``C:\Python27\``, add this to your
|
||||
PATH::
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
C:\Python27\;C:\Python27\Scripts\
|
||||
|
||||
You can do this easily by running the following in ``powershell``::
|
||||
You can do this easily by running the following in ``powershell``:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User")
|
||||
|
||||
|
||||
@@ -100,7 +100,9 @@ code easier to understand. In Python, comments begin with a hash
|
||||
|
||||
.. _docstring-ref:
|
||||
|
||||
In Python, *docstrings* describe modules, classes, and functions: ::
|
||||
In Python, *docstrings* describe modules, classes, and functions:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
def square_and_rooter(x):
|
||||
"""Returns the square root of self times self."""
|
||||
@@ -130,7 +132,9 @@ Docstrings versus Block comments
|
||||
|
||||
These aren't interchangeable. For a function or class, the leading
|
||||
comment block is a programmer's note. The docstring describes the
|
||||
operation of the function or class: ::
|
||||
*operation* of the function or class:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
# This function slows down program execution for some reason.
|
||||
def square_and_rooter(x):
|
||||
|
||||
+10
-5
@@ -336,7 +336,9 @@ Instead, use a list comprehension:
|
||||
four_lists = [[] for __ in xrange(4)]
|
||||
|
||||
|
||||
A common idiom for creating strings is to use :py:meth:`str.join` on an empty string.::
|
||||
A common idiom for creating strings is to use :py:meth:`str.join` on an empty string.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
letters = ['s', 'p', 'a', 'm']
|
||||
word = ''.join(letters)
|
||||
@@ -345,7 +347,9 @@ This will set the value of the variable *word* to 'spam'. This idiom can be appl
|
||||
|
||||
Sometimes we need to search through a collection of things. Let's look at two options: lists and dictionaries.
|
||||
|
||||
Take the following code for example::
|
||||
Take the following code for example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
d = {'s': [], 'p': [], 'a': [], 'm': []}
|
||||
l = ['s', 'p', 'a', 'm']
|
||||
@@ -365,7 +369,7 @@ Zen of Python
|
||||
|
||||
Also known as :pep:`20`, the guiding principles for Python's design.
|
||||
|
||||
::
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> import this
|
||||
The Zen of Python, by Tim Peters
|
||||
@@ -406,14 +410,15 @@ exists a command-line program, `pep8 <https://github.com/jcrocholl/pep8>`_,
|
||||
that can check your code for conformance. Install it by running the following
|
||||
command in your Terminal:
|
||||
|
||||
::
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ pip install pep8
|
||||
|
||||
|
||||
Then run it on a file or series of files to get a report of any violations.
|
||||
|
||||
::
|
||||
.. code-block:: console
|
||||
|
||||
$ pep8 optparse.py
|
||||
optparse.py:69:11: E401 multiple imports on one line
|
||||
|
||||
@@ -81,7 +81,7 @@ series of tools.
|
||||
|
||||
Creating testcases is accomplished by subclassing a TestCase base class
|
||||
|
||||
::
|
||||
.. code-block:: python
|
||||
|
||||
import unittest
|
||||
|
||||
@@ -148,7 +148,7 @@ py.test is a no-boilerplate alternative to Python's standard unittest module.
|
||||
|
||||
Despite being a fully-featured and extensible test tool, it boasts a simple
|
||||
syntax. Creating a test suite is as easy as writing a module with a couple of
|
||||
functions
|
||||
functions:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@@ -251,9 +251,10 @@ the need to change any other code.
|
||||
mock
|
||||
----
|
||||
|
||||
mock is a library for testing in Python. Starting with Python 3.3, it is
|
||||
available in the `standard library <http://docs.python.org/dev/library/unittest.mock`_. For older versions of
|
||||
python, simply:
|
||||
**``mock``** is a library for testing in Python. As of Python 3.3, it is
|
||||
available in the `standard library <http://docs.python.org/dev/library/unittest.mock`_.
|
||||
|
||||
For older versions of Python:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
@@ -262,7 +263,7 @@ python, simply:
|
||||
It allows you to replace parts of your system under test with mock objects and
|
||||
make assertions about how they have been used.
|
||||
|
||||
For example, you can monkey patch a method
|
||||
For example, you can monkey-patch a method:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
Reference in New Issue
Block a user