Host photos locally
@@ -3,7 +3,7 @@ Hitchhiker's Guide to Python
|
||||
|
||||
**Python Best Practices Guidebook**
|
||||
|
||||
.. image:: https://farm1.staticflickr.com/628/33173824932_58add34581_k_d.jpg
|
||||
.. image:: /_static/photos/33173824932_58add34581_k_d.jpg
|
||||
|
||||
-----------
|
||||
|
||||
|
||||
|
After Width: | Height: | Size: 156 KiB |
|
After Width: | Height: | Size: 177 KiB |
|
After Width: | Height: | Size: 121 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 117 KiB |
|
After Width: | Height: | Size: 119 KiB |
|
After Width: | Height: | Size: 178 KiB |
|
After Width: | Height: | Size: 106 KiB |
|
After Width: | Height: | Size: 107 KiB |
|
After Width: | Height: | Size: 60 KiB |
|
After Width: | Height: | Size: 72 KiB |
|
After Width: | Height: | Size: 201 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 116 KiB |
|
After Width: | Height: | Size: 124 KiB |
|
After Width: | Height: | Size: 100 KiB |
|
After Width: | Height: | Size: 82 KiB |
|
After Width: | Height: | Size: 102 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 117 KiB |
|
After Width: | Height: | Size: 91 KiB |
|
After Width: | Height: | Size: 192 KiB |
|
After Width: | Height: | Size: 136 KiB |
|
After Width: | Height: | Size: 84 KiB |
|
After Width: | Height: | Size: 38 KiB |
|
After Width: | Height: | Size: 47 KiB |
|
After Width: | Height: | Size: 74 KiB |
|
After Width: | Height: | Size: 43 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 81 KiB |
|
After Width: | Height: | Size: 54 KiB |
|
After Width: | Height: | Size: 78 KiB |
|
After Width: | Height: | Size: 127 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 94 KiB |
|
After Width: | Height: | Size: 57 KiB |
|
After Width: | Height: | Size: 62 KiB |
|
After Width: | Height: | Size: 46 KiB |
|
After Width: | Height: | Size: 153 KiB |
|
After Width: | Height: | Size: 127 KiB |
|
After Width: | Height: | Size: 167 KiB |
@@ -1,7 +1,7 @@
|
||||
Your Development Environment
|
||||
============================
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2930/33175624924_7febc46cc4_k_d.jpg
|
||||
.. image:: /_static/photos/33175624924_7febc46cc4_k_d.jpg
|
||||
|
||||
|
||||
Text Editors
|
||||
@@ -164,8 +164,8 @@ MIT licensed.
|
||||
Enthought Canopy
|
||||
----------------
|
||||
`Enthought Canopy <https://www.enthought.com/products/canopy/>`_ is a Python
|
||||
IDE which is focused towards Scientists and Engineers as it provides pre
|
||||
installed libraries for data analysis.
|
||||
IDE which is focused towards Scientists and Engineers as it provides pre
|
||||
installed libraries for data analysis.
|
||||
|
||||
Eclipse
|
||||
-------
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Further Configuration of Pip and Virtualenv
|
||||
===========================================
|
||||
|
||||
.. image:: https://farm4.staticflickr.com/3934/34018732105_f0e6758859_k_d.jpg
|
||||
.. image:: /_static/photos/34018732105_f0e6758859_k_d.jpg
|
||||
|
||||
Requiring an active virtual environment for ``pip``
|
||||
---------------------------------------------------
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Pipenv & Virtual Environments
|
||||
=============================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4290/35294660055_42c02b2316_k_d.jpg
|
||||
.. image:: /_static/photos/35294660055_42c02b2316_k_d.jpg
|
||||
|
||||
This tutorial walks you through installing and using Python packages.
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
The Community
|
||||
=============
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4225/34689432801_78d97ecec9_k_d.jpg
|
||||
.. image:: /_static/photos/34689432801_78d97ecec9_k_d.jpg
|
||||
|
||||
BDFL
|
||||
----
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Documentation
|
||||
=============
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4178/33928823133_2f3d32cf32_k_d.jpg
|
||||
.. image:: /_static/photos/33928823133_2f3d32cf32_k_d.jpg
|
||||
|
||||
Official Documentation
|
||||
----------------------
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Introduction
|
||||
============
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4180/34725946825_0f85497e60_k_d.jpg
|
||||
.. image:: /_static/photos/34725946825_0f85497e60_k_d.jpg
|
||||
|
||||
From the `official Python website <http://python.org/about/>`_:
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Learning Python
|
||||
===============
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2840/32800783863_11a00db52c_k_d.jpg
|
||||
.. image:: /_static/photos/32800783863_11a00db52c_k_d.jpg
|
||||
|
||||
Beginner
|
||||
--------
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
News
|
||||
====
|
||||
|
||||
.. image:: https://farm4.staticflickr.com/3804/33573767786_eececc5d27_k_d.jpg
|
||||
.. image:: /_static/photos/33573767786_eececc5d27_k_d.jpg
|
||||
|
||||
Planet Python
|
||||
~~~~~~~~~~~~~
|
||||
@@ -35,7 +35,7 @@ A short-form Python podcast covering recent developer headlines.
|
||||
Pycoder's Weekly
|
||||
~~~~~~~~~~~~~~~~
|
||||
|
||||
Pycoder's Weekly is a free weekly Python newsletter for Python developers
|
||||
Pycoder's Weekly is a free weekly Python newsletter for Python developers
|
||||
by Python developers (Projects, Articles, News, and Jobs).
|
||||
|
||||
`Pycoder's Weekly <http://www.pycoders.com/>`_
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Contribute
|
||||
~~~~~~~~~~
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2858/33573769116_49c1ef51e7_k_d.jpg
|
||||
.. image:: /_static/photos/33573769116_49c1ef51e7_k_d.jpg
|
||||
|
||||
Python-guide is under active development, and contributors are welcome.
|
||||
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
License
|
||||
=======
|
||||
|
||||
.. image:: https://farm4.staticflickr.com/3762/32800805573_568d6b72fd_k_d.jpg
|
||||
.. image:: /_static/photos/32800805573_568d6b72fd_k_d.jpg
|
||||
|
||||
The Guide is licensed under the `Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license <https://creativecommons.org/licenses/by-nc-sa/3.0/>`_.
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
The Guide Style Guide
|
||||
=====================
|
||||
|
||||
.. image:: https://farm4.staticflickr.com/3684/33573755856_7f43d43adf_k_d.jpg
|
||||
.. image:: /_static/photos/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,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Systems Administration
|
||||
======================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4179/34435690580_3afec7d4cd_k_d.jpg
|
||||
.. image:: /_static/photos/34435690580_3afec7d4cd_k_d.jpg
|
||||
|
||||
Fabric
|
||||
------
|
||||
@@ -234,37 +234,37 @@ The Ansible playbook will ping all of the servers in the :file:`hosts.yml` file.
|
||||
You can also select groups of servers using Ansible. For more information
|
||||
about Ansible, read the `Ansible Docs <http://docs.ansible.com/>`_.
|
||||
|
||||
`An Ansible tutorial <https://serversforhackers.com/an-ansible-tutorial/>`_ is also a
|
||||
`An Ansible tutorial <https://serversforhackers.com/an-ansible-tutorial/>`_ is also a
|
||||
great and detailed introduction to getting started with Ansible.
|
||||
|
||||
|
||||
Chef
|
||||
----
|
||||
`Chef <https://www.chef.io/chef/>`_ is a systems and cloud infrastructure automation
|
||||
framework that makes it easy to deploy servers and applications to any physical,
|
||||
virtual, or cloud location. In case this is your choice for configuration management,
|
||||
you will primarily use Ruby to write your infrastructure code.
|
||||
`Chef <https://www.chef.io/chef/>`_ is a systems and cloud infrastructure automation
|
||||
framework that makes it easy to deploy servers and applications to any physical,
|
||||
virtual, or cloud location. In case this is your choice for configuration management,
|
||||
you will primarily use Ruby to write your infrastructure code.
|
||||
|
||||
Chef clients run on every server that is part of your infrastructure and these regularly
|
||||
check with your Chef server to ensure your system is always aligned and represents the
|
||||
desired state. Since each individual server has its own distinct Chef client, each server
|
||||
Chef clients run on every server that is part of your infrastructure and these regularly
|
||||
check with your Chef server to ensure your system is always aligned and represents the
|
||||
desired state. Since each individual server has its own distinct Chef client, each server
|
||||
configures itself and this distributed approach makes Chef a scalable automation platform.
|
||||
|
||||
Chef works by using custom recipes (configuration elements), implemented in cookbooks. Cookbooks, which are basically
|
||||
packages for infrastructure choices, are usually stored in your Chef server.
|
||||
Read the `Digital Ocean tutorial series
|
||||
<https://www.digitalocean.com/community/tutorials/how-to-install-a-chef-server-workstation-and-client-on-ubuntu-vps-instances>`_
|
||||
Chef works by using custom recipes (configuration elements), implemented in cookbooks. Cookbooks, which are basically
|
||||
packages for infrastructure choices, are usually stored in your Chef server.
|
||||
Read the `Digital Ocean tutorial series
|
||||
<https://www.digitalocean.com/community/tutorials/how-to-install-a-chef-server-workstation-and-client-on-ubuntu-vps-instances>`_
|
||||
on chef to learn how to create a simple Chef Server.
|
||||
|
||||
To create a simple cookbook the `knife <https://docs.chef.io/knife.html>`_ command is used:
|
||||
|
||||
.. code-block:: console
|
||||
.. code-block:: console
|
||||
|
||||
knife cookbook create cookbook_name
|
||||
|
||||
`Getting started with Chef <http://gettingstartedwithchef.com/first-steps-with-chef.html>`_
|
||||
is a good starting point for Chef Beginners and many community maintained cookbooks that can
|
||||
serve as a good reference or tweaked to serve your infrastructure configuration needs can be
|
||||
`Getting started with Chef <http://gettingstartedwithchef.com/first-steps-with-chef.html>`_
|
||||
is a good starting point for Chef Beginners and many community maintained cookbooks that can
|
||||
serve as a good reference or tweaked to serve your infrastructure configuration needs can be
|
||||
found on the `Chef Supermarket <https://supermarket.chef.io/cookbooks>`_.
|
||||
|
||||
- `Chef Documentation <https://docs.chef.io/>`_
|
||||
@@ -304,7 +304,7 @@ your Puppet modules.
|
||||
.. code-block:: console
|
||||
|
||||
$ facter operatingsystem
|
||||
Ubuntu
|
||||
Ubuntu
|
||||
|
||||
Writing Modules in Puppet is pretty straight forward. Puppet Manifests together
|
||||
form Puppet Modules. Puppet manifest end with an extension of ``.pp``.
|
||||
@@ -374,7 +374,7 @@ Buildout
|
||||
--------
|
||||
|
||||
`Buildout <http://www.buildout.org>`_ is an open source software build tool.
|
||||
Buildout is created using the Python programming language. It implements a
|
||||
Buildout is created using the Python programming language. It implements a
|
||||
principle of separation of configuration from the scripts that do the setting up.
|
||||
Buildout is primarily used to download and set up dependencies in Python eggs
|
||||
format of the software being developed or deployed. Recipes for build tasks in any
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Continuous Integration
|
||||
======================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4173/33907150594_9abba7ad0a_k_d.jpg
|
||||
.. image:: /_static/photos/33907150594_9abba7ad0a_k_d.jpg
|
||||
|
||||
.. note::
|
||||
For advice on writing your tests, see :doc:`/writing/tests`.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Command-line Applications
|
||||
=========================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4272/34435690330_11930b5987_k_d.jpg
|
||||
.. image:: /_static/photos/34435690330_11930b5987_k_d.jpg
|
||||
|
||||
Command-line applications, also referred to as
|
||||
`Console Applications <http://en.wikipedia.org/wiki/Console_application>`_,
|
||||
@@ -68,9 +68,9 @@ sub-command to do the work.
|
||||
Cement
|
||||
------
|
||||
|
||||
`Cement <http://builtoncement.com/>`_ is an advanced CLI Application Framework.
|
||||
`Cement <http://builtoncement.com/>`_ is an advanced CLI Application Framework.
|
||||
Its goal is to introduce a standard, and feature-full platform
|
||||
for both simple and complex command line applications as well
|
||||
as support rapid development needs without sacrificing quality.
|
||||
Cement is flexible, and it's use cases span from the simplicity of a micro-framework
|
||||
to the complexity of a meg-framework.
|
||||
as support rapid development needs without sacrificing quality.
|
||||
Cement is flexible, and it's use cases span from the simplicity of a micro-framework
|
||||
to the complexity of a meg-framework.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Interfacing with C/C++ Libraries
|
||||
================================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4173/34725951345_c8f5959a2e_k_d.jpg
|
||||
.. image:: /_static/photos/34725951345_c8f5959a2e_k_d.jpg
|
||||
|
||||
C Foreign Function Interface
|
||||
----------------------------
|
||||
@@ -105,20 +105,20 @@ Example: Overloading __repr__
|
||||
:linenos:
|
||||
|
||||
%include "string.i"
|
||||
|
||||
|
||||
%module myclass
|
||||
%{
|
||||
#include <string>
|
||||
#include "MyClass.h"
|
||||
%}
|
||||
|
||||
|
||||
%extend MyClass {
|
||||
std::string __repr__()
|
||||
{
|
||||
return $self->getName();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
%include "MyClass.h"
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Network Applications
|
||||
====================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4251/34364815780_bea6614025_k_d.jpg
|
||||
.. image:: /_static/photos/34364815780_bea6614025_k_d.jpg
|
||||
|
||||
HTTP
|
||||
::::
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
Cryptography
|
||||
============
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4220/33907152824_bf91078cc1_k_d.jpg
|
||||
.. image:: /_static/photos/33907152824_bf91078cc1_k_d.jpg
|
||||
|
||||
Cryptography
|
||||
------------
|
||||
|
||||
`Cryptography <https://cryptography.io/en/latest/>`_ is an actively developed
|
||||
library that provides cryptographic recipes and primitives. It supports
|
||||
library that provides cryptographic recipes and primitives. It supports
|
||||
Python 2.6-2.7, Python 3.3+ and PyPy.
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ Example
|
||||
.. code-block:: python3
|
||||
|
||||
import gpg
|
||||
|
||||
|
||||
# Encryption to public key specified in rkey.
|
||||
a_key = input("Enter the fingerprint or key ID to encrypt to: ")
|
||||
filename = input("Enter the filename to encrypt: ")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Databases
|
||||
=========
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4225/33907152464_a99fdcc8de_k_d.jpg
|
||||
.. image:: /_static/photos/33907152464_a99fdcc8de_k_d.jpg
|
||||
|
||||
DB-API
|
||||
------
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
GUI Applications
|
||||
================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4250/33907143624_cd621b535c_k_d.jpg
|
||||
.. image:: /_static/photos/33907143624_cd621b535c_k_d.jpg
|
||||
|
||||
|
||||
Alphabetical list of GUI Applications.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
Image Manipulation
|
||||
==================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4157/34575689432_3de8e9a348_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
JSON
|
||||
====
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4174/33928819683_97b5c6a184_k_d.jpg
|
||||
.. image:: /_static/photos/33928819683_97b5c6a184_k_d.jpg
|
||||
|
||||
The `json <https://docs.python.org/2/library/json.html>`_ library can parse
|
||||
JSON from strings or files. The library parses JSON into a Python dictionary
|
||||
@@ -60,7 +60,7 @@ You can start using simplejson when the json library is not available by
|
||||
importing simplejson under a different name:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
import simplejson as json
|
||||
|
||||
After importing simplejson as json, the above examples will all work as if you
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
Machine Learning
|
||||
================
|
||||
|
||||
.. image:: https://farm4.staticflickr.com/3954/34018729885_002ced9b54_k_d.jpg
|
||||
.. image:: /_static/photos/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.
|
||||
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.
|
||||
|
||||
@@ -45,7 +45,7 @@ Installation
|
||||
Through PyPI:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
pip install -U scikit-learn
|
||||
|
||||
Through conda:
|
||||
@@ -61,28 +61,28 @@ Example
|
||||
|
||||
For this example, we train a simple classifier on the `Iris dataset <http://en.wikipedia.org/wiki/Iris_flower_data_set>`_, which comes bundled in with scikit-learn.
|
||||
|
||||
The dataset takes four features of flowers: sepal length, sepal width, petal length and petal width, and classifies them into three flower species (labels): setosa, versicolor or virginica. The labels have been represented as numbers in the dataset: 0 (setosa), 1 (versicolor) and 2 (virginica).
|
||||
The dataset takes four features of flowers: sepal length, sepal width, petal length and petal width, and classifies them into three flower species (labels): setosa, versicolor or virginica. The labels have been represented as numbers in the dataset: 0 (setosa), 1 (versicolor) and 2 (virginica).
|
||||
|
||||
We shuffle the Iris dataset, and divide it into separate training and testing sets: keeping the last 10 data points for testing and rest for training. We then train the classifier on the training set, and predict on the testing set.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn.datasets import load_iris
|
||||
from sklearn import tree
|
||||
from sklearn.metrics import accuracy_score
|
||||
import numpy as np
|
||||
|
||||
#loading the iris dataset
|
||||
iris = load_iris()
|
||||
iris = load_iris()
|
||||
|
||||
x = iris.data #array of the data
|
||||
y = iris.target #array of labels (i.e answers) of each data entry
|
||||
|
||||
#getting label names i.e the three flower species
|
||||
y_names = iris.target_names
|
||||
y_names = iris.target_names
|
||||
|
||||
#taking random indices to split the dataset into train and test
|
||||
test_ids = np.random.permutation(len(x))
|
||||
test_ids = np.random.permutation(len(x))
|
||||
|
||||
#splitting data and labels into train and test
|
||||
#keeping last 10 entries for testing, rest for training
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Networking
|
||||
==========
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2892/34151833832_6bdfd930af_k_d.jpg
|
||||
.. image:: /_static/photos/34151833832_6bdfd930af_k_d.jpg
|
||||
|
||||
Twisted
|
||||
-------
|
||||
@@ -35,4 +35,4 @@ gevent
|
||||
|
||||
`gevent <http://www.gevent.org/>`_ is a coroutine-based Python networking
|
||||
library that uses greenlets to provide a high-level synchronous API on top of
|
||||
the libev event loop.
|
||||
the libev event loop.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
Scientific Applications
|
||||
=======================
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2890/33925223870_97e44f5629_k_d.jpg
|
||||
.. image:: /_static/photos/33925223870_97e44f5629_k_d.jpg
|
||||
|
||||
Context
|
||||
:::::::
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
HTML Scraping
|
||||
=============
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2900/34268661876_442428e122_k_d.jpg
|
||||
.. image:: /_static/photos/34268661876_442428e122_k_d.jpg
|
||||
|
||||
Web Scraping
|
||||
------------
|
||||
|
||||
@@ -2,41 +2,41 @@
|
||||
Data Serialization
|
||||
==================
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2927/33467946364_3e59bd376a_k_d.jpg
|
||||
.. image:: /_static/photos/33467946364_3e59bd376a_k_d.jpg
|
||||
|
||||
What is data serialization?
|
||||
---------------------------
|
||||
|
||||
Data serialization is the concept of converting structured data into a format
|
||||
that allows it to be shared or stored in such a way that its original
|
||||
structure to be recovered. In some cases, the secondary intention of data
|
||||
serialization is to minimize the size of the serialized data which then
|
||||
Data serialization is the concept of converting structured data into a format
|
||||
that allows it to be shared or stored in such a way that its original
|
||||
structure to be recovered. In some cases, the secondary intention of data
|
||||
serialization is to minimize the size of the serialized data which then
|
||||
minimizes disk space or bandwidth requirements.
|
||||
|
||||
Pickle
|
||||
------
|
||||
|
||||
The native data serialization module for Python is called `Pickle
|
||||
<https://docs.python.org/2/library/pickle.html>`_.
|
||||
The native data serialization module for Python is called `Pickle
|
||||
<https://docs.python.org/2/library/pickle.html>`_.
|
||||
|
||||
Here's an example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
|
||||
import pickle
|
||||
|
||||
|
||||
#Here's an example dict
|
||||
grades = { 'Alice': 89, 'Bob': 72, 'Charles': 87 }
|
||||
|
||||
|
||||
#Use dumps to convert the object to a serialized string
|
||||
serial_grades = pickle.dumps( grades )
|
||||
|
||||
#Use loads to de-serialize an object
|
||||
|
||||
#Use loads to de-serialize an object
|
||||
received_grades = pickle.loads( serial_grades )
|
||||
|
||||
Protobuf
|
||||
--------
|
||||
|
||||
If you're looking for a serialization module that has support in multiple
|
||||
languages, Google's `Protobuf
|
||||
<https://developers.google.com/protocol-buffers>`_ library is an option.
|
||||
If you're looking for a serialization module that has support in multiple
|
||||
languages, Google's `Protobuf
|
||||
<https://developers.google.com/protocol-buffers>`_ library is an option.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Speed
|
||||
=====
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2826/33175625804_e225b90f3e_k_d.jpg
|
||||
.. image:: /_static/photos/33175625804_e225b90f3e_k_d.jpg
|
||||
|
||||
CPython, the most commonly used implementation of Python, is slow for CPU bound
|
||||
tasks. `PyPy`_ is fast.
|
||||
@@ -234,14 +234,14 @@ Concurrent.futures
|
||||
The `concurrent.futures`_ module is a module in the standard library that
|
||||
provides a "high-level interface for asynchronously executing callables". It
|
||||
abstracts away a lot of the more complicated details about using multiple
|
||||
threads or processes for concurrency, and allows the user to focus on
|
||||
threads or processes for concurrency, and allows the user to focus on
|
||||
accomplishing the task at hand.
|
||||
|
||||
The `concurrent.futures`_ module exposes two main classes, the
|
||||
`ThreadPoolExecutor` and the `ProcessPoolExecutor`. The ThreadPoolExecutor
|
||||
will create a pool of worker threads that a user can submit jobs to. These jobs
|
||||
will then be executed in another thread when the next worker thread becomes
|
||||
available.
|
||||
available.
|
||||
|
||||
The ProcessPoolExecutor works in the same way, except instead of using multiple
|
||||
threads for its workers, it will use multiple processes. This makes it possible
|
||||
@@ -275,7 +275,7 @@ everything in parallel. :
|
||||
# Do something with the result
|
||||
print(page.text)
|
||||
|
||||
For even more control, the `submit(func, *args, **kwargs)` method will schedule
|
||||
For even more control, the `submit(func, *args, **kwargs)` method will schedule
|
||||
a callable to be executed ( as `func(*args, **kwargs)`) and returns a `Future`_
|
||||
object that represents the execution of the callable.
|
||||
|
||||
@@ -389,9 +389,9 @@ still alive (because the join call timed out):
|
||||
Because multiple threads have access to the same section of memory, sometimes
|
||||
there might be situations where two or more threads are trying to write to the
|
||||
same resource at the same time or where the output is dependent on the sequence
|
||||
or timing of certain events. This is called a `data race`_ or race condition.
|
||||
or timing of certain events. This is called a `data race`_ or race condition.
|
||||
When this happens, the output will be garbled or you may encounter problems
|
||||
which are difficult to debug. A good example is this `stackoverflow post`_.
|
||||
which are difficult to debug. A good example is this `stackoverflow post`_.
|
||||
|
||||
The way this can be avoided is by using a `Lock`_ that each thread needs to
|
||||
acquire before writing to a shared resource. Locks can be acquired and released
|
||||
@@ -412,7 +412,7 @@ through either the contextmanager protocol (`with` statement), or by using
|
||||
|
||||
def monitor_website(some_website):
|
||||
"""
|
||||
Monitor a website and then if there are any changes,
|
||||
Monitor a website and then if there are any changes,
|
||||
log them to disk.
|
||||
"""
|
||||
while True:
|
||||
@@ -429,7 +429,7 @@ Here, we have a bunch of threads checking for changes on a list of sites and
|
||||
whenever there are any changes, they attempt to write those changes to a file
|
||||
by calling `log(changes)`. When `log()` is called, it will wait to acquire
|
||||
the lock with `with file_lock:`. This ensures that at any one time, only one
|
||||
thread is writing to the file.
|
||||
thread is writing to the file.
|
||||
|
||||
Spawning Processes
|
||||
------------------
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
Web Applications & Frameworks
|
||||
=============================
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2891/34309496175_b82d104282_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
@@ -99,7 +99,7 @@ to build RESTful API microservices that are fast and scalable.
|
||||
|
||||
It is a reliable, high-performance Python web framework for building large-scale
|
||||
app backends and microservices. Falcon encourages the REST architectural style of
|
||||
mapping URIs to resources, trying to do as little as possible while remaining highly effective.
|
||||
mapping URIs to resources, trying to do as little as possible while remaining highly effective.
|
||||
|
||||
Falcon highlights four main focuses: speed, reliability, flexibility and debuggability.
|
||||
It implements HTTP through "responders" such as ``on_get()``, ``on_put()``, etc.
|
||||
@@ -120,9 +120,9 @@ Pyramid
|
||||
|
||||
`Pyramid <https://trypyramid.com/>`_ is a very flexible framework with a heavy
|
||||
focus on modularity. It comes with a small number of libraries ("batteries")
|
||||
built-in, and encourages users to extend its base functionality. A set of
|
||||
built-in, and encourages users to extend its base functionality. A set of
|
||||
provided cookiecutter templates helps making new project decisions for users.
|
||||
It poweres one of the most important parts of python infrastucture
|
||||
It poweres one of the most important parts of python infrastucture
|
||||
`PyPI <http://pypi.org/>`_.
|
||||
|
||||
Pyramid does not have a large user base, unlike Django and Flask. It's a
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
XML parsing
|
||||
===========
|
||||
|
||||
.. image:: https://farm3.staticflickr.com/2808/33888714601_a1f7d020a2_k_d.jpg
|
||||
.. image:: /_static/photos/33888714601_a1f7d020a2_k_d.jpg
|
||||
|
||||
untangle
|
||||
--------
|
||||
|
||||
@@ -4,27 +4,27 @@
|
||||
Freezing Your Code
|
||||
==================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4227/33907151034_e0a9e53402_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
"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', 'Civilization IV', and
|
||||
BitTorrent clients do this.
|
||||
|
||||
The advantage of distributing this way is that your application will "just work",
|
||||
even if the user doesn't already have the required version of Python (or any)
|
||||
even if the user doesn't already have the required version of Python (or any)
|
||||
installed. On Windows, and even on many Linux distributions and OS X, the right
|
||||
version of Python will not already be installed.
|
||||
|
||||
Besides, end-user software should always be in an executable format. Files
|
||||
ending in ``.py`` are for software engineers and system administrators.
|
||||
Besides, end-user software should always be in an executable format. Files
|
||||
ending in ``.py`` are for software engineers and system administrators.
|
||||
|
||||
One disadvantage of freezing is that it will increase the size of your
|
||||
One disadvantage of freezing is that it will increase the size of your
|
||||
distribution by about 2–12MB. Also, you will be responsible for shipping
|
||||
updated versions of your application when security vulnerabilities to
|
||||
Python are patched.
|
||||
updated versions of your application when security vulnerabilities to
|
||||
Python are patched.
|
||||
|
||||
Alternatives to Freezing
|
||||
------------------------
|
||||
@@ -75,34 +75,34 @@ Prerequisite is to install :ref:`Python, Setuptools and pywin32 dependency on Wi
|
||||
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.
|
||||
@@ -110,7 +110,7 @@ resides.
|
||||
5. Freeze!
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
|
||||
$ python bb_setup.py
|
||||
|
||||
py2exe
|
||||
@@ -204,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 <https://riverbankcomputing.com/software/pyqt/intro>`_ or `PySide <http://wiki.qt.io/About-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 <http://pythonhosted.org/PyInstaller/spec-files.html#spec-file-options-for-a-mac-os-x-bundle>`_. 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 <http://pythonhosted.org/PyInstaller/spec-files.html#spec-file-options-for-a-mac-os-x-bundle>`_. For example, to specify an icon for the app, use the :code:`icon=\path\to\icon.icns` option.
|
||||
|
||||
|
||||
Linux
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
Packaging Your Code
|
||||
===================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4325/36137234682_be6898bf57_k_d.jpg
|
||||
.. image:: /_static/photos/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,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Installing Python 2 on Linux
|
||||
=============================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4268/34435688560_4cc2a7bcbb_k_d.jpg
|
||||
.. image:: /_static/photos/34435688560_4cc2a7bcbb_k_d.jpg
|
||||
|
||||
.. note::
|
||||
Check out our :ref:`guide for installing Python 3 on Linux<install3-linux>`.
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Installing Python 2 on Mac OS X
|
||||
===============================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4268/34435688560_4cc2a7bcbb_k_d.jpg
|
||||
.. image:: /_static/photos/34435688560_4cc2a7bcbb_k_d.jpg
|
||||
|
||||
.. note::
|
||||
Check out our :ref:`guide for installing Python 3 on OS X<install3-osx>`.
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Installing Python 2 on Windows
|
||||
==============================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4268/34435688560_4cc2a7bcbb_k_d.jpg
|
||||
.. image:: /_static/photos/34435688560_4cc2a7bcbb_k_d.jpg
|
||||
|
||||
.. note::
|
||||
Check out our :ref:`guide for installing Python 3 on Windows<install3-windows>`.
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Installing Python 3 on Linux
|
||||
============================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4276/34435689480_2e6f358510_k_d.jpg
|
||||
.. image:: /_static/photos/34435689480_2e6f358510_k_d.jpg
|
||||
|
||||
This document describes how to install Python 3.6 on Ubuntu Linux machines.
|
||||
|
||||
@@ -93,7 +93,7 @@ care about that.
|
||||
Pipenv & Virtual Environments
|
||||
-----------------------------
|
||||
|
||||
The next step is to install Pipenv, so you can install dependencies and manage 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
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
Installing Python 3 on Mac OS X
|
||||
===============================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4276/34435689480_2e6f358510_k_d.jpg
|
||||
.. image:: /_static/photos/34435689480_2e6f358510_k_d.jpg
|
||||
|
||||
The latest version of Mac OS X, High Sierra, **comes with Python 2.7 out of the box**.
|
||||
|
||||
@@ -106,13 +106,13 @@ The rest of the guide will assume that ``python`` references Python 3.
|
||||
|
||||
# Do I have a Python 3 installed?
|
||||
$ python --version
|
||||
Python 3.6.4 # Success!
|
||||
Python 3.6.4 # Success!
|
||||
# If you still see 2.7 ensure in PATH /usr/local/bin/ takes precedence over /usr/bin/
|
||||
|
||||
Pipenv & Virtual Environments
|
||||
-----------------------------
|
||||
|
||||
The next step is to install Pipenv, so you can install dependencies and manage 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
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Installing Python 3 on Windows
|
||||
==============================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4276/34435689480_2e6f358510_k_d.jpg
|
||||
.. image:: /_static/photos/34435689480_2e6f358510_k_d.jpg
|
||||
|
||||
First, follow the installation instructions for `Chocolatey <https://chocolatey.org/install>`_.
|
||||
It's a community system packager manager for Windows 7+. (It's very much like Homebrew on OSX.)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Properly Installing Python
|
||||
==========================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4303/36137232412_fdcb0f84eb_k_d.jpg
|
||||
.. image:: /_static/photos/36137232412_fdcb0f84eb_k_d.jpg
|
||||
|
||||
There's a good chance that you already have Python on your operating system.
|
||||
|
||||
@@ -16,8 +16,8 @@ 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
|
||||
-------------------
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Picking an Python Interpreter (3 vs. 2)
|
||||
=======================================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4265/34484834733_5b80f65ab1_k_d.jpg
|
||||
.. image:: /_static/photos/34484834733_5b80f65ab1_k_d.jpg
|
||||
|
||||
.. _which-python:
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Documentation
|
||||
=============
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4279/35620636012_f66aa88f93_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
@@ -67,10 +67,10 @@ There is also **great**, **free** hosting for your Sphinx_ docs:
|
||||
your source repository so that rebuilding your documentation will
|
||||
happen automatically.
|
||||
|
||||
When run, Sphinx_ will import your code and using Python's introspection
|
||||
When run, Sphinx_ will import your code and using Python's introspection
|
||||
features it will extract all function, method and class signatures. It will
|
||||
also extract the accompanying docstrings, and compile it all into well
|
||||
structured and easily readable documentation for your project.
|
||||
structured and easily readable documentation for your project.
|
||||
|
||||
.. note::
|
||||
|
||||
@@ -146,7 +146,7 @@ their source code, and as a side effect, it also ensures that their code is
|
||||
tested and works.
|
||||
|
||||
::
|
||||
|
||||
|
||||
def my_function(a, b):
|
||||
"""
|
||||
>>> my_function(2, 3)
|
||||
@@ -175,14 +175,14 @@ comment block is a programmer's note. The docstring describes the
|
||||
Unlike block comments, docstrings are built into the Python language itself.
|
||||
This means you can use all of Python's powerful introspection capabilities to
|
||||
access docstrings at runtime, compared with comments which are optimised out.
|
||||
Docstrings are accessible from both the `__doc__` dunder attribute for almost
|
||||
Docstrings are accessible from both the `__doc__` dunder attribute for almost
|
||||
every Python object, as well as with the built in `help()` function.
|
||||
|
||||
While block comments are usually used to explain *what* a section of code is
|
||||
doing, or the specifics of an algorithm, docstrings are more intended for
|
||||
explaining to other users of your code (or you in 6 months time) *how* a
|
||||
particular function can be used and the general purpose of a function, class,
|
||||
or module.
|
||||
particular function can be used and the general purpose of a function, class,
|
||||
or module.
|
||||
|
||||
Writing Docstrings
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
@@ -196,19 +196,19 @@ really obvious cases, such as::
|
||||
return a + b
|
||||
|
||||
The docstring should describe the function in a way that is easy to understand.
|
||||
For simple cases like trivial functions and classes, simply embedding the
|
||||
function's signature (i.e. `add(a, b) -> result`) in the docstring is
|
||||
unnecessary. This is because with Python's `inspect` module, it is already
|
||||
For simple cases like trivial functions and classes, simply embedding the
|
||||
function's signature (i.e. `add(a, b) -> result`) in the docstring is
|
||||
unnecessary. This is because with Python's `inspect` module, it is already
|
||||
quite easy to find this information if needed, and it is also readily available
|
||||
by reading the source code.
|
||||
by reading the source code.
|
||||
|
||||
In larger or more complex projects however, it is often a good idea to give
|
||||
more information about a function, what it does, any exceptions it may raise,
|
||||
In larger or more complex projects however, it is often a good idea to give
|
||||
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 more
|
||||
lines than the previous example, it allows the developer to include a lot
|
||||
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):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Common Gotchas
|
||||
==============
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4163/34435688380_b5a740762b_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Choosing a License
|
||||
==================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4228/33907149294_82d7535a6c_k_d.jpg
|
||||
.. image:: /_static/photos/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.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Logging
|
||||
=======
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4246/35254379756_c9fe23f843_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
@@ -37,14 +37,14 @@ Other reasons why logging is better than ``print``:
|
||||
Logging in a Library
|
||||
--------------------
|
||||
|
||||
Notes for `configuring logging for a library`_ are in the
|
||||
Notes for `configuring logging for a library`_ are in the
|
||||
`logging tutorial`_. Because the *user*, not the library, should
|
||||
dictate what happens when a logging event occurs, one admonition bears
|
||||
repeating:
|
||||
|
||||
.. note::
|
||||
It is strongly advised that you do not add any handlers other than
|
||||
NullHandler to your library’s loggers.
|
||||
NullHandler to your library’s loggers.
|
||||
|
||||
|
||||
Best practice when instantiating loggers in a library is to only create them
|
||||
@@ -100,23 +100,23 @@ section of the `logging tutorial`_.
|
||||
|
||||
[loggers]
|
||||
keys=root
|
||||
|
||||
|
||||
[handlers]
|
||||
keys=stream_handler
|
||||
|
||||
|
||||
[formatters]
|
||||
keys=formatter
|
||||
|
||||
|
||||
[logger_root]
|
||||
level=DEBUG
|
||||
handlers=stream_handler
|
||||
|
||||
|
||||
[handler_stream_handler]
|
||||
class=StreamHandler
|
||||
level=DEBUG
|
||||
formatter=formatter
|
||||
args=(sys.stderr,)
|
||||
|
||||
|
||||
[formatter_formatter]
|
||||
format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s
|
||||
|
||||
@@ -131,7 +131,7 @@ Then use :meth:`logging.config.fileConfig` in the code:
|
||||
fileConfig('logging_config.ini')
|
||||
logger = logging.getLogger()
|
||||
logger.debug('often makes a very good meal of %s', 'visiting tourists')
|
||||
|
||||
|
||||
|
||||
Example Configuration via a Dictionary
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Reading Great Code
|
||||
==================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4221/34689452831_93d7fd0571_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Structuring Your Project
|
||||
========================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4203/33907151224_0574e7dfc2_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
@@ -395,9 +395,9 @@ 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. 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
|
||||
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
|
||||
@@ -799,7 +799,7 @@ 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,
|
||||
@@ -833,14 +833,14 @@ The map function can be even faster than a list comprehension in some cases.
|
||||
# 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)
|
||||
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
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
Code Style
|
||||
==========
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4223/33907150054_5ee79e8940_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
Testing Your Code
|
||||
=================
|
||||
|
||||
.. image:: https://farm5.staticflickr.com/4166/34435687940_8f73fc1fa6_k_d.jpg
|
||||
.. image:: /_static/photos/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
|
||||
Getting used to writing testing code and running this code in parallel is now
|
||||
considered a good habit. Used wisely, this method helps you define more
|
||||
precisely your code's intent and have a more decoupled architecture.
|
||||
|
||||
@@ -29,7 +29,7 @@ Some general rules of testing:
|
||||
tests as often as needed.
|
||||
|
||||
- Learn your tools and learn how to run a single test or a test case. Then,
|
||||
when developing a function inside a module, run this function's tests
|
||||
when developing a function inside a module, run this function's tests
|
||||
frequently, ideally automatically when you save the code.
|
||||
|
||||
- Always run the full test suite before a coding session, and run it again
|
||||
@@ -65,10 +65,10 @@ Some general rules of testing:
|
||||
|
||||
- Another use of the testing code is as an introduction to new developers. When
|
||||
someone will have to work on the code base, running and reading the related
|
||||
testing code is often the best thing that they can do to start. They will
|
||||
or should discover the hot spots, where most difficulties arise, and the
|
||||
corner cases. If they have to add some functionality, the first step should
|
||||
be to add a test to ensure that the new functionality is not already a
|
||||
testing code is often the best thing that they can do to start. They will
|
||||
or should discover the hot spots, where most difficulties arise, and the
|
||||
corner cases. If they have to add some functionality, the first step should
|
||||
be to add a test to ensure that the new functionality is not already a
|
||||
working path that has not been plugged into the interface.
|
||||
|
||||
The Basics
|
||||
|
||||