Notes by Dr. Xi

Displaying keyword search results 1 - 10
Created by Dr. Xi on April 17, 2012 15:35:23    Last update: April 17, 2012 15:35:23
$ sudo apt-get install python-numpy [sudo] pa...
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 August 25, 2011 08:41:36    Last update: August 25, 2011 08:41:36
There is no switch statement in Python. PEP 3103 is in rejected status. Use the " if..elif.. " struct instead: r = i % 5 if r == 0: print "V: ", l ...
Created by Dr. Xi on August 10, 2011 14:58:49    Last update: August 10, 2011 14:58:49
In python: #!/usr/local/bin/python import sys if sys.ve... In shell script: #!/bin/bash PYTHON=/usr/local/bin/python ...
Created by Dr. Xi on August 02, 2011 15:44:45    Last update: August 02, 2011 15:44:45
The time module provides functions for time manipulation: $ python Python 2.7 (r27:82500, Sep 16 2010, 18...
Created by Dr. Xi on April 19, 2011 16:02:55    Last update: April 19, 2011 16:03:24
Method Description object.__getattr__(self,name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self ). object.__setattr__(self,name,value) Called when an attribute assignment is attempted. This is called instead of the normal mechanism (i.e. store the value in the instance dictionary). If __setattr__() wants to assign to an instance attribute, it should not simply execute self.name = value — this would cause a recursive call to itself. Instead, for new style classes, it should call the base class method with the same name, for example, object.__setattr__(self, name, value) . For classic classes, it should insert the value in the dictionary of instance attributes, e.g., self.__dict__ [name] =...
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 April 17, 2011 21:34:20    Last update: April 17, 2011 21:34:20
#!/usr/bin/python # -*- coding: latin-1 -*- ...
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...
Previous  1 2 3 4 5 6 7 Next