Recent Notes
Displaying keyword search results 1 - 10
Created by Dr. Xi on June 06, 2009 18:31:44
Last update: June 25, 2012 12:37:35
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...
or
>>> import subprocess
>>> subprocess.call(['cur...
Created by voodoo on February 16, 2012 13:35:38
Last update: February 16, 2012 13:35:57
The C shell allows you to define aliases with arguments: \!^ passes the first argument, \!* passes all arguments. Examples from http://unixhelp.ed.ac.uk :
alias print 'lpr \!^ -Pps5'
alias print 'lp...
In ksh or bash you cannot define alias with arguments. Use function instead.
Created by Dr. Xi on April 20, 2011 21:44:15
Last update: May 02, 2011 20:56:58
The String.format() method provides versatile formatting capabilities. This tutorial tries to present these capabilities in a accessible manner. The format string A format string can contain zero, one, or more format specifiers . The general form of a format specifier is:
%[argument_index$] [flags] [width] [.precision]co... where things in square brackets are optional, and conversion is a character indicating the conversion to be applied to the corresponding variable value. The only required characters in the format specifier is the percent sign % and the conversion character. A simple example: public static void simpleFormat() { System.out... The Argument index The argument index is specified by a number, terminated by the dollar sign $ . The same argument may be repeated multiple times in a format string. Unindexed...
Created by jinx on April 20, 2011 08:40:38
Last update: April 20, 2011 08:48:09
The PHP function time returns the current time in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). This is the same function as the Perl time() .
<?php
print time() . "\n";
?>
Calling mktime without any arguments does the same:
<?php
echo mktime(), "\n";
?>
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 February 28, 2011 12:29:19
Last update: February 28, 2011 12:30:40
The Unix shell passes these parameters to the shell script:
Variable Meaning
$* A single string representing all command line arguments separated by $IFS (internal field separator, usually a space)
$@ A sequence of strings representing the command line arguments.
$1,$2...$n $1 is the first argument, $2 is the second argument, and so on...
$0 The name of the script itself.
$# The number of arguments.
Example shell script ( sharg.sh ):
#!/bin/sh
echo $# arguments passed to $0: $@
...
Output for ./sharg.sh "a b c d" :
1 arguments passed to ./sharg.sh: a b c d
here ...
Output for ./sharg.sh a "b c" d :
3 arguments passed to ./sharg.sh: a b c d
here ...
Created by dov on October 25, 2010 08:42:12
Last update: October 25, 2010 08:42:12
Ouch, got bitten on pythons lack of copy of arrays, even in construct arguments:
class Foo():
def __init__(self,
...
This prints:
foo2 = [42]
Solution is to do:
self.stuff = stuff[:]
Created by Dr. Xi on May 31, 2009 03:19:00
Last update: July 16, 2010 19:37:07
For old-style classes: BaseClassName.methodname(self, arguments)
>>> class A:
... def hello(self):
... ...
For new-style class (any class which inherits from object ): super(ClassName, self).method(args)
>>> class M(object):
... def hello(self):
...
Reference:
Things to Know About Python Super
Created by Dr. Xi on December 05, 2009 20:12:16
Last update: December 05, 2009 20:46:45
It's quite easy for Perl to open a pipe and read from it:
$file = "nospace.txt";
open(IN, "cat $file |") ...
But the code breaks when the file name contains a space:
# This does not work!
$file = "yes space.txt";
...
On Windows, these don't work either:
# This does not work!
$file = "yes space.txt";
...
You need to use a technique called Safe Pipe Opens :
$file = "yes space.txt";
$prog = "cat";
...
Created by Dr. Xi on December 04, 2009 04:33:05
Last update: December 04, 2009 04:33:05
Variable Meaning $_ The default or implicit variable. @_ Within a subroutine the array @_ contains the parameters passed to that subroutine. $a, $b Special package variables when using sort() $<digit> Contains the subpattern from the corresponding set of capturing parentheses from the last pattern match, not counting patterns matched in nested blocks that have been exited already. $. Current line number for the last filehandle accessed. $/ The input record separator, newline by default. $| If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after...