background preloader

Python snppets doc

Facebook Twitter

Django - Using Python's os.path, how do I go up one directory? Sha256/python-var-dump. HOWTO Fetch Internet Resources Using urllib2. Introduction urllib2 is a Python module for fetching URLs (Uniform Resource Locators). It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols.

It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on. Urllib2 supports fetching URLs for many “URL schemes” (identified by the string before the ”:” in URL - for example “ftp” is the URL scheme of “ using their associated network protocols (e.g. For straightforward situations urlopen is very easy to use. Fetching URLs The simplest way to use urllib2 is as follows: import urllib2response = urllib2.urlopen(' = response.read() Many uses of urllib2 will be that simple (note that instead of an ‘http:’ URL we could have used an URL starting with ‘ftp:’, ‘file:’, etc.). Req = urllib2.Request(' Data This is done as follows: e.g. HOWTO Fetch Internet Resources Using The urllib Package — Python 3.3.4 documentation. Introduction urllib.request is a Python module for fetching URLs (Uniform Resource Locators).

It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols. It also offers a slightly more complex interface for handling common situations - like basic authentication, cookies, proxies and so on. Urllib.request supports fetching URLs for many “URL schemes” (identified by the string before the ”:” in URL - for example “ftp” is the URL scheme of “ using their associated network protocols (e.g. For straightforward situations urlopen is very easy to use. Fetching URLs The simplest way to use urllib.request is as follows: import urllib.requestresponse = urllib.request.urlopen(' = response.read() If you wish to retrieve a resource via URL and store it in a temporary location, you can do so via the urlretrieve() function: req = urllib.request.Request(' Data. Advanced Python Scheduler — APScheduler 2.1.2 documentation. Starting the scheduler To start the scheduler with default settings: from apscheduler.scheduler import Scheduler sched = Scheduler()sched.start() The constructor takes as its first, optional parameter a dictionary of “global” options to facilitate configuration from .ini files.

All APScheduler options given in the global configuration must begin with “apscheduler.” to avoid name clashes with other software. You can also configure the scheduler after its instantiation, if necessary. From apscheduler.scheduler import Scheduler sched = Scheduler() @sched.interval_schedule(hours=3)def some_job(): print "Decorated job" sched.configure(options_from_ini_file)sched.start() Scheduling jobs The simplest way to schedule jobs using the built-in triggers is to use one of the shortcut methods provided by the scheduler: These shortcuts cover the vast majority of use cases. When a scheduled job is triggered, it is handed over to the thread pool for execution. Warning Job options Shutting down the scheduler and:

Iterator and generator

Introduction — virtualenv 1.11.6 documentation. Virtualenv is a tool to create isolated Python environments. The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded. Or more generally, what if you want to install an application and leave it be? Also, what if you can’t install packages into the global site-packages directory? In all these cases, virtualenv can help you. Warning We advise installing virtualenv-1.9 or greater. When using pip to install virtualenv, we advise using pip 1.3 or greater.

To install globally with pip (if you have pip 1.3 or greater installed globally): $ [sudo] pip install virtualenv Note Windows Notes¶ Virtualenvwrapper 4.3.15.g866ef2e — virtualenvwrapper 4.3.15.g866ef2e documentation. Virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

Features¶ Organizes all of your virtual environments in one place.Wrappers for managing your virtual environments (create, delete, copy).Use a single command to switch between environments.Tab completion for commands that take a virtual environment as argument.User-configurable hooks for all operations (see Per-User Customization).Plugin system for more creating sharable extensions (see Extending Virtualenvwrapper). Introduction¶ The best way to explain the features virtualenvwrapper gives you is to show it in use. First, some initialization steps. Now we can install some software into the environment. We can see the new package with lssitepackages: Support¶ Shell Aliases¶ License¶ Virtual Environments. 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.

Basic Usage Create a virtual environment for a project: $ cd my_project_folder $ virtualenv my_project 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 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 my_project. You can also use the Python interpreter of your choice (like python2.7). $ virtualenv -p /usr/bin/python2.7 my_project or change the interpreter globally with an env variable in ~/.bashrc: $ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7.

Hidden Scanner functionality in re module. 7.2. re — Regular expression operations. This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings as well as 8-bit strings. Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal. The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. 7.2.1.

Regular Expression Syntax (Dot.) (Caret.) Getattr with arbitrary depth. 5. Expressions. This chapter explains the meaning of the elements of expressions in Python. Syntax Notes: In this and the following chapters, extended BNF notation will be used to describe syntax, not lexical analysis. When (one alternative of) a syntax rule has the form name ::= othername and no semantics are given, the semantics of this form of name are the same as for othername. 5.1.

Arithmetic conversions When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type,” the arguments are coerced using the coercion rules listed at Coercion rules. If either argument is a complex number, the other is converted to complex;otherwise, if either argument is a floating point number, the other is converted to floating point;otherwise, if either argument is a long integer, the other is converted to long integer;otherwise, both must be plain integers and no conversion is necessary. 5.2. Atoms are the most basic elements of expressions. 5.2.1. 5.2.2. 5.2.3.