Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Thursday, June 30, 2011

python example (jpeg timestamp)

This is a combination usage of:
iterator
callback
yield
image processing

Created/updated by BA43B09F0725,qunshan@newsmth

import os, sys, time
import Image, ImageDraw, ImageFont

def findfiles(path, *callbacks):
for root, dirs, names in os.walk(path):
for name in names:
filename = os.path.join(root, name)
info = tuple(x(filename) for x in callbacks)
yield (filename,)+info

def addString(filename, timestr):
font = ImageFont.truetype(os.path.join(os.environ['windir'], 'Fonts', 'AGENCYB.TTF'), 108)
im = Image.open(filename)
x, y = im.size
draw=ImageDraw.Draw(img)
#draw.ink = 0 + 255*256 + 0*256*256
draw.text((x-640,y-128), timestr, font=font)
img.save(filename)

def addTimeString(filename, mtime):
timestr = time.strftime("%Y-%m-%d %H:%M", time.localtime(mtime))
addString(filename, timestr)
os.utime(filename, (time.time(), mtime))

if __name__ == '__main__':
for filename, mtime in findfiles(sys.argv[1], os.path.getmtime):
if filename.lower().endswith(('.jpg', '.jpeg')):
addTimeString(filename, mtime)

Wednesday, May 25, 2011

cshell redirection

Character Action

>
Redirect standard output

>&
Redirect standard output and standard error

<
Redirect standard input

>!
Redirect standard output; overwrite file if it exists

>&!
Redirect standard output and standard error; overwrite file if it exists

|
Redirect standard output to another command (pipe)

>>
Append standard output

>>&
Append standard output and standard error

Tuesday, May 10, 2011

[python] Parameters: pass by value or ref?

C is pass-by-value.
Java is pass-by-value.
Python passes references-to-objects by value (like Java), and everything in Python is an object.

Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed.

Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside. Assigning a separate object (e.g. creating a new instance) to the input parameter will also not change the original instance.


   def  no_change0(val)
          val *= 2   #original val is not changed.
         
    def  no_change1(inst)
          inst = {}
          inst['anything'] = 'something'  #original inst is not changed!!!


   def  change1(inst)
          inst['anything'] = 'something'  #original inst is changed this time.

[python] How to iterate through a list

  • basic iteration 
Iterating through a list in Python is simple using a for in loop.
colours = ["red","green","blue"]

for colour in colours:
    print colour
  • enumerate 
To iterate through the same list using an index we can apply the enumerate() function.
colours = ["red","green","blue"]

for i, colour in enumerate(colours):
    print i, colour

  • Remove elements as you traverse a list
iterate over a copy of the list:
for c in colors[:]:
    if c == 'green':
        colors.remove(c)
Hereby colors[:] is a copy (a weird but, sigh, idiomatic way to spell list(colors)) so it doesn't get affected by the .remove calls

Monday, March 14, 2011

text formatter

#!/usr/bin/env python
# -*- coding: utf8 -*-
#-------------------------------------------------------------------------
#File name : strip.py
#Author : lfchen (Full Name please)
#Created : Mon 14 Mar 2011 02:04:35 PM CST
#Description :
# :
#Notes :
#-------------------------------------------------------------------------
#Copyright 2011 (c)
#-------------------------------------------------------------------------
import sys
import re

coding = 'utf-8'
def strip(infile, outfile):
try:
INFILE = open(infile,"r")
except:
print "\tERROR: fail to open file %s for processing" % infile
return
OUTFILE = open(outfile,"w")
prev_line=""
for line in INFILE:
line=line.decode(coding).rstrip("\n")
if line.endswith(" "): #keep ending space to avoid unintended contatenation of strings
line=line.rstrip() + " "
else:
line=line.rstrip()
if re.match(r'^\s*$', line) \
or line.endswith(".") or line.endswith("!") or line.endswith("?") or line.endswith(":") or line.endswith(";") \
or line.endswith("。".decode(coding)) or line.endswith("!".decode(coding)) or line.endswith("?".decode(coding)) \
or line.endswith(":".decode(coding)) or line.endswith(";".decode(coding)):
line += "\n"
if ('A'<=line[0]<='Z' or u'A'<=line[0]<=u'Z') and not prev_line.endswith("\n"):
line = "\n" + line
OUTFILE.write(line.encode(coding))
prev_line=line
INFILE.close()
OUTFILE.close()

if __name__ == "__main__":
if ("-h" in sys.argv[:]):
print "Usage: %s <filename(s)>" % sys.argv[0]
sys.exit(0)
else:
for filename in sys.argv[1:]:
print "dealing with file %s" % filename
strip(filename, "formatted_"+filename)

Monday, March 7, 2011

decorator

Definition
A decorator is a function whose primary purpose is to wrap another function or class.
Purpose
The primary purpose of this wrapping is to transparently alter or enhance the behavior
of the object being wrapped.
Basic usage
Syntactically, decorators are denoted using the special @ symbol as follows.  More than one decorator can also be applied.
@trace1
@trace0
def square(x):
    return x*x
The preceding code is shorthand for the following:
def square(x):
    return x*x
square = trace1(trace0(square))

Using parameters
A decorator can also accept arguments. Here’s an example:
@eventhandler('BUTTON')
def handle_button(msg):
    ...
The semantics of the decorator are as follows:
def handle_button(msg):
    ...
temp = eventhandler('BUTTON') # Call decorator with supplied arguments
handle_button = temp(handle_button) # Call the function returned by the decorator

Class decorator
Decorators can also be applied to class definitions. For example:
@foo
class Bar(object):
    def __init__(self,x):
        self.x = x
    def spam(self):
        statements

For class decorators, you should always have the decorator function return a class object
as a result.

how to release python program/lib

To distribute Python programs to others, you should use the distutils module.
1. As preparation, you should first cleanly organize your work into a directory that has a
README file, supporting documentation, and your source code.
Typically, this directory will contain a mix of library modules, packages, and scripts. Modules and packages refer to source files that will be loaded with import statements. Scripts are programs that will run as the main program to the interpreter (e.g., running as python scriptname).
Here is an example of a directory containing Python code:
spam/
    README.txt
    Documentation.txt
    libspam.py # A single library module
    spampkg/ # A package of support modules
        __init__.py
        foo.py
        bar.py
    runspam.py # A script to run as: python runspam.py



2. After you have organized your code, create a file setup.py in the top most directo-
ry (spam in the previous examples). In this file, put the following code:
# setup.py
from distutils.core import setup
setup(name = "spam",
    version = "1.0",
    py_modules = ['libspam'],
    packages = ['spampkg'],
    scripts = ['runspam.py'],
)

3.Type the following shell command to make a source distribution:
% python setup.py sdist
...
This creates an archive file such as spam-1.0.tar.gz or spam-1.0.zip in the directo-
ry spam/dist.

If you type 'python setup.py bdist', a binary distribution is created in
which all of the .py files have already been precompiled into .pyc files and placed into
a directory structure that mimics that of the local platform.
  If you run 'python setup.py bdist_wininst' on a Windows machine, an .exe file will be created.
4. To install, a user simply unpacks the archive and performs these steps:
% unzip spam-1.0.zip
...
% cd spam-1.0
% python setup.py install

This installs the software into the local Python distribution and makes it available for
general use. Modules and packages are normally installed into a directory called
"site-packages" in the Python library.

5. alternative install option
 Python software is often distributed in the form of an .egg file.This format is created by the popular setuptools
extension (http://pypi.python.org/pypi/setuptools).To support setuptools, you can
simply change the first part of your setup.py file as follows:
# setup.py
try:
    from setuptools import setup
    except ImportError:
    from distutils.core import setup
    setup(name = "spam",
        ...
    )

6. Appendix:
list of setup arguments:
Parameters to setup()Parameter Description
name Name of the package (required)
version Version number (required)
author Author’s name
author_email Author’s email address

maintainer Maintainer’s name
maintainer_email Maintainer’s email
url Home page for the package
description Short description of the package
long_description Long description of the package
download_url Location where package can be downloaded
classifiers List of string classifiers

Sunday, February 13, 2011

ajax & json

AJAX技术是由Jesse James Garrett于2005年2月在一篇文章中提出来,是Asynchronous JavaScript XML(异步JavaScript 和XML)的简称,Ajax提供与服务器异步通信的能力,一个最简单的应用是无需刷新整个页面而在网页中更新一部分数据。

设计的初衷是用传统的Web技术也能达到FLASH的效果。后来我在实践中发现,可以把JavaScript 和 XML 这两种传统的Web技术让来实现。

Google应该是AJAX最主要推动者,Google Map、Gmail都在应用AJAX,但是对Google最主要业务搜索来说,AJAX的支持并不好,因为搜索引擎主要是抓取静态的网页,而无法抓取态的AJAX技术。


Ajax包括:
XHTML和CSS
使用DOM(DocumentObjectModel,i.e.文档对象模型)作动态显示和交互
使用XML和XSLT做数据交互和操作
使用XMLHttpRequest进行异步数据接收
使用JavaScript将它们绑定在一起

一个Ajax交互从一个称为XMLHttpRequest的JavaScript对象开始。如同名字所暗示的,它允许一个客户端脚本来执行HTTP请求,并且将会解析一个XML格式的服务器响应。Ajax处理过程中的第一步是创建一个XMLHttpRequest实例。使用HTTP方法(GET或POST)来处理请求,并将目标URL设置到XMLHttpRequest对象上
在JavaWeb服务器上,到达的请求与任何其它HttpServletRequest一样。在解析请求参数后,servlet执行必需的应用逻辑,将响应序列化到XML中,并将它写回HttpServletResponse

JavaScript 代码完成非常基本的任务:
## 从 Web 表单中获取需要的数据。JavaScript 代码很容易从 HTML 表单中抽取数据并发送到服务器。
## 修改表单上的数据:更新表单也很简单,从设置字段值到迅速替换图像。
## 建立要连接的 URL; 打开到服务器的连接。
## 设置服务器在完成后要运行的函数。
## 发送请求。

XMLHttpRequest 几个方法和属性。
open():建立到服务器的新请求。
该方法有五个参数:
request-type:发送请求的类型。典型的值是 GET 或 POST,但也可以发送 HEAD 请求。
url:要连接的 URL。
asynch:如果希望使用异步连接则为 true,否则为 false。该参数是可选的,默认为 true。
username:如果需要身份验证,则可以在此指定用户名。该可选参数没有默认值。
password:如果需要身份验证,则可以在此指定口令。该可选参数没有默认值。

通常使用其中的前三个参数

send():向服务器发送请求。
abort():退出当前请求。
readyState:提供当前 HTML 的就绪状态。 onreadystatechange 属性允许指定一个回调函数。回调允许服务器反向调用 Web 页面中的代码
responseText:服务器返回的请求响应文本。
E.g.:
// --- html code, which calls callServer whenever the input box is changed ---
<form>
<p>City: <input type="text" name="city" id="city" size="25"
onChange="callServer();" /></p>
<p>State: <input type="text" name="state" id="state" size="25"
onChange="callServer();" /></p>
<p>Zip Code: <input type="text" name="zipCode" id="city" size="5" /></p>
</form>

// --- javascript ---
// --- create XMLHttpRequest on a browser (MSIE/opera/etc) ---
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}


function callServer() {
// Get the city and state from the web form
var city = document.getElementById("city").value;
var state = document.getElementById("state").value;
// Only go on if there are values for both fields
if ((city == null) || (city == "")) return;
if ((state == null) || (state == "")) return;
// Build the URL to connect to
var url = "/scripts/getZipCode.php?city=" + escape(city) + "&state=" + escape(state);
// Open a connection to the server
xmlHttp.open("GET", url, true);
// Setup a function for the server to run when it's done
xmlHttp.onreadystatechange = updatePage; //-> call updatePage when response is received (i.e. onreadystatechange)
// Send the request
xmlHttp.send(null);
}

// --- process server's response
function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById("zipCode").value = response;
}
}
// escape(): 它用于转义不能用明文正确发送的任何字符。比如,电话号码中的空格将被转换成字符 %20,从而能够在 URL 中传递这些字符

其中, 下面的"true" 表明这是一个异步连接, 这时表单将不等待服务器响应,用户可以继续访问表单.
>> xmlHttp.open("GET", url, true);



JSON:
JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然后就可以在函数之间轻松地传递这个字符串,或者在异步应用程序中将字符串从 Web 客户机传递给服务器端程序。
JSON 用 hash结构 ({}包围的) 来定义一个数据, 用 [] 包围的多个数据来 定义一个数组.

用[index]来访问数组的某个元素,"."来访问数据的key, 如:
>> people.programmers[0].lastName
要把json数据转换为字符串
>> String newJSONtext = people.toJSONString();
利用 POST 请求发送 JSON 数据
>> var url = "organizePeople.php?timeStamp=" + new Date().getTime();
>> request.open("POST", url, true);
>> request.onreadystatechange = updatePage;
>> request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
>> request.send(people.toJSONString());
如果通过GET方法通过url直接发送JSON数据,需用escape(people.toJSONString()) 以避免url错误

在服务器上解释 JSON
在服务器端处理 JSON 基本上就需要两个步骤。
1. 针对编写服务器端程序所用的语言,找到相应的 JSON 解析器/工具箱/帮助器 API。
2. 使用 JSON 解析器/工具箱/帮助器 API 取得来自客户机的请求数据并将数据转变成脚本能理解的东西。

比如,假设为 PHP 使用的是 JSON-PHP 模板
require_once('JSON.php');
$json = new Services_JSON();
// accept POST data and decode it
$value = $json->decode($GLOBALS['HTTP_RAW_POST_DATA']);
// Now work with value as raw PHP

Wednesday, August 4, 2010

advanced regular expression 高级正则表达式

断言
零宽度正预测先行断言 and 零宽度正回顾后发断言

(1)零宽度: 这表示匹配是一个位置(Loaction)而不是子表达式。
(2)预测先行 vs 回顾后发
预测先行:仅当子表达式在此位置的右侧匹配时才继续匹配。返回与子表达式匹配的前面位置,从做左到右匹配。
回顾后发:仅当子表达式在此位置的左侧匹配时才继续匹配。返回与子表达式匹配的后边位置,从右到左匹配。
(3)正向 vs 负向:
正向:当子表达式满足时才匹配。
负向:当子表达式不满足时才匹配。


总共四种断言:
零宽度正预测先行断言
vim: \@=
vim例:\w\+\(ing\)\@=     匹配以ing结尾的单词的前面部分(除了ing以外的部分)
   如果用magic, 则为 \v\w+(ing)@\=
零宽度正回顾后发断言
vim: \@<=
vim例:\(re\)\@<=\w+     匹配以re开头的单词的后半部分(除了re以外的部分)
   如果用magic, 则为 \v(re)@\<\=\w+
零宽度负预测先行断言
vim: \@!
vim例:foo\(bar\)\@!     匹配后面不带 "bar" 的 "foo"
零宽度负回顾后发断言
vim: \@<!
vim例:\(foo\)\@<!bar  匹配前面不带 "foo" 的 "bar"

PCRE 对应的格式为:
(?= 子表达式), (?<= 子表达式)
(?! 子表达式), (?<! 子表达式)

for perl help:
# perldoc perlre

.NET Framework中支持有名RE, 并支持注释:
(?<myname>expression) 如果遇到匹配,则把匹配的内容命名为myname,并压入堆栈
(?<-myname>expression') 如果遇到匹配,则从堆栈中弹出 名为myname的匹配内容。
(?(myname)yes|no) 如果堆栈上存在命名为myname的匹配的内容,则去继续匹配yes部分的表达式,否去继续匹配no部分。


 注释
 注释一般通过表达式(?#注释)实现。
以下正则表达式在第一个分组中中添加了注释“不能以数字开头”。
(?<!\d+(?#不能以数字开头))[a-z_A-Z]+
如果要在正则表达式中包含注释,最好打开IgnorePatternWhitespace选项,即忽略模式里的空白字符。

Monday, June 21, 2010

mix of hash & array (perl)

  • array of hashes
@AoH = (
    {
       husband  => "barney",
       wife     => "betty",
       son      => "bamm bamm",
    },
…
)
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
$AoH[0]{husband} = "fred";
$AoH[1]{husband} =~ s/(\w)/\u$1/;

  • array of arrays
@AoA = (
         [ "fred", "barney" ],
         [ "george", "jane", "elroy" ],
         [ "homer", "marge", "bart" ],
);
$AoA[2][3]
$ref_to_AoA->[2][3]


  • hash of arrays
%HoA = (
  flintstones => [ "fred","barney" ],
…)
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
$HoA{flintstones}[0] = "Fred";


  • hash of hashes
%HoH = (
    flintstones => {
        husband   => "fred",
        pal       => "barney",
    },
...
)
$HoH{ mash } = {
    captain  => "pierce",
    major    => "burns",
    corporal => "radar",
};
$role=$HoH{$family}{$role};

Sunday, May 30, 2010

perl vs python (hash and list/array)











  perl python
  hash array hash array (tuple for constant, and
list for variable)
symbol
(whole, ele)
{},{} (),[] {},[] []/()
(list/tuple), []
declare my %hash =
("foo", 35)

my %last_name = (

       
"fred" => "flintstone",

…)
my @var = (11, 27.1 , "a string");

my @var = qw/ hello my god /
some_dict =
{

       
"fred": "flintstone",

…}
list = ['milk',
1, 'lettuce']
clear %hash = () @var = ()

$#array = -1;
dict={} list = []
retain
the size
my $count = keys %hash; $scalar = @array;

$scalar = scalar @array;
count = len(hash) l=len(L)
add/use
element
$last_name{'wilma'} =
"flintstone";
$array[3] = 5

@list2 = (1, @list1, 5); 
#nesting is not supported

@subarray2 = @array[0,1..3]
some_dict['wilma'] =
"flintstone"
a = (1, 2); b = (3, 4); c = (a, b) => ((1,2),(3,4))

c=a+b => (1,2,3,4)

L[i:j] = J;
use
keys/values
my @k =
keys %hash;

my @v = values %hash;

while ( ($key, $value) = each %hash )

  …
  k =
hash.keys()

v = hash.values()

for (key, value) in hash.items():

  ...
 
check
existence of an ele
if (exists $books{'dino'}) exists  $array[
$index ] -> existence

defined $array[ $index ] -> defined
if books.has_key('dino'):  
add an
ele to the end
  push(@coins,
"Penny");
  list.append(x)
add an
ele to the beginning
  shift(@coins,
"First");
  list.insert(0,
x)
remove
an ele
delete $books{$person};    del books[person]  
from the
beginning
  $var=unshift(@coins);   a=list.pop([i])
from the
end
  $var=pop(@coins);   a=list.pop()
sort sort { $a <=> $b }
keys(%hash)
@array2 =
reverse sort (@array);
  sorted([5, 2, 3, 1, 4])

list.sort()

list.reverse()
sort
(unique value)
  my @s = uniq sort @a;    
join/split   $firststring
= join(", ",@array);

@array = split('-',$astring);
   
index       list.index(var)  #find the first ele that match var
ref my
$hash_ref = {};

$href->{ $key } = $value;
    ref_list=true_list





Monday, May 17, 2010

verilog/sv indent


  1 #!/usr/bin/perl

  2 # Author; Lifeng Chen, Magnum Semiconductor

  3 # Purpose: Indent verilog/systemverilog file

  4

  5 use Term::ANSIColor;

  6

  7 # Flow Control

  8 my $debug_me = 1;

  9

 10 ##TODO

 11 # 1. better indent for block-comment

 12

 13 # Space for each indent

 14 my $Space = "  ";

 15

 16 # comment definition.

 17 my $sl_delm = qw(// );

 18 my %block_delm = (

 19   '{' => '}',

 20   '(' => ')'

 21 );

 22

 23 # Note: Each Open keyword must have corresponding close keyword

 24 my @OpenSingle = qw{repeat for while if else};

 25 my @CloseSingle = qw{; };

 26

 27 my @OpenKeywords = qw{ begin module program package class task function fork case casex casez};  #interface

 28 my @CloseKeywords = qw{ end endmodule endprogram endpackage endclass endtask endfunction join join_none join_any endcase};  #endinterface

 29 my @IgnoreKeywords = qw{ extern typedef disable};

 30

 31 my @OpenSpecial = qw{ovm_field_utils_begin ovm_object_utils_begin ovm_object_param_utils_begin ovm_component_utils_begin};

 32 my @CloseSpecial = qw{ovm_field_utils_end ovm_object_utils_end ovm_object_param_utils_end ovm_component_utils_end};

 33

 34 @OpenKeywords = (@OpenKeywords, @OpenSpecial);

 35 @CloseKeywords = (@CloseKeywords, @CloseSpecial);

 36

 37 sub indent {

 38     my $open_block_comment=0, $close_block_comment=0, $in_block_comment=0;

 39     my $total_indent = 0, $block_indent = 0, $single_indent = 0, $last_indent=0;

 40     my($infile) = @_;

 41     my $tmpfile= "$infile.tmp";

 42     my $open_num=0, $close_num=0, $bracket_delta=0;

 43     my $cleaned_line;

 44

 45     print colored ['green'],"$infile\n";

 46     system("cp $infile $tmpfile");

 47

 48     open(TFILE, ">$tmpfile");

 49     open(FILE, "<$infile");

 50

 51     LINE_LOOP: while(my $line = <FILE>) {

 52         chomp $line;

 53         if ($open_block_comment==1 and $close_block_comment==0) {$in_block_comment = 1;}

 54         else {$in_block_comment = 0;}

 55         $line =~ s{$sl_delm(\w|\*)}{$sl_delm $1};  #remove false block-comment; append space after single-line comment symbol immediately followed by letters etc

 56         $cleaned_line=$line;

 57

 58         if ($cleaned_line =~ m{/\*}) {

 59             $open_block_comment=1;  #start a block comment

 60         }

 61         if ($cleaned_line =~ m{\*/}) {

 62             $close_block_comment=1; #end a block comment

 63         }

 64         if ($close_block_comment == 1) {  #clear block comment flag

 65             $open_block_comment=0;

 66             $close_block_comment=0;

 67         }

 68         #if ($in_block_comment) {$cleaned_line =~ s{^.*\*/}{*/};}  #remove anything in block comment, till end-block symbol is detected

 69         $cleaned_line=~s{"[^"]*"}{}g;  #remove keyword in string

 70         $cleaned_line =~ s{/\*.*\*/}{}g;  #remove keyword in a single-line block comment

 71         $cleaned_line =~ s{^.*\*/}{};  #remove keyword in block comment

 72         $cleaned_line =~ s{/\*.*$}{};  #remove keyword in block comment

 73         $cleaned_line=~ s{$sl_delm.*$}{};  #remove keyword in line comment

 74         $cleaned_line =~ s{\s+$}{};  #remove tailing blanks

 75         #if ($debug_me == 1 and $. >= 577 and $. <= 585) {

 76         #print "debug_me: $., blk_ind: $block_indent, sgl_ind: $single_indent, comment=$in_block_comment: '$cleaned_line'\n";

 77         #}

 78         if ($in_block_comment==1) {

 79             print TFILE "$line\n";  #keep things as they were in a block comment

 80             next LINE_LOOP;

 81         }

 82         $line =~ s/^\s+//;  #remove leading space to prepare for indent

 83         $line =~ s/\s+$//;  #remove any tailing space

 84

 85         #output the active line

 86         if ($line =~ m{^$sl_delm}) {  #indent single-line comment

 87             for($i=0;$i<$block_indent+$single_indent;$i++) { print TFILE $Space; }

 88             print TFILE "$line\n";

 89             next LINE_LOOP;

 90         }

 91

 92         foreach $CloseKey (@CloseKeywords) {

 93             if($cleaned_line =~ /\b$CloseKey\b/) { $block_indent--; $single_indent=0;}

 94         }

 95         $bracket_delta=0;

 96         foreach $open (keys %block_delm) { #indent brackets

 97           $close=$block_delm{"$open"};

 98           $open_num = ($cleaned_line =~ s/\Q$open\E/$open/isg);

 99           $close_num = ($cleaned_line =~ s/\Q$close\E/$close/isg);

100           $bracket_delta+=$open_num-$close_num;

101         }

102         if ($cleaned_line =~ /\S/) {for($i=0;$i<$block_indent+$single_indent;$i++) { print TFILE $Space; }}  #don't indent empty line

103         print TFILE "$line\n";

104         $start_block_line=0;

105         OPEN_BLOCK: foreach $OpenKey (@OpenKeywords) {

106             foreach $IgnoreKey (@IgnoreKeywords) {

107               if($cleaned_line =~ /\b$IgnoreKey\b/) { last OPEN_BLOCK;}

108             }

109             if($cleaned_line =~ /\b$OpenKey\b/) { $block_indent++; $start_block_line=1; }

110         }

111         if ($start_block_line == 0) {

112             OPEN_SINGLE: foreach $OpenKey (@OpenSingle) {

113                 if($cleaned_line =~ /\b$OpenKey\b/ and $cleaned_line !~ /`$OpenKey\b/ ) {

114                     $single_indent++;

115                 }

116             }

117             if ($single_indent > 0) {

118                 CLOSE_SINGLE: foreach $CloseKey (@CloseSingle) {

119                     if($cleaned_line =~ /$CloseKey$/) {

120                         $single_indent=0;

121                     }

122                 }

123             }

124         }

125         $block_indent+=$bracket_delta;

126         #if ($bracket_delta<0) {$block_indent+=$bracket_delta; }

127         #if ($bracket_delta>0) {$block_indent+=$bracket_delta; }

128         if ($block_indent < 0) {print colored ['yellow'],"\tindent_num = $block_indent @ cleaned_line $.. will be reset.\n"; $block_indent=0;}

129         $last_indent=$block_indent;

130     }

131     $total_indent=$block_indent + $single_indent;

132     if ($total_indent != 0) {print colored ['yellow'],"\tThe indentation of final line is not cleared to zero: $block_indent + $single_indent\n";}

133     close FILE; close TFILE;

134     system("mv -f $tmpfile $infile");

135 }

136

137 my $argnum;

138 if($ARGV[0] eq "" or $ARGV[0] =~ /-h/) {

139     print "Usage : $0 verilogfile.v\n";

140     print "        $0 *.sv\n";

141     print "        $0 *.*v*\n";

142     print "Help  :\n";

143     print "        $0\n";

144     exit(0);

145 }

146 print "Indenting...\n";

147 foreach $argnum (0 .. $#ARGV) {

148     my $ifile = $ARGV[$argnum];

149     &indent($ifile);

150 }

>

Tuesday, May 26, 2009

perl vs xml vs excel

use Spreadsheet::ParseExcel;
use XML::DOM;
my $doc = XML::DOM::Document->new;

my $xml_pi = $doc->createXMLDecl ('1.0',"UTF-8","yes");
$doc->setXMLDecl($xml_pi);

my $root = $doc->createElement('tests');
$root->setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance");

my $parser = Spreadsheet::ParseExcel->new(
  CellHandler => \&cell_handler,
  NotSetCell => 1
);

my $workbook = $parser->Parse($filename);
$root->printToFile ($outFile);

exit(0);

  sub cell_handler {

  my $workbook = $_[0];
  my $sheet_index = $_[1];
  my $row = $_[2];
  my $col = $_[3];
  my $cell = $_[4];
 my $myTag;
 my $myText;

  $testName=$cell->value();
  if ($testName !~ m{\w+\.sv}) {$testName = ""; $lastCol = -1; return;}
  $test = $doc->createElement('Test');
  $myTag = $doc->createElement('Name');
  $myText = $doc->createTextNode($cell->value());
  $myTag->appendChild($myText);    #set text content for this tag
  $test->appendChild($myTag);

  if ($implemented == 1) { $root->appendChild($test); } # add new entry only if it's new line

}

Monday, May 25, 2009

install perl module as non-root

perl Makefile.PL, make, make test, make install

The standard way to install a Perl module on Unix is to change into the directory that was created when you unpacked the .tar.gz file, and then type the following sequence of commands:

  perl Makefile.PL
  make
  make test
  make install


This will create a makefile for you, then compile the module, test it, and put it in the correct location for you. This requires that you are logged in as root, so that you can copy files to the Perl library directory, and various other places on your system where the installation will put files.

If you do not have root permissions on the machine where you want to install the module, such as if you wish to install a module in your home directory, just change one of those commands. Instead of

  perl Makefile.PL

type

  #perl Makefile.PL PREFIX=/path/to/where/you/want/it
  perl Makefile.PL INSTALL_BASE=/path/to/where/you/want/it

 make install

That will put all the files in that directory. In order to use modules that are stored in that location, you will need to add the following like to your Perl programs:

  use lib "/path/to/where/you/want/it/perl5"
and, yes, thanks for the comment, local::lib might be easier to install a new module.
  use local::lib "/path/to/where/you/want/it/perl5"

Tuesday, December 30, 2008

Make script

The oritinal page is http://www.hsrl.rutgers.edu/ug/make_help.html
How to write a Makefile

How to write a Makefile



Introduction


Make is one of the original Unix tools for Software Engineering. By
S.I. Feldman of AT&T Bell Labs circa 1975. But there are public
domain versions (eg. GNU) and versions for other systems
(eg. Vax/VMS).


Related tools are the language compilers (cc, f77, lex, yacc, etc.)
and shell programming tools (eg. awk, sed, cp, rm, etc.). You need to
know how to use these.


Important adjuncts are lint (source code checking for obvious errors)
ctags (locate functions, etc. in source code) and mkdepend. These are
nice, and good programmers use them.


Important, and related tools, are the software revision systems SCCS (Source Code Control System) and RCS (Revision Control System -- the recommended choice)

The idea is to automate and optimize the construction of
programs/files -- ie. to leave enough foot prints so that others can
follow.

Makefile Naming



make is going to look for a file called Makefile, if not found then a
file called makefile. Use the first (so the name stands out in
listings).


You can get away without any Makefile (but shouldn't)! Make has
default rules it knows about.

Makefile Components




  • Comments


    Comments are any text beginning with the pound (#) sign. A
    comment can start anywhere on a line and continue until the end of the
    line. For example:


    # $Id: slides,v 1.2 1992/02/14 21:00:58 reggers Exp $


  • Macros

    Make has a simple macro definition and substitution mechanism. Macros are defined in a Makefile as = pairs. For example:


    MACROS= -me
    PSROFF= groff -Tps
    DITROFF= groff -Tdvi
    CFLAGS= -O -systype bsd43

    There are lots of default macros -- you should honor the existing naming conventions. To find out what rules/macros make is using type:

    % make -p

    NOTE: That your environment variables are exported into the make as macros. They will override the defaults.


    You can set macros on the make command line:


    % make "CFLAGS= -O" "LDFLAGS=-s" printenv
    cc -O printenv.c -s -o printenv


  • Targets

    You make a particular target (eg. make all), in none specified then the first target found:


    paper.dvi: $(SRCS)
    $(DITROFF) $(MACROS) $(SRCS) >paper.dvi

    NOTE: The the line beginning with $(DITROFF) begins with TAB not spaces.

    The target is made if any of the dependent files have changed. The dependent files in this case are represented by the $(SRCS) statement.



  • Continuation of Lines

    Use a back slash (\). This is important for long macros and/or rules.




  • Conventional Macros


    There are lots of default macros (type "make -p" to print out the defaults). Most are pretty obvious from the rules in which they are used:


    AR = ar
    GFLAGS =
    GET = get
    ASFLAGS =
    MAS = mas
    AS = as
    FC = f77
    CFLAGS =
    CC = cc
    LDFLAGS =
    LD = ld
    LFLAGS =
    LEX = lex
    YFLAGS =
    YACC = yacc
    LOADLIBS =
    MAKE = make
    MAKEARGS = 'SHELL=/bin/sh'
    SHELL = /bin/sh
    MAKEFLAGS = b


  • Special Macros


    Before issuing any command in a target rule set there are certain special macros predefined.



    1. $@ is the name of the file to be made.
    2. $? is the names of the changed dependents.


    So, for example, we could use a rule


    printenv: printenv.c
    $(CC) $(CFLAGS) $? $(LDFLAGS) -o $@

    alternatively:

    printenv: printenv.c
    $(CC) $(CFLAGS) $@.c $(LDFLAGS) -o $@

    There are two more special macros used in implicit rules. They are:


    1. $< the name of the related file that caused the action.
    2. $* the prefix shared by target and dependent files.



  • Makefile Target Rules


    The general syntax of a Makefile Target Rule is


    target [target...] : [dependent ....]
    [ command ...]

    Items in brackets are optional, ellipsis means one or more.
    Note the tab to preface each command is required.


    The semantics is pretty simple. When you say "make target"
    make finds the target rule that applies and, if any of the
    dependents are newer than the target, make executes the com-
    mands one at a time (after macro substitution). If any
    dependents have to be made, that happens first (so you have
    a recursion).


    A make will terminate if any command returns a failure sta-
    tus. That's why you see rules like:


    clean:
    -rm *.o *~ core paper

    Make ignores the returned status on command lines that begin
    with a dash. eg. who cares if there is no core file?


    Make will echo the commands, after macro substition to show
    you what's happening as it happens. Sometimes you might want
    to turn that off. For example:


    install:
    @echo You must be root to install



  • Example Target Rules


    For example, to manage sources stored within RCS (sometimes
    you'll need to "check out" a source file):


    SRCS=x.c y.c z.c

    $(SRCS):
    co $@

    To manage sources stored within SCCS (sometimes you'll need
    to "get" a source file):

    $(SRCS):
    sccs get $@

    Alternativley, to manage sources stored within SCCS or RCS
    let's generalize with a macro that we can set as required.

    SRCS=x.c y.c z.c
    # GET= sccs get
    GET= co

    $(SRCS):
    $(GET) $@

    For example, to construct a library of object files

    lib.a: x.o y.o z.o
    ar rvu lib.a x.o y.o z.o
    ranlib lib.a

    Alternatively, to be a bit more fancy you could use:

    OBJ=x.o y.o z.o
    AR=ar

    lib.a: $(OBJ)
    $(AR) rvu $@ $(OBJ)
    ranlib $@

    Since AR is a default macro already assigned to "ar" you can
    get away without defining it (but shouldn't).


    If you get used to using macros you'll be able to make a few
    rules that you can use over and over again.

    For example, to construct a library in some other directory


    INC=../misc
    OTHERS=../misc/lib.a

    $(OTHERS):
    cd $(INC); make lib.a

    Beware:, the following will not work (but you'd think it
    should)

    INC=../misc
    OTHERS=../misc/lib.a

    $(OTHERS):
    cd $(INC)
    make lib.a

    Each command in the target rule is executed in a separate
    shell. This makes for some interesting constructs and long
    continuation lines.


    To generate a tags file


    SRCS=x.c y.c z.c
    CTAGS=ctags -x >tags

    tags: $(SRCS)
    ${CTAGS} $(SRCS)

    On large projects a tags file, that lists all functions and
    their invocations is a handy tool.


    To generate a listing of likely bugs in your problems

    lint:
    lint $(CFLAGS) $(SRCS)

    Lint is a really good tool for finding those obvious
    bugs that slip into programs -- eg. type classes, bad argu-
    ment list, etc.


  • Some Basic Make Rule


    People have come to expect certain targets in Makefiles. You
    should always browse first, but it's reasonable to expect
    that the targets all (or just make), install, and clean will
    be found.



    1. make all -- should compile everything so that you can do local testing before installing things.
    2. make install -- should install things in the right places. But watch out that things are installed in the right place for your system.
    3. make clean -- should clean things up. Get rid of the executables, any temporary files, object files, etc.


    You may encounter other common targets, some have been
    already mentioned (tags and lint).



  • An Example Makefile for printenv



    # make the printenv command
    #
    OWNER=bin
    GROUP=bin
    CTAGS= ctags -x >tags
    CFLAGS= -O
    LDFLAGS= -s
    CC=cc
    GET=co
    SRCS=printenv.c
    OBJS=printenv.o
    SHAR=shar
    MANDIR=/usr/man/manl/printenv.l
    BINDIR=/usr/local/bin
    DEPEND= makedepend $(CFLAGS)
    all: printenv

    # To get things out of the revision control system
    $(SRCS):
    $(GET) $@
    # To make an object from source
    $(CC) $(CFLAGS) -c $*.c

    # To make an executable

    printenv: $(OBJS)
    $(CC) $(LDFLAGS) -o $@ $(OBJS)

    # To install things in the right place
    install: printenv printenv.man
    $(INSTALL) -c -o $(OWNER) -g $(GROUP) -m 755 printenv $(BINDIR)
    $(INSTALL) -c -o $(OWNER) -g $(GROUP) -m 644 printenv.man $(MANDIR)

    # where are functions/procedures?
    tags: $(SRCS)
    $(CTAGS) $(SRCS)

    # what have I done wrong?
    lint: $(SRCS)
    lint $(CFLAGS) $(SRCS)

    # what are the source dependencies
    depend: $(SRCS)
    $(DEPEND) $(SRCS)

    # to make a shar distribution
    shar: clean
    $(SHAR) README Makefile printenv.man $(SRCS) >shar

    # clean out the dross
    clean:
    -rm printenv *~ *.o *.bak core tags shar

    # DO NOT DELETE THIS LINE -- make depend depends on it.
    printenv.o: /usr/include/stdio.h




  • Makefile Implicit Rules


    Consider the rule we used for printenv


    printenv: printenv.c
    $(CC) $(CFLAGS) printenv.c $(LDFLAGS) -o printenv

    We generalized a bit to get

    printenv: printenv.c
    $(CC) $(CFLAGS) $@.c $(LDFLAGS) -o $@

    The command is one that ought to work in all cases where we
    build an executable x out of the source code x.c This can be
    stated as an implicit rule:

    .c:
    $(CC) $(CFLAGS) $@.c $(LDFLAGS) -o $@

    This Implicit rule says how to make x out of x.c -- run cc
    on x.c and call the output x. The rule is implicit because
    no particular target is mentioned. It can be used in all
    cases.


    Another common implicit rule is for the construction of .o
    (object) files out of .c (source files).


    .o.c:
    $(CC) $(CFLAGS) -c $<

    alternatively

    .o.c:
    $(CC) $(CFLAGS) -c $*.c


  • Make Dependencies


    It's pretty common to have source code that uses include
    files. For example:


    % cat program.c

    #include
    #include "defs.h"
    #include "glob.h"
    etc....
    main(argc,argv)
    etc...

    The implicit rule only covers part of the source code depen-
    dency (it only knows that program.o depends on program.c).
    The usual method for handling this is to list the dependen-
    cies separately;

    etc...
    $(CC) $(CFLAGS) -c $*.c
    etc...
    program.o: program.c defs.h glob.h

    Usually an implicit rule and a separate list of dependencies
    is all you need. And it ought to be easy enough to figure
    out what the dependencies are.


    However, there are a number of nice tools around that will
    automatically generate dependency lists for you. For example
    (trivial):


    DEPEND= makedepend $(CFLAGS)
    etc...
    # what are the source dependencies

    depend: $(SRCS)
    $(DEPEND) $(SRCS)

    etc....
    # DO NOT DELETE THIS LINE -- ....

    printenv.o: /usr/include/stdio.h

    These tools (mkdepend, mkmkf, etc.) are very common these
    days and aren't too difficult to use or understand. They're
    just shell scripts that run cpp (or cc -M, or etc.) to find
    out what all the include dependencies are. They then just
    tack the dependency list onto the end of the Makefile.



Based on Make and Makefiles by
Reg Quinton

Computing and Communications Services

The University of Western Ontario

London, Ontario N6A 5B7

Canada




Press here to return to the General Unix Software Menu.

Wednesday, November 12, 2008

python常用命令

本文出自 http://www.dev.idv.tw:8080/mediawiki/index.php/
常用的Python指令

下面列出常用的Python指令及其說明,對於初學Python的人而言應當相當有用:


sys模組:


 argv: 命令列參數。
exit([arg]): 結束程式。
exitfunc: 若有指定此函式,程式結束前會先呼叫此函式。

getopt模組:


 getopt(args, options[, long_options]): 解譯命令列參數選項的工具。

os模組:


 chdir(path): 變更目前工作目錄。
getcwd(): 取得目前工作目錄。
getenv(varname[, defaultValue]): 取得環境變數。
putenv(varname, value): 設定或新增環境變數。
popen(command[, mode[, bufsize] ]): 執行某個命令,並將結果以pipe的方式傳會此程式。
tmpfile(): 傳回一個新的暫存檔案物件,此物件將以"w+b"的mode開啟。
listdir(path): 傳回指定路徑的內容。
remove(path): 刪除指定的檔案。
removedirs(path): 以遞迴的方式刪除指定的路徑。
rename(src, dst): 變更路徑或檔案名稱。
renames(old, new): 遞迴的變更路徑或檔案名稱。
rmdir(path): 非遞迴的方式移除目錄。
stat(path): 傳回指定檔案或路徑的stat結構。
walk(top[, topdown=True [, onerror=None] ]): 產生整個目錄的樹狀結構。
abort(): 對目前的行程產生SIGABRT的訊號。
system(command): 在sub-shell中執行命令。
path.abspath(path): 傳回指定目錄的絕對路徑。
path.join(a, *p): 將兩個或多個路徑名稱結合成一個路徑。並在適當的地方加上目錄分隔字元。
path.exists(path): 檢查檔案或路徑是否存在。
path.basename(path): 傳回路徑的最後一個部分。
path.dirname(path): 傳回檔案名稱中屬於路徑的部分。
path.split(path): 將路徑分割成為各部分。
path.splitdrive(path): 分割出檔案名稱中,關於磁碟機的部分。
path.splitext(path): 分割出檔案名稱中,關於副檔名的部分。
path.splitunc(path): 分割出檔案名稱中屬於UNC的部分。
path.getsize(path): 取得指定檔案的大小。
path.isfile(path): 檢查指定的路徑是否指向一個檔案。
path.isdir(path): 檢查指定的路徑是否為一個目錄。
path.isabs(path): 檢查指定的路徑是否為一個絕對路徑。

shutil模組:


 copy(src, dst): 拷貝src所指定的檔案到dst所指定的檔案中。
copytree(src, dst[, symlinks]): 遞迴的方式將整個src所指定的目錄樹拷貝到dst所指定的地方。
rmtree(path[, ignore_errors[, onerror] ]): 將整個目錄樹下的所有檔案目錄刪除。
move(src, dst): 將整個檔案或目錄搬移到dst所指定的地方。

glob模組:


 glob(pathname): 依照類似於shell在使用的檔案符合檢驗的格式來找出某個目錄下的檔案。

re模組:


 re.compile(pattern[, flags]): 將指定的pattern字串編譯並產生RegExp物件。
regexp.match(string[, pos[, endpos] ]): 若字串與pattern相符,傳回Match物件,否則傳回None。
regexp.search(string[, pos[, endpos] ]): 搜尋字串便找出符合pattern的字串。
regexp.split(string[, maxsplit]): 以pattern作為分割字串,將指定的字串分割成為數個部分。
regexp.sub(repl, string[, count]): 將相符合的字串取得成為另一個字串。
matchObj.group([group1, ...]): 傳回Match物件中的指定群組字串。
matchObj.groups(): 傳回所有的Match物件中的群組。
matchObj.groupdict(): 傳回所有的Match物件中有取名稱的群組。

Saturday, November 8, 2008

python 简明教程

http://www.chinesepython.org/pythonfoundry/tut2.3/tmp/tmp.html
变量和赋值
同一个值可以同时赋给几个变量:
>>> x = y = z = 0
复数也同样得到了支持,虚部由一个后缀"j"或者"J"来表示。带有非零实部的复数记为"real+imagj",也可以通过"complex(real, img)"函数创建
字符串用单引号或双引号标识。可以在行加反斜杠做为继续符。字符串可以用一对三重引号”””或'''来标识
字符串可以用 + 号联接(或者说粘合),也可以用 * 号循环。e.g. '<' + word*5 + '>'。两个字符串值之间的联接是自动的
字符串可以用下标(索引)查询;就像 C 一样,字符串的第一个字符下标是0(zero)
切片索引(Slice indices)可以使用默认值
链表写为中括之间用逗号分隔的一列值(子项)。链表的子项不一定是同一类型的值。
>>> a = ['spam', 'eggs', 100, 1234]
与不变的字符串不同,链表可以改变每个独立元素的值
Control Flow
if ... elif ... elif ... 序列用于替代其它语言中的 switch 或 case 语句。
for 语句依据任意序列(链表或字符串)中的子项,按它们在序列中的顺序来进行迭代
range()生成一个等差级数链表
>>> range(5, 10) [5, 6, 7, 8, 9]
>>> range(0, 10, 3) [0, 3, 6, 9]
>>> range(-10, -100, -30) [-10, -40, -70]
break 语句和 C 中的类似,用于跳出最近的一级 for 或 while 循环。
continue 语句是从 C 中借鉴来的,它表示循环继续执行下一次迭代
pass 语句什么也不做
定义函数
关键字 def 引入了一个函数定义。在其后必须跟有函数名和包括形式参数的圆括号。函数体语句从下一行开始,必须是缩进的。函数体的第一行可以是一个字符串值,这个字符串是该函数的 (文档字符串(documentation string)),也可称作 docstring 。
引用参数时,会先从局部符号表中查找,然后是全局符号表,然后是内置命名表。
全局参数虽然可以被引用,但它们不能在函数中直接赋值
函数引用的实际参数在函数调用时引入局部符号表,因此,实参总是传值调用
参数个数可变的函数
1.最有用的形式是给一个或多个参数指定默认值
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
2.函数可以通过参数关键字的形式来调用
parrot(action = 'VOOOOOM', voltage = 1000000)
3.不常用的选择是可以让函数调用可变个数的参数
def fprintf(file, format, *args):
如果要传递的参数已经是一个列表但要调用的函数却接受分开一个个的参数值,在调用函数时加一个 *操作符来自动把参数列表拆开
range(*args)
通过 lambda 关键字,可以创建很小的匿名函数
模块
标准模块
包通常是使用用“圆点模块名”的结构化模块命名空间。例如,名为 A.B 的模块表示了名为 "B" 的包中名为 "A"的子模块。
导入模块时,Python通过 sys.path 中的目录列表来搜索存放包的子目录
包用户可以从包中导入合法的模块,例如: import Sound.Effects.echo
使用 from package import item 方式导入包时,这个子项(item)既可以是包中的一个子模块(或一个子包),也可以是包中定义的其它命名,像函数、类或变量
包支持一个另为特殊的变量, __path__ 。 在包的 __init__.py 文件代码执行之前,该变量初始化一个目录名列表。该变量可以修改,它作用于包中的子包和模块的搜索功能
输入和输出
字符串格式化
标准模块 string包括了一些操作,将字符串填充入给定列时,这些操作很有用
使用 % 操作符,以某个字符串做为其左参数
Python总是把任意值传入 repr() 或 str() 函数,转为字符串。相对而言引号 (``) 等价于repr()
>>> repr(0.1)' 0.10000000000000001'
iterator
大多数容器对象都可以用 for 遍历:
for element in [1, 2, 3]: print element
for element in (1, 2, 3): print element
for element in range(len(data)-1, 1, -1): print element
for key in {'one':1, 'two':2}: print key
for char in "123": print char
for line in open("myfile.txt"): print line
在后台,for 语句在容器对象中调用 iter()。它在容器中逐一访问元素。没有后续的元素时,next() 抛出一个 StopIteration 异常通知 for 语句循环结束。

Class
类定义语法
最简单的类定义形式如下:

class ClassName:

.
.
.


类的定义就像函数定义( def 语句),要先执行才能生效。(你当然可以把它放进 if 语句的某一分支,或者一个函数的内部。)

定义一个类的时候,会创建一个新的命名空间,将其作为局部作用域使用——因此,所以对局部变量的赋值都引入新的命名空间。特别是函数定义将新函数的命名绑定于此。
类对象 Class Objects
类对象支持两种操作:属性引用和实例化
类的实例化使用函数符号。只要将类对象看作是一个返回新的类实例的无参数函数即可。例如:
x = MyClass()
类的实例化操作会自动为新创建的类实例调用 __init__() 方法。可以有参数
实例、方法对象
a method can called immediately like this: x.f()
or stored and used like this: xf = x.f; while True: print xf()
special notes
同名的数据属性会覆盖方法属性,因此应避免可能的命名冲突
类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称,但是在调用这个方法的时候你不为这个参数赋值,Python会提供这个值。这个特别的变量指对象本身,按照惯例它的名称是self。但对 Python 而言,self 绝对没有任何特殊含义
继承
派生类的定义如下所示:
class DerivedClassName(BaseClassName):

Python同样有限的支持多继承形式。多继承的类定义形如下例:
class DerivedClassName(Base1, Base2, Base3):

私有变量
Python 对类的私有成员提供了有限的支持。任何形如 __foo随即都被替代为 _classname__foo