文章分类 -  python

摘要:import osimport socketimport threadingimport getopt, sys def connect_port(ip, port): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, port)) print 'port %d is open' % (port) ... 阅读全文
posted @ 2011-12-01 23:15 Mingxx 阅读(422) 评论(0) 推荐(0)
摘要:最近在项目中碰到一些文字替换。如果手动一个一个去换,这对一个程序员,太丢脸了。最近想想用PYTHON写些脚本。 form xml.etree.ElementTree import ElementTree if __name__ == ‘__main__’ tree = ElementTree() root = tree.parse(‘Test.xml’) nodes = root.get... 阅读全文
posted @ 2011-11-23 23:18 Mingxx 阅读(710) 评论(0) 推荐(0)
摘要:Python中有一个有趣的语法,只要定义类型的时候,实现__call__函数,这个类型就成为可调用的。 换句话说,我们可以把这个类型的对象当作函数来使用,相当于 重载了括号运算符。 例如,现在我们要计算重力环境下的自然落体位移。我们知道Sy=(gt**2)/2,那么,我们可以建立一个函数: def g_dpm(t): return (9.8*t**2)/2 我们都知道,地球表面的重力加速... 阅读全文
posted @ 2011-10-28 21:14 Mingxx 阅读(323) 评论(0) 推荐(0)
摘要:1.多线程方法#!/usr/python#threadimport string, threading, timedef thread_main(a): #get thread name global count, mutex threadname = threading.currentThread().getName() for x in xrange(0, int(a)): #get lock mutex.acquire() count = count + 1 #release lock mutex.release() print threadname, x, count time.sle 阅读全文
posted @ 2011-06-20 21:07 Mingxx 阅读(349) 评论(0) 推荐(0)
摘要:1. in python3 use dict to formate strings:>>>print(“%(a)s” % {'a’:'b’})b>>>a = ‘b’>>>print(“%(a)s” % locals())b2. in Python 2.6 on up you can use the new format() method on strings:>>>'{a}'.format(a = ‘spam’)'spam’3. using string>>>imp 阅读全文
posted @ 2011-06-20 20:28 Mingxx 阅读(1328) 评论(0) 推荐(0)