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).

HOWTO Fetch Internet Resources Using urllib2

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. These are provided by objects called handlers and openers. 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. HOWTO Fetch Internet Resources Using The urllib Package — Python 3.3.4 documentation. Introduction.

HOWTO Fetch Internet Resources Using The urllib Package — Python 3.3.4 documentation

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.

Advanced Python Scheduler — APScheduler 2.1.2 documentation

All APScheduler options given in the global configuration must begin with “apscheduler.” to avoid name clashes with other software. The constructor also takes options as keyword arguments (without the prefix). 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:

Iterator and generator

Introduction — virtualenv 1.11.6 documentation. Virtualenv is a tool to create isolated Python environments.

Introduction — virtualenv 1.11.6 documentation

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? Virtualenvwrapper 4.3.15.g866ef2e — virtualenvwrapper 4.3.15.g866ef2e documentation. Virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool.

virtualenvwrapper 4.3.15.g866ef2e — virtualenvwrapper 4.3.15.g866ef2e documentation

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.

Virtual Environments

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. 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.

7.2. re — Regular expression operations

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'. Getattr with arbitrary depth. 5. Expressions. This chapter explains the meaning of the elements of expressions in Python.

5. Expressions

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. 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.