yunshichen

我相信人生是值得活的,尽管人在一生中必须遭受痛苦,卑劣,残酷,不幸和死亡的折磨,我依然深信如此.但我认为人生不一定要有意义,只是对一些人而言,他们可以使人生有意义. ---J 赫胥黎

2008年11月21日

Python实用教程:基础篇

介绍



本文方便有经验的程序员进入Python世界.

起步走

#! /usr/bin/python

a
=2
b
=3
c="test"
c
=a+b
print "execution result: %i"%c

知识点
  • Python是动态语言,变量不须预先声明.
  • 打印语句采用C风格

字符串和数字

但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异,如下:
#! /usr/bin/python

a
=2
b
="test"
c
=a+b

运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换
#! /usr/bin/python

a
=2
b
="test"
c
=str(a)+b
d
="1111"
e
=a+int(d)
#How to print multiply values
print "c is %s,e is %i" % (c,e)
知识点:
  • 用int和str函数将字符串和数字进行转换
  • 打印以#开头,而不是习惯的//
  • 打印多个参数的方式

国际化

写腻了英文注释,我们要用中文!

#! /usr/bin/python
#
 -*- coding: utf8 -*- 

print "上帝重返人间:马拉多纳出任阿根廷国家足球队主帅."
知识点:
  • 加上字符集即可使用中文

列表

列表类似Javascript的数组,方便易用

#! /usr/bin/python
#
 -*- coding: utf8 -*- 

#定义元组
word=['a','b','c','d','e','f','g']

#如何通过索引访问元组里的元素
a=word[2]
print "a is: "+a
b
=word[1:3]
print "b is: "
print b # index 1 and 2 elements of word.
c=word[:2]
print "c is: "
print c # index 0 and 1 elements of word.
d=word[0:]
print "d is: "
print d # All elements of word.

#元组可以合并
e=word[:2]+word[2:]
print "e is: "
print e # All elements of word.
f=word[-1]
print "f is: "
print f # The last elements of word.
g=word[-4:-2]
print "g is: "
print g # index 3 and 4 elements of word.
h=word[-2:]
print "h is: "
print h # The last two elements.
i=word[:-2]
print "i is: "
print i # Everything except the last two characters
l=len(word)
print "Length of word is: "+ str(l)
print "Adds new element"
word.append(
'h')
print word

#删除元素
del word[0]
print word
del word[1:3]
print word

知识点:
  • 列表长度是动态的,可任意添加删除元素.
  • 用索引可以很方便访问元素,甚至返回一个子列表
  • 更多方法请参考Python的文档

字典

#! /usr/bin/python

x
={'a':'aaa','b':'bbb','c':12}
print x['a']
print x['b']
print x['c']

for key in x:
    
print "Key is %s and value is %s",(key,x[key])
    
keys
=x.items();
print keys[0]
keys[0]
='ddd'
print keys[0]

知识点:
  • 将他当Java的Map来用即可.

字符串

比起C/C++,Python处理字符串的方式实在太让人感动了.把字符串当列表来用吧.

word="abcdefg"
a
=word[2]
print "a is: "+a
b
=word[1:3]
print "b is: "+# index 1 and 2 elements of word.
c=word[:2]
print "c is: "+# index 0 and 1 elements of word.
d=word[0:]
print "d is: "+# All elements of word.
e=word[:2]+word[2:]
print "e is: "+# All elements of word.
f=word[-1]
print "f is: "+# The last elements of word.
g=word[-4:-2]
print "g is: "+# index 3 and 4 elements of word.
h=word[-2:]
print "h is: "+# The last two elements.
i=word[:-2]
print "i is: "+# Everything except the last two characters
l=len(word)
print "Length of word is: "+ str(l)

不过要注意Asc和Unicode字符串的区别:
#! /usr/bin/python
#
 -*- coding: utf8 -*- 

s
=raw_input("输入你的中文名,按回车继续");
print "你的名字是  : " +s;

l
=len(s)
print "你中文名字的长度是:"+str(l);
a
=unicode(s,"utf8")
l
=len(a)
print "对不起,刚才计算错误.我们应该用utf8来计算中文字符串的长度, \
你名字的长度应该是:"+str(l);
知识点:
  • 用unicode函数进行转码

条件和循环语句

#! /usr/bin/python
x=int(raw_input("Please enter an integer:"))
if x<0:
    x
=0
    
print "Negative changed to zero"

elif x==0:
    
print "Zero"

else:
    
print "More"


# Loops List
= ['cat''window''defenestrate']
for x in a:
    
print x, len(x)

知识点:
  • 条件和循环语句
  • 如何得到控制台输入

函数

#! /usr/bin/python
#
 -*- coding: utf8 -*- 

def sum(a,b):
    
return a+b


func 
= sum
= func(5,6)
print r

# 提供默认值
def add(a,b=2):
    
return a+b
r
=add(1)
print r
r
=add(1,5)
print r

一个好用的函数
#! /usr/bin/python
#
 -*- coding: utf8 -*- 

# The range() function
=range(5,10)
print a
= range(-2,-7)
print a
= range(-7,-2)
print a
= range(-2,-11,-3# The 3rd parameter stands for step
print a

知识点:
  • Python 不用{}来控制程序结构,他强迫你用缩进来写程序,使代码清晰.
  • 定义函数方便简单
  • 方便好用的range函数

异常处理


#! /usr/bin/python
s=raw_input("Input your age:")
if s =="":
    
raise Exception("Input must no be empty.")

try:
    i
=int(s)
except ValueError:
    
print "Could not convert data to an integer."
except:
    
print "Unknown exception!"
else# It is useful for code that must be executed if the try clause does not raise an exception
    print "You are %d" % i," years old"
finally# Clean up action
    print "Goodbye!"




posted @ 2008-11-21 17:53 Chenyunshi 阅读(26) | 评论 (0)编辑 收藏

2008年11月20日

Squirrel sql client 是个好东东


Squirrel Sql Client 是个相当不错的sql客户端.支持oracle,mysql,hsql等多种数据库.java写的,跨平台,强力推荐.

官方网站:  http://www.squirrelsql.org/

posted @ 2008-11-20 23:04 Chenyunshi 阅读(29) | 评论 (0)编辑 收藏

Qt4 Designer 起步走



Qt简介和安装


最近打算重拾gui编程.vb/vc 已经忘得快差不多了,用java太繁琐,python嘛... 想复习复习c++语法,于是想到用Qt Designer , 支持C++/JAVA/PYTHON ,就算换一种语言,gui的知识还可以迁移--要熟悉一个gui编程环境也不容易.我不想花了很多时间学gui,换种语言后又把这个gui知识扔了.

Qt 的优点:
1.如上所述,支持多种语言.
2.跨平台.当然,用Java/Python能轻易写出跨平台代码,C++要注意别引入平台相关的库.
3.免费.
4.简单.和vb/delphi的难度差不多.
5.界面漂亮.

Qt安装

用ubuntu鼓捣东东是件挺快乐的事情.起码不用四处找软件,例如安装相关软件就很简单:

sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-designer ctags

如果本机还没有安装C++开发环境,那你还需要:

 sudo apt-get install build-essential g++ gcc-4.2-doc

IDE->qdevelop

你可以选择Qdevelop,和Qt绑定的一个小巧而强大的开发工具.

sudo apt-get install qdevelop

IDE->Eclipse


当然,还可以选择Eclipse.安装Java,下载Eclipse CDT , 然后到这个网站下载Qt plugin : http://trolltech.com/developer/eclipse-integration 并进行安装.

以前是没有这个plugin的,嘿嘿,现在做程序就是幸福.

打开Eclipse , 在"Window->Preference->qt" 里配置如下信息:
  • Name : Qt4
  • Bin Path : /usr/bin
  • Include Path : /usr/include/qt4
这是Ubuntu8.04 默认安装的qt4路径.如果你的路径和此不同,请自行修改.

然后,New一个Qt Project 看看效果,怀旧的同学们,这是不是让你想起曾经的Delphi?

2008-11-21-095135_1439x899_scrot.png




更新时间:2008-11-20

感谢

Ubuntu下配置Qt开发环境
How_to_use_Eclipse_with_Qt4

posted @ 2008-11-20 16:51 Chenyunshi 阅读(227) | 评论 (0)编辑 收藏

2008年11月5日

Javascript 工具和调试篇


简单介绍

    本文介绍整理在JS开发中常见的工具和调试技巧,分为:
  • 开发工具篇
  • 调试篇

开发工具

Linux文本编辑器

  • VIM
  • EMACS
  • GEDIT
    能用VIM/EMACS的一般不是新手,配置JS就不用我多废话了.在这里简单介绍GEDIT的配置:
  • 在Edit-->Preference-->Plugins里选上External tools,并选择"Configure plugin"






posted @ 2008-11-05 12:05 Chenyunshi 阅读(21) | 评论 (0)编辑 收藏

2008年10月9日

YUI 第一篇 : Yahoo global object 翻译


简介

Yahoo global object 提供namespace机制和封装了一些好用的方法. js名称为yahoo.js ,使用YUI前必须先引入这个js,且应该最先加载.

源文件
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js" ></script>

常用方法列举如下:

YAHOO.namespace


避免js全局变量污染的好办法,用法如下:
// Creates a namespace for "myproduct1"
YAHOO.namespace ("myproduct1");
YAHOO.myproduct1.Class1 
= function(info) {
    alert(info);
};

// Creates a namespace for "myproduct2", and for "mysubproject1"
YAHOO.namespace ("myproduct2.mysubproject1");
YAHOO.myproduct2.mysubproject1.Class1 
= function(info) {
    alert(info);
};

YAHOO.lang


提供了一些简便易用方法,如下:

// true, an array literal is an array
YAHOO.lang.isArray([12]);

// false, an object literal is not an array
YAHOO.lang.isArray({"one""two"});

// however, when declared as an array, it is true
function() {
    
var a = new Array();
    a[
"one"= "two";
    
return YAHOO.lang.isArray(a);
}();

// false, a collection of elements is like an array, but isn't
YAHOO.lang.isArray(document.getElementsByTagName("body"));

// true, false is a boolean
YAHOO.lang.isBoolean(false);

// false, 1 and the string "true" are not booleans
YAHOO.lang.isBoolean(1);
YAHOO.lang.isBoolean(
"true");

// null is null, but false, undefined and "" are not
YAHOO.lang.isNull(null); // true
YAHOO.lang.isNull(undefined); // false
YAHOO.lang.isNull(""); // false

// a function is a function, but an object is not
YAHOO.lang.isFunction(function(){}); // true
YAHOO.lang.isFunction({foo: "bar"}); // false

// true, ints and floats are numbers
YAHOO.lang.isNumber(0);
YAHOO.lang.isNumber(
123.123);

// false, strings that can be cast to numbers aren't really numbers
YAHOO.lang.isNumber("123.123");

// false, undefined numbers and infinity are not numbers we want to use
YAHOO.lang.isNumber(1/0);

// true, objects, functions, and arrays are objects
YAHOO.lang.isObject({});
YAHOO.lang.isObject(
function(){});
YAHOO.lang.isObject([
1,2]);

// false, primitives are not objects
YAHOO.lang.isObject(1);
YAHOO.lang.isObject(
true);
YAHOO.lang.isObject(
"{}");

// strings
YAHOO.lang.isString("{}"); // true
YAHOO.lang.isString({foo: "bar"}); // false
YAHOO.lang.isString(123); // false
YAHOO.lang.isString(true); // false
 
// undefined is undefined, but null and false are not
YAHOO.lang.isUndefined(undefined); // true
YAHOO.lang.isUndefined(false); // false
YAHOO.lang.isUndefined(null); // false


YAHOO.lang.hasOwnProperty

检查某个对象是否有某属性

// this is what we are protecting against
Object.prototype.myCustomFunction = function(x) {
    alert(x);
}

var o = {};
o[
"foo"= "bar";
o[
"marco"= "polo";

// this is where we need the protection
for (var i in o) {
    
if (YAHOO.lang.hasOwnProperty(o, i)) {
        alert(
"good key: " + i);
    } 
else {
        alert(
"bad key: " + i);
    }
}

YAHOO.lang.extend

提供了继承机制.(译者注:js使用继承还是有一定风险,建议酌情使用.)

YAHOO.namespace("test");

YAHOO.test.Class1 
= function(info) {
    alert(
"Class1: " + info);
};

YAHOO.test.Class1.prototype.testMethod 
= function(info) {
    alert(
"Class1: " + info);
};

YAHOO.test.Class2 
= function(info) {
    
// chain the constructors
    YAHOO.test.Class2.superclass.constructor.call(this, info);
    alert(
"Class2: " + info);
};

// Class2 extends Class1.  Must be done immediately after the Class2 constructor
YAHOO.lang.extend(YAHOO.test.Class2, YAHOO.test.Class1);

YAHOO.test.Class2.prototype.testMethod 
= function(info) {
    
// chain the method
    YAHOO.test.Class2.superclass.testMethod.call(this, info);
    alert(
"Class2: " + info);
};

var class2Instance = new YAHOO.test.Class2("constructor executed");
class2Instance.testMethod(
"testMethod invoked");


YAHOO.lang.augment

将某个对象的prototype属性复制到另一个对象,代码重用的好方法.
<!-- debugger output for environments without "console" -->
<div id="consoleelement"> </div>

<script>
////////////////////////////////////////////////////////////////////////////
//
 The ConsoleProvider example will log to the console if available, or a 
//
 div with id="consoleelement" if the console is not available
//
//////////////////////////////////////////////////////////////////////////
YAHOO.example.ConsoleProvider = function() { };
YAHOO.example.ConsoleProvider.prototype 
= {
    log: 
function(msg) {
        
// use the error console if available (FF+FireBug or Safari)
        if (typeof console != "undefined") {
            console.log(msg);
        
// write the msg to a well-known div element
        } else {
            
var el = document.getElementById("consoleelement");
            
if (el) {
                el.innerHTML 
+= "<p>" + msg + "</p>";
            }
        }
    }
};

////////////////////////////////////////////////////////////////////////////
//
 Define a class that should be able to write debug messages
//
//////////////////////////////////////////////////////////////////////////
YAHOO.example.ClassWithLogging = function() { };
YAHOO.lang.augment(YAHOO.example.ClassWithLogging, YAHOO.example.ConsoleProvider);

////////////////////////////////////////////////////////////////////////////
//
 Try it out
//
//////////////////////////////////////////////////////////////////////////
var c = new YAHOO.example.ClassWithLogging();
c.log(
"worked");

</script>

YAHOO.log

log工具,使用之前要先引入logger控件,如果没有引入,那么浏览器会忽略这条log语句.用这个东西使你免陷于开发时加alert,发布时四处删除alert的窘境.(注:当然,用firebug调试也可以避免alert问题.).


其他

至于YAHOO.env 和 YAHOO.name 等,参考官方文档即可.

http://developer.yahoo.com/yui/yahoo/




posted @ 2008-10-09 17:25 Chenyunshi 阅读(168) | 评论 (0)编辑 收藏

2008年10月6日

实战YUI 的 patinator(datatable) 组件

     摘要: 讲述如何定制YUI的paginator组件  阅读全文

posted @ 2008-10-06 11:17 Chenyunshi 阅读(406) | 评论 (6)编辑 收藏

2008年10月2日

实战YUI 的 Menu 组件

     摘要: Menu家族成员 YAHOO.widget.Menu YAHOO.widget.Overlay 的子类,其他menu容器的超类. Menu类创建一个容器(Container)并放置一列垂直的列表.每项菜单称为MenuItem.YAHOO.widget.ContextMenuMenu的子类,创建一个菜单,能被某html元...  阅读全文

posted @ 2008-10-02 01:07 Chenyunshi 阅读(66) | 评论 (0)编辑 收藏

2008年10月1日

实战YUI的Layout Manager 组件

     摘要: 序 和官网示例吹得天花乱坠不同,使用YUI是特别让人讨厌的经历,按照官方网指导有时候并不能得到正确的结果.本文记录了一些心得,顺手把官网文章翻译下来,以作备忘.正文出现的是翻译文字,个人见解用蓝色或红色字体标出.YUI使用常识篇1.必须引入一些js库和css,并且顺序很重要.顺序不对很可能得不到正确结果.2.debug版本js格式整齐,带所有log,通常用于开发阶段.min版本js将debug版本...  阅读全文

posted @ 2008-10-01 21:27 Chenyunshi 阅读(224) | 评论 (0)编辑 收藏

2008年9月24日

Ubuntu 8 配置 VIM

 安装
sudo apt-get install vim-gtk vim-doc cscope

创建目录

如果目录不存在,运行以下命令创建目录和文件:
mkdir ~/.vim
cd ~/.vim
mkdir plugin doc syntax
touch .vimrc

标准配置

打开.vimrc 文件,然后写入如下内容并保存:
set encoding=utf-8
syn on                  
" 打开语法高亮
set guifont=Luxi\ Mono\ 10 
" 设置字体,字体名称和字号
set tabstop
=4             " 设置tab键的宽度
set shiftwidth=4           
" 换行时行间交错使用4个空格
set autoindent             
" 自动对齐
set backspace=2           
" 设置退格键可用
set smartindent            
" 智能对齐方式
set ai!                   
" 设置自动缩进
set nu!                   
" 显示行号
set showmatch            
" 设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号
set ruler                 
" 在编辑过程中,在右下角显示光标位置的状态行
set incsearch              
" 查询时非常方便,如要查找book单词,当输入到/b时,会自动找到
                        
" 第一个b开头的单词,当输入到/bo时,会自动找到第一个bo开头的
                        
" 单词,依次类推,进行查找时,使用此设置会快速找到答案,当你
                        
" 找要匹配的单词时,别忘记回车
set vb t_vb=             
" vim进行编辑时,如果命令错误,会发出一个响声,该设置去掉响声

安装taglist插件

简单的说,该插件能让你根据函数名转到该函数定义,类似ide一样的功能

首先,先安装所需文件:
sudo apt-get install exuberant-ctags

然后下载taglist . http://vim-taglist.sourceforge.net/download.html

得到一个zip包,解压并将里面的doc和plugin复制到.vim目录

打开.vimrc 并增加如下内容:
map <C-t> :TlistToggle<CR>
imap <C-t> <Esc><C-t>i

打开一个c文件并按下CTRL+T,看看有什么效果?


感谢

网上无私的分享,本文参考资料如下:

http://blog.chinaunix.net/u1/55904/showart_435472.html




posted @ 2008-09-24 09:36 Chenyunshi 阅读(131) | 评论 (0)编辑 收藏

2008年9月12日

Django 1:安装配置运行1小时谈

     摘要: Django配置文档最精简版  阅读全文

posted @ 2008-09-12 16:35 Chenyunshi 阅读(161) | 评论 (0)编辑 收藏

仅列出标题  下一页
<2009年1月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

搜索

最新评论

阅读排行榜

评论排行榜