GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.coderwall.com — Python Good Practices (Example) Python Good PracticesVirtual EnvironmentsA virtual environment is a private copy of the Python interpreter, onto which you can install packages privately, without affecting the global Python interpreter installed in your system. The more projects you have, the more likely it is that you will be working with different versions of Python itself, or at least different versions of Python libraries. For this reason, we use virtual environments. For instance, you can always check your environment configuration with:
VirtualenvInstall virtualev: Create a virtual environment:
To begin using the virtual environment, it needs to be activated:
If you are done working in the virtual environment for the moment, you can deactivate it:
To delete a virtual environment, just delete its folder. VirtualenvwrapperVirtualenvwrapper provides a set of commands and also places all your virtual environments in one place. To install it, make sure virtualenv is already installed, then:
Add this to your .bashrc file:
To create a virtual environment:
To check if it is working:
To work on a virtual environment:
To deactivate the environment:
To delete it:
Why?Because now we can have a file with all the requirements of your project, and install it anywhere you setup your work environment with:
Debuggingpdb, the Python debugger:An introduction to pdb can be found here. For example, you can use the option -i to run a script, and then it goes to an interactive session with the Python interpreter:
Another way of debugging is by setting traces inside your code. For instance, you can write the following snippet inside the function that you want to inspect:
When running from the console, some useful commands are:
Unit TestsUnittest
Run with:
ScriptingYou can add a custom command to a script in your app, such as manage.py:
To run:
pytestTo use pytest, you just need to include a function that starts with test_ in a file that starts with test_. More information here. You install with:
For example, create a file with:
And run with:
You can also drop this to pdb:
DoctestThe test is run on everything in docstrings:
nosetestNose finds all the files that start with test and run for you. More information here.
CoverageCode coverage tools measure how much of the application is exercised by unit tests
To run from the command line:
Finally, you can create a HTML coverage version:
ScriptingWe can integrate the coverage metrics to a manage.py file in our application. For example:
To run:
Unicode and StyleAll strings are Unicode in Python 3. However, in Python 2, there are lots of problems with UTF-8 and ASCII conversion. You can use codecs module:
An example of code before:
Now, using codecs:
Finally, you should know that the convection used when you write a code in Python are deffined by PEP 8 (Python Enhancement Proposal 8). To fix style issues, you can use pep8ify. -- InterpretersNow, let us talk about the interpreter: the command line environment to directly run code. The first one is the REPL (Read-Evaluate-Print Loop), which comes with the Python installation. You use it simply typing:
A second option is IPython, which allows you to make notebooks suitable for the web. Finally, you can use bpython, which has a great GUI integrated with Python's documentation. Further ReferencesWritten by bt3glRelated protips
1 Response
Add your response
Have a fresh tip? Share with Coderwall community!Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |