Archive for the ‘python’ Category
Sage Tutorial中文版
Sage Tutorial是一份快速了解Sage的入门文档,花了两周多的时间将其翻译为中文。但是“Some more advanced mathematics”一节没有翻译,因为完全不熟悉这一节所涉及到的内容。
第一次翻译开源软件的技术文档,水平有限,希望各位多提意见,任何方面的都可以。如果没有大的问题,大概一周后会向Sage开发组提交。
源文件使用Sphinx管理,PDF文件是由自动转换而来的LaTeX文件编译的,因此其中很多格式不符合中文的习惯。所以请大家先以HTML文件为准,最后发布前,我会手工调整LaTeX文件后再编译。
Email: amao@ai7.org
相关内容:翻译Sage文档的准备工作
Ubuntu 9.10下unzip的乱码问题
Ubuntu 9.10中unzip升级为6.0,结果原来的-O参数没有了,解压缩Win平台来的带中文文件名的文件时,会有乱码。Google,Baidu无果后,自行使用Python解决。
01 #!/usr/bin/python
02 #coding=utf8
03
04 import zipfile
05 import sys
06
07 if len(sys.argv)<2:
08 print u’punzip zipfilename’
09 else:
10 f=zipfile.ZipFile(sys.argv[1])
11 nlist=f.namelist()
12 for n in nlist:
13 m=unicode(n,’gb2312′).encode(‘utf8′)
14 file(m,’wb’).write(f.read(n))
15 f.close()
翻译Sage文档的准备工作
打算翻译一下Sage的文档。Sage的文档完全是用reStructuredTEXT写成的,由Sphinx管理,对应的翻译就行了,但是为了与原文档保持相同的风格,还是折腾了好久。这里记录一下。
环境是:Ubuntu+Sage 4.2.1+CTeXLive 2008。以翻译Sage Tutorial为例,需要做以下准备工作。(下面用$SAGE代替sage的主目录)
- 转到$SAGE/devel/sage-main/doc/目录;
- 在common目录中的builder.py文件中,找到
LANGUAGES = ['en', 'fr']
修改为:
LANGUAGES = ['en', 'fr', 'zh']
- 新建zh子目录,并将en子目录下的tutorial目录复制到刚才新建的zh目录中 (如果是翻译其他文档,复制相应的目录);
- 修改zh目录下的conf.py文件:
- 在
import sys, os
这一行后 添加 两行:
reload(sys) sys.setdefaultencoding('utf8') - 在conf.py文件最后添加:
latex_preamble='\usepackage{ctex}'latex_premble+='\usepackage{ctex}\n\punctstyle{CCT}'
- 在
- 修改$SAGE/local/lib/python2.6/site-packages/Sphinx-0.6.3-py2.6.egg/sphinx/texinputs/Makefile文件,将其中的“pdflatex”全部替换为“xelatex”(共有5处);
- 将$SAGE/local/lib/python2.6/site-packages/Sphinx-0.6.3-py2.6.egg/sphinx/writers/latex.py中的“\\usepackage[utf8]{inputenc}”中的“utf8”修改为“latin1”,即“\\usepackage[latin1]{inputenc}”。
如果上述操作正常的话,就可以开始翻译了。将zh/tutorial下的所有.rst 文件翻译为中文后,即可进行编译。编译的方法如下:
- 转到$SAGE目录;
- 查看所有文档列表:
./sage -docbuild -D
此时应该可以看到“zh/tutorial”选项;
- 编译:
./sage -docbuild zh/tutorial html ./sage -docbuild zh/tutorial pdf
编译结果会在编译信息最后一行提示。
Zoundry Raven开源了
Zoundry Raven以及原来的Zoundry是我在Windows平台下最喜欢的Blog客户端程序。但是因为只有Windows平台的版本,所以后来还是ScribeFire用的多。刚刚知道Zoundry Raven开源了(消息来源:python编写的博客编写客户端Zoundry Raven开源)。过去看了一下,两个没想到,没想到今年1月份就已经开源了,我还以为是最近的事情;另一个没想到是,开源了大半年,居然还没有人移植到其他平台上。
SimPy
想用Python写一个仿真程序,网上的中文资料很少,比较完整的是IBM的这篇技术文章:
英文原版:Charming Python: SimPy simplifies complex models
中文版:可爱的 Python: SimPy 简化了复杂模型
本来是不错的文章,但是由于写的比较早(2002年12月26日),很多语句的用法在新版中发生了很大的变化。搞了一个晚上,程序勉强调通,但是那个记录最大最小时间的问题一直解决不了。实在搞不下去了,就到SimPy自带的doc中扒扒,没想到这个例子已经被收入正式的发行包中。具体位置在:/usr/share/doc/python-simpy-doc/SimPyModels/Market.py,如果是windows用户,请自己查找Market.py文件,应该是有的。
教训啊教训。
附Market.py原文:
import random
from math import sqrt
AISLES = 6 # Number of open aisles
ITEMTIME = 0.1 # Time to ring up one item
AVGITEMS = 20 # Average number of items purchased
CLOSING = 60*12 # Minutes from store open to store close
AVGCUST = 1500 # Average number of daily customers
RUNS = 8 # Number of times to run the simulation
SEED = 111333555 # seed value for random numbers
class Customer(Process):
def __init__(self):
Process.__init__(self)
# Randomly pick how many items this customer is buying
self.items = 1 + int(random.expovariate(1.0/AVGITEMS))
def checkout(self):
start = now() # Customer decides to check out
yield request, self, checkout_aisle
at_checkout = now() # Customer gets to front of line
waittime.tally(at_checkout-start)
yield hold, self, self.items*ITEMTIME
leaving = now() # Customer completes purchase
checkouttime.tally(leaving-at_checkout)
yield release, self, checkout_aisle
class Customer_Factory(Process):
def run(self):
while 1:
c = Customer()
activate(c, c.checkout())
arrival = random.expovariate(float(AVGCUST)/CLOSING)
yield hold, self, arrival
class Monitor2(Monitor):
def __init__(self):
Monitor.__init__(self)
self.min, self.max = (int(2**31-1),0)
def tally(self, x):
self.observe(x)
self.min = min(self.min, x)
self.max = max(self.max, x)
random.seed(SEED)
print ‘Market’
for run in range(RUNS):
waittime = Monitor2()
checkouttime = Monitor2()
checkout_aisle = Resource(AISLES)
initialize()
cf = Customer_Factory()
activate(cf, cf.run(), 0.0)
simulate(until=CLOSING)
print “Waiting time average: %.1f“ % waittime.mean(), \
“(std dev %.1f, maximum %.1f)” % (sqrt(waittime.var()),waittime.max)
print ‘AISLES:’, AISLES, ‘ ITEM TIME:’, ITEMTIME
Project Euler第6题
筛法求素数表
第一个版本:
P=range(2,20001)
i=0
l=len(P)
t1=datetime.datetime.now()
while i
for item in P[(i+1):]:
if item % P[i]==0:
P.remove(item)
l-=1
i+=1
t2=datetime.datetime.now()
print t2-t1
print P
第二个版本:
P=range(2,20001)
i=0
l=len(P)
t1=datetime.datetime.now()
while i
temp=[item for item in P[(i+1):] if item % P[i] != 0]
P=P[:(i+1)]+temp
l=len(P)
i+=1
t2=datetime.datetime.now()
print t2-t1
print P
第三个版本:
temp=range(2,20001)
P=[]
t1=datetime.datetime.now()
while temp!=[]:
P.append(temp[0])
temp=[item for item in temp if item % P[-1] != 0]
t2=datetime.datetime.now()
print t2-t1
print P
第一个版本需要7秒多,第二个版本只需要2秒多,第三个版本就只需要1.8秒左右了。
注1:刚开始学Python,正在慢慢体会Python的精神,希望能尽快写出Python风格的程序,而不是用Python语言写Pascal程序。
注2:正是因为刚开始学,所以实际上不止三个版本,但是从7秒多优化到2秒以内,还是很有成就感的。
注3:还是太慢了。
ibus输入法
Ubuntu下的输入法换成了ibus,尝试一下。上屏的方式和FCITX不太一样,有点类似拼音输入法的方式,和一般形码的上屏方式都不一样,要多按很多空格,感觉有点不习惯。
另外就是安装文件非常大。
用一段再说吧。不行就再换回去。
python没有规划相关的库?
只找到SciPy中有求函数最小值的fmin函数。但是这个似乎是求解无约束优化的。
最近刚开始学python,有没有高手指点一下。