ITeye博客

Python

共 8 篇文章

python通过装饰器和线程限制函数的执行时间

很多时候函数内部包含了一些不可预知的事情,比如调用其它软件,从网络抓取信息,可能某个函数会卡在某个地方不动 态,这段代码可以用来限制函数的执行时间,只需要在函数的上方添加一个装饰器,timelimited(2)就可以限定函数必须在2秒内执行完成,如果执 行完成则返回函数正常的返回值,如果执行超时则会抛出错误信息。 代码参考: http://augustwu.iteye.com/

2015-08-09 17:46 · Python

Python高效编程技巧

我已经使用Python编程有多年了,即使今天我仍然惊奇于这种语言所能让代码表现出的整洁和对DRY编程原则的适用。这些年来的经历让我学到了很多的小技巧和知识,大多数是通过阅读很流行的开源软件,如Django, Flask, Requests中获得的。 下面我挑选出的这几个技巧常常会被人们忽略,但它们在日常编程中能真正的给我们带来不少帮助。 1.字典推导(Dictionary comprehensions)和集合推导(Set comprehensions) 大多数的Python程序员都知道且使用过列表推导(list comprehensions)。如果你对list comprehensions概…

2012-12-26 20:03 · Python

python 多线程 和 多进程

单线程例子: #!/usr/bin/python # -*- coding:UTF-8 -*- # Name: danxiancheng.py import time import threading def loop(num,sec): print 'loop %s start: ....' % num, time.strftime('%Y-%M-%d %H:%m:%S') time.sleep(sec) print 'loop %s stop: .....' % num, time.strftime('%Y-%M-%d %H:%m:%S') def main(): print '\nSt…

2012-12-24 23:17 · Python

python 数据类型

Python 元组、列表、字典、文件 转自 http://yangsq.iteye.com/category/20857 python的元组、列表、字典数据类型是很python(there python is a adjective )的数据结构。这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的益处。 元组 个人认为就像java的数组,python中的元组有以下特性: 任意对象的有序集合,这条没啥说的,数组的同性; 通过偏移读取; 一旦生成,不可改变; 固定长度,支持嵌套 来例子吧: python 代码 >>> (0, 'haha', (4j, 'y')) (0, …

2012-12-08 22:57 · Python

python 访问 oralce 和mysql 的两个工具类

oracle: python test_python_cx_Oracle.py renxp renxp 172.17.18.164:1521/orcl #!/usr/bin/python # Copyright Platform Computing Inc., an IBM company, 2012 import sys import string import os import time import cx_Oracle as oraledb # ----------------------------------------- # utility functions # ------…

2012-07-18 21:50 · Python

用C语音编写python的扩展模块,也就是python调c库

参考资料: 1.用C语言扩展Python的功能: http://www.ibm.com/developerworks/cn/linux/l-pythc/ 2.用C语言编写Python扩展模块: http://hi.baidu.com/jinmu190/blog/item/c5475846eee39c056a63e5f1.html 3.怎样编写python脚本的C扩展模块 http://blog.csdn.net/dengxu11/article/details/7393395 4. 使用C语言扩展Python http://www.cnblogs.com/phinecos/archive/2…

2012-07-09 11:17 · Python

可爱的python

1. ipython 是一个python的交互式的shell,功能强大,支持语法高亮,自动完成,代码调试,对象自省,支持bash shell 命令 2. python 没有switch 语句 3. python 中的三元表达试: 工作中的例子:getattr(o, "addresses", "") 这个可能返回空 {} ip = getattr(o, "addresses", "").get('private')[0]['addr'] if(getattr(o, "addresses", "")) else 'None' 4. 字符转换成整型 >>> a="12345" >>> import…

2010-11-08 19:28 · Python

开始学习python

按照下面的教材 花了 1天时间 学习了一下 http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/index.html

2010-09-27 14:52 · Python