十一 03

但愿不用等太晚:)

#!/usr/bin/python
 
import urllib2
import sys
import time
 
def alarm():
	while True:
		sys.stdout.write('\a')
		sys.stdout.flush()
 
def poll():
	count=0;
	while True:
		count = count + 1
		now = time.localtime( time.time() )
		print '%02d:%02d:%02d %d' % (now.tm_hour, now.tm_min, now.tm_sec, count)
		try:
			urllib2.urlopen('http://store.apple.com/hk-zh/browse/home/shop_iphone/family/iphone/iphone4s')
			alarm()
		except urllib2.HTTPError as error:
			pass
			#print error
 
		try:
			iphone4 = urllib2.urlopen( 'http://store.apple.com/hk-zh/browse/home/shop_iphone/family/iphone/iphone4' )
			html = ''.join( iphone4.readlines() )
			if html.find( 'iPhone 4S' ) != -1 :
				alarm()
		except:
			pass
 
		#time.sleep(1)
 
if __name__ == '__main__':
	poll()
Tagged with:
24

这两天时间比较充裕,可以有比较长的连续时间集中精神,我用来学Python了。
有C++、Perl、Bash和PHP的基础,要入门其实并不太难。
没有关注太多的细节,套用很远古一句话,语言是用来写程序的,不是用来理解的。(粗体部分请用适当的词替换)
的确很方便,而且比Perl要容易,很多。
不过似乎Python不支持++运算符,无语。
另外,Python3和Python2不完全兼容,但对我是没有丝毫影响的:)

我主要的学习资料是网上的两个教程:

  1. Python 绝对简明手册
  2. 简明 Python 教程

都写得很好,如果是有其他语言的基础,看过应该就可以了解Python的基本用法了。

至于进一步的参考,找到了《Dive Into Python》的中文版,已经下载,作为工具书翻阅:)

趁着还记得一点,把前两天那个进化论中的概率论用Python重写了一次。
当然,结果一样,因为这个问题的数学期望是有理论保证的。

#!/usr/bin/python
 
import os
import random
 
RandomCount=0;
GenerateCount=0;
Target = list("tobeornottobe");
 
def getRandomChar():
	global RandomCount;
	RandomCount = RandomCount+1;
	return random.choice("abcdefghijklmnopqrstuvwxyz");
 
def compareList(list1, list2):
	if( len(list1) != len(list2) ):
		raise Exception;
	DifferentCount = 0;
	for i in range(len(list2)):
		if( list1[i] == list2[i] ):
			continue;
		else:
			DifferentCount = DifferentCount+1;
	return DifferentCount;
 
def generateRandomList(now, target):
	global GenerateCount;
	GenerateCount = GenerateCount + 1;
	result = [];
	for i in range(len(target)):
		if( now[i] != target[i] ):
			result.append( getRandomChar() );
			if( result[i] == target[i] ):
				now[i] = target[i];
		else:
			result.append( ' ' );
	return result;
#	print result;
 
 
Now = list(" " * len(Target));
while compareList(Now, Target) != 0:
	Result = generateRandomList(Now, Target);
	print "%6d" % GenerateCount, "|", "".join(Now), "|", "".join(Result);
print "RandomCount: ", RandomCount;
Tagged with:
preload preload preload

无觅相关文章插件,快速提升流量