2008/04/08

重载Python类对象的打印函数

没有评论:
class G:
def __init__(self):
g.nodes = "all nodes of G"
def __repr__(self):
return self.nodes
...
g = G()
print G

Python中的全局变量

没有评论:

全局变量

这里谈全局变量呢,倒不是说Python和c的全局变量概念不同,他们的概念是相同的。只是在使用机制上,是有一些差异的。举个例子:

– module.py –
globalvar = 1

def func():
print globalvar
# This makes someglobal readonly,
# any attempt to write to someglobal
# would create a new local variable.

def func2():
global globalvar
globalvar = 2
# this allows you to manipulate the global
# variable

在 func这个函数中,globalvar是只读的。如果你使用了globalvar = xxx这种赋值语句,Python会重新创造一个新的本地对象并将新值赋给它,原来的对象值不变。而在func2函数中,由于我们事先申明了 globalvar是global的,那么此时的更改就直接在全局变量上生效。

很明显这和c中的使用机制是不一样的,在c中,我们只要在函数外的全局区域申明了变量,就可以在函数中直接对其操作,不用还申明一个global。

Python中的时间操作

没有评论:

Python中的时间操作还是比较方便的,有专门的time模块提供多种接口形式。其中常用的两种操作数据格 式就是滴答和时间元组,关于这两个奇怪的名字,我也不便解释,总之在一本书上看着这么写,也就这么用,确实很难听。滴答是从1970年开始到现在的秒数, 在Python2.4.3中是一个浮点数,小数部分为更精密的部分,小于1秒的精细时间值。时间元组是一个具有9个成员的元组,依次为:年、月、日、小 时、分钟、秒、星期(0-6,0为周一)、一年中的哪一天(1-366,古罗马历)、Daylight savings(-1,0,1,我也不知是干什么用的)。

所以可以用如下方法提取当天的当时时间:

a,a,a,hour,minute,second,a,a,a=time.localtime(time.time())

这里的hour、minute、second分别是当时的时、分、秒,也是比较常用的参数。另外就是关于求时间差,如果使用 time.localtime()测定时间差,那么滴答值为0时的中国标准时间是1970年1月1日8:00,时间差无从谈起,所以对于时间差应该使用 time.gmtime()转换为TimeTuple再解包为各个时间参量。比如如下获得时间差:

a,a,a,hour,minute,second,a,a,a=time.gmtime(t1-t2)


对 dictionary of datetime排序

通常用 listObj.sort()排序,如果需要定义自己的排序方式,则:
1. 定义一个排序函数
def dictionary_datetime_sorter( d1, d2):
if d1 elif d1>d2: return 1
else: return 0

...
2. 将此函数作为sort()的input 对 list 排序。调用格式:
datetimeList.sort(dictionary_datetime_sorter)


2008/04/05

在source insight中添加 python language

没有评论:

语言包从该处下载:
http://www.sourceinsight.com/public/languages/

To import a custom language file into Source Insight:

1. Select Options > Preferences. Click the Languages tab.

2. Click the Import button. Select and load the custom language file (.CLF)

3. You should now see the new language in the list of languages.

4. Click the Document Types button to open the Document Options dialog box.

5. If you don't have a document type already created for the type of language file, you will need to create one now. If you already have a document type created, select it in the list and go to step 7.

6. Click the Add Type button to create a new document type. Give it a name that describes the type of file. For example, "Ant File". Click OK and then fill in the file filter text box with a wildcard. For example, "*.ant".

7. In the Parsing section of the Document Options dialog box, select the newly imported language in the language drop-down list. This is what associates your document type with the custom language.

8. Click the Close button to close Document Options. Then click OK to close the Preferences dialog box.

2008/04/04

Python的时间操作 2

没有评论:
字符串是使用静态的方式进行存储,只能读而不能直接修改字符内容。特别将一堆对字符串并在一起的时候,虽然可以直接相加,听说这样的速度奇慢,只有用其它函数的方式进行,好在也不太麻烦。

比如用 print ','.join(datelist)

就可以将datelist列表里面的所有项目并成一个字符串,当然这个表达式会在每一个项目中间插入一个逗号,这种方式比用循环的方式更简洁。

日期的操作必须使用time或datetime库

import time
>>> s="2006-1-2"
>>> time.strptime(s,"%Y-%m-%d)
这是将字符串格式的日期及时间转成日期对象
转义符对应意义如下
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%j 年内的一天(001-366)
%m 月份(01-12)
%M 分钟数(00=59)
%p 本地A.M.或P.M.的等价符
%S 秒(00-59)
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%Z 当前时区的名称
%% %号本身

============================================

#-*- coding:utf-8 -*-

import time
import datetime

# 2007-11-25 15:36:35

#使用datetime模块可以很方便的解决这个问题,举例如下:

d1 = datetime.datetime(2005, 2, 16)
d2 = datetime.datetime(2004, 12, 31)

# 结果:47
print (d1 - d2).days

#上例演示了计算两个日期相差天数的计算。

starttime = datetime.datetime.now()

endtime = datetime.datetime.now()
print (endtime - starttime).seconds

#上例演示了计算运行时间的例子,以秒进行显示。

d1 = datetime.datetime.now()
d3 = d1 + datetime.timedelta(days =10)

print str(d3)
print d3.ctime()

# 上例演示了计算当前时间向后10天的时间。
# 如果是小时 days 换成 hours

# 其本上常用的类有:datetime和timedelta两个。它们之间可以相互加减。
# 每个类都有一些方法和属性可以查看具体的值,如datetime可以查看:天数(day),小时数(hour),星期几(weekday())等;
# timedelta可以查看:天数(days),秒数(seconds) 等。

#
# time , datetime , string 类型互相转换
#
# string -> time
# time.strptime(publishDate,"%Y-%m-%d %H:%M:%S")
#
# time -> string
# time.strftime("%y-%m-%d",t)

date = '2007-01-01'

print type(date)

date = time.strptime(date,"%Y-%m-%d")

print type(date)

print date[0]

d4 = datetime.datetime(date[0], date[1],date[2])

print d4
print type(d4)

#将日期时间对象转成字符串则要用

date = time.strftime("%y-%m-%d",date)
print type(date)

#其中d为日期时间对象