Recent Notes

Displaying keyword search results 1 - 10
Created by Dr. Xi on October 01, 2007 03:26:46    Last update: August 25, 2011 08:57:40
Use the sub function in the re module to do global replacement: import re re.sub(pattern, replacement, inpu...
Created by Dr. Xi on April 19, 2011 16:01:39    Last update: April 19, 2011 16:01:39
This note relates to Python 2.x. A Python class is old-style by default, unless it has another new style class or the "top level" class object as its parent. The sure way to tell that an object is an instance of a new style class is to use the function type , type(x) returns <type 'instance'> for an old-style class, but it returns <class 'ClassType.X'> for a new-style class. Class definition: class A: # old style class def __init__(sel... Test session: >>> A <class ClassType.A at 0x7f36ae442fb0> ...
Created by Dr. Xi on April 18, 2011 12:10:37    Last update: April 18, 2011 12:14:24
Python defines a long list of special methods to customize class behavior. This is a short list for the Basic Customizations . Method Description object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[,...]) Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__()...
Created by Dr. Xi on December 24, 2009 22:25:38    Last update: April 04, 2011 13:48:24
Use the urlparse module to parse a URL into parts. The urlparse function parses a URL into six components, returning a 6-tuple. This corresponds to the general structure of a URL: scheme://netloc/path;parameters?query#fragment >>> from urlparse import urlparse >>> parts = u...
Created by Dr. Xi on September 10, 2010 22:53:42    Last update: September 10, 2010 22:54:06
The sort operation for a Python list sorts a list in place . It takes three optional arguments to control the comparisons: s.sort([cmp[, key[, reverse]]]) cmp specifies a custom comparison function of two arguments (list items) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()) . The default value is None . key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower . The default value is None . reverse is a boolean value. If set to True , then the list elements are sorted as if each comparison were reversed. Python 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC...
Created by Dr. Xi on September 10, 2010 16:30:16    Last update: September 10, 2010 16:30:16
Perl uses the pop function to remove an item from the end of an array, and the shift function to remove an item from the beginning of an array. Python does not have a shift function, but its pop function takes a optional parameter. To remove an item from the beginning of a list , simply pop(0) . >>> a = [] >>> a.append(1) >>> a.append(2) ...
Created by Dr. Xi on November 15, 2009 21:56:19    Last update: November 15, 2009 21:57:59
Python doesn't seem to have a built-in function to find an object in a list that satisfies a specified criterion, for example, making a function f to return True . To find jack among people : jack = None for p in people: if p.first_... This is not a lot of code, but it's not terse enough as you would expect of Python. Some suggestions are offered here: http://dev.ionous.net/2009/01/python-find-item-in-list.html http://tomayko.com/writings/cleanest-python-find-in-list-function
Created by Dr. Xi on June 06, 2009 18:31:44    Last update: June 06, 2009 19:01:57
You can use the system call from the os module to execute an external program: >>> import os >>> os.system(the_command_line_st... However, the path to the executable contains a space character, the system call treats the strings after the first space as arguments, causing an error. Python doc recommends the use of the subprocess module: The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. For example, using wget to get the google home page: >>> from subprocess import Popen, PIPE >>> (out...
Created by Dr. Xi on May 31, 2009 05:16:45    Last update: May 31, 2009 05:16:45
Use the reload function to reload a Python module at runtime: >>> from mytest import greeter >>> greeter.say_...
Created by Dr. Xi on May 02, 2009 21:25:35    Last update: May 02, 2009 22:07:40
Use the built-in dict function: >>> dict([(1,2), (3,4), (5,6)]) {1: 2, 3: 4, 5:... Or, if you have a list with even number of elements and want to use the odd numbered elements for key and even numbered elements for value: >>> a = [ 'a', 1, 'b', 2, 'c', 3 ] >>> dict([(a...
Previous  1 2 Next