ITeye博客

python

共 17 篇文章

lost on Py_DECREF/INCREF when handling PyList_Append in Python C extension

The main hint is in the docs, if it says 'Steals a reference' than the function basically takes ownership, if it says 'New Reference' than it did an INCREF for you, if nothing is said it probably does an INCREF and DECREF pair as needed. http://stackoverflow.com/questions/10863669/lost-on-py-decref…

2015-11-03 21:41 · c++ · python · c

How can I convert a character to a integer in Python, and viceversa?

Use chr() and ord(): chr(97) 'a' ord('a') 97

2015-10-27 14:38 · python

Date/Time处理函数总结 [To Do]

几种我所用到的用来处理日期,时间的函数总结。 Perl localtime # 0 1 2 3 4 5 6 7 8 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); If parameter is not provides, it will use current time. Note the $year returned contains the number of years since 1900, to get a 4-digit year you need: $year += 1900; …

2013-04-12 10:46 · java · perl · python · javascript · shell

Twisted中的DeferredList

在这个blog上学习twisted。 http://blog.163.com/gzjck_gshock/blog/static/77206203201291461538770/ 里面的一个关于DeferredList的例子一开始对语法不太理解,copy并run了一遍就理解了,把代码贴在这里留作以后借鉴。 connecttest.py: from twisted.internet import reactor, defer, protocol class CallbackAndDisconnectProtocol(protocol.Protocol): def connectionMade(s…

2013-02-21 17:08 · python

python中list类型相减

两个 list ()类型相减 就是比如 a=[1,2,3,4,5] b=[2,3,4] a-b需要得到[1,5] 但是 python list 不支持 减法 只能使用py的投机取巧了~ 呵呵 我们来用set() 如下 <code l= python > print list (set(a)-set(b)) [1,5] #结果

2009-10-16 09:00 · python

python中的import

什么时候你应该使用 from module import ? 如果你要经常访问模块的属性和方法,且不想一遍又一遍地敲入模块名,使用 from module import 。 如果你想要有选择地导入某些属性和方法,而不想要其它的,使用 from module import 。 如果模块包含的属性和方法与你的某个模块同名,你必须使用 import module 来避免名字冲突。 除了这些情况,剩下的只是风格问题了,你会看到用两种方式编写的 Python 代码。 尽量少用 from module import * ,因为判定一个特殊的函数或属性是从哪来的有些困难,并且会造成调试和重构都更困难

2009-10-12 17:00 · python

python中的and-or技巧

>>> a = "first" >>> b = "second" >>> 1 and a or b 'first' >>> 0 and a or b 'second' 这个语法看起来类似于 C 语言中的 bool ? a : b 表达式。整个表达式从左到右进行演算,所以先进行 and 表达式的演算。 1 and 'first' 演算值为 'first' ,然后 'first' or 'second' 的演算值为 'first' 。 0 and 'first' 演算值为 False ,然后 0 or 'second' 演算值为 'second' 。 and-or 技巧,也就是 bool and…

2009-09-29 09:27 · python

python中使用可选参数和命名参数

Python 允许函数参数有缺省值;如果调用函数时不使用参数,参数将获得它的缺省值。此外,通过使用命名参数还可以以任意顺序指定参数。SQL Server Transact/SQL 中的存储过程也可以做到这些;如果你是脚本高手,你可以略过这部分。 info 函数就是这样一个例子,它有两个可选参数。 def info(object, spacing=10, collapse=1): spacing 和 collapse 是可选参数,因为它们已经定义了缺省值。 object 是必备参数,因为它没有指定缺省值。如果调用 info 时只指定一个参数,那么 spacing 缺省为 10 , collap…

2009-09-28 11:35 · python

python中的连续赋值

连续值赋值 >>> range(7) [0, 1, 2, 3, 4, 5, 6] >>> (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7) >>> MONDAY 0 >>> TUESDAY 1 >>> SUNDAY 6 内置的 range 函数返回一个元素为整数的 list。这个函数的简化调用形式是接收一个上限值,然后返回一个初始值从 0 开始的 list,它依次递增,直到但不包含上限值。(如果您愿意,您可以传入其它的参数来指定一个非 0 的初始值和非 1 的步长。也可以使用 print r…

2009-09-28 10:10 · python

python中的boolean

在 2.2.1 版本之前,Python 没有单独的布尔数据类型。为了弥补这个缺陷,Python 在布尔环境 (如 if 语句) 中几乎接受所有东西,遵循下面的规则: 0 为 false; 其它所有数值皆为 true。 空串 ( "" ) 为 false; 其它所有字符串皆为 true。 空 list ( [] ) 为 false; 其它所有 list 皆为 true。 空 tuple ( () ) 为 false; 其它所有 tuple 皆为 true。 空 dictionary ( {} ) 为 false; 其它所有 dictionary 皆为 true。

2009-09-28 09:55 · python

Python 和其他编程语言数据类型的比较

摘自《Dive into Python》。 静态类型语言 一种在编译期间就确定数据类型的语言。大多数静态类型语言是通过要求在使用任一变量之前声明其数据类型来保证这一点的。Java 和 C 是静态类型语言。 动态类型语言 一种在运行期间才去确定数据类型的语言,与静态类型相反。VBScript 和 Python 是动态类型的,因为它们确定一个变量的类型是在您第一次给它赋值的时候。 强类型语言 一种总是强制类型定义的语言。Java 和 Python 是强制类型定义的。您有一个整数,如果不明确地进行转换 ,不能将把它当成一个字符串。 弱类型语言 一种类型可以被忽略的语言,与强类型相反。VBScrip…

2009-09-27 14:43 · python

python中删除非空目录

import shutil shutil.rmtree(dir)

2009-08-25 21:08 · python

python subprocess

Module: subprocess Purpose: Spawn and communicate with additional processes. Python Version: New in 2.4 An updated version of this article can be found on the main PyMOTW site . Description: The subprocess module provides a consistent interface to creating and working with additional processes. It …

2009-08-13 11:30 · python

python中使用随机数

随机整数: >>> import random >>> random.randint(0,99) 21 随机选取0到100间的偶数: >>> import random >>> random.randrange(0, 101, 2) 42 随机浮点数: >>> import random >>> random.random() 0.85415370477785668 >>> random.uniform(1, 10) 5.4221167969800881 随机字符: >>> import random >>> random.choice('abcdefg&#%^*f') 'd' 多个字符中选…

2009-08-05 09:35 · python

Example of Using getopt in Python

import getopt, sys def main(): try: opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) output = None verbose = False for o, a in opts: if o == "-v": verbose = True if o in ("-h", "--help"): usage() …

2009-07-24 12:31 · python

python脚本参数处理的一个技巧

import sys width = int(sys.argv[1]) height = int(sys.argv[2]) count = int(sys.argv[3]) length = int(sys.argv[4]) base = sys.argv[5] type = sys.argv[6] 比较简便的做法: [width, height, count, length, base, type] = sys.argv[1:] 但是这样得到的都是string,如果前面三个是int,后面两个是string呢,可以使用map函数。 [width, height, count, length]…

2009-07-23 23:20 · python

Turn to Python from Perl: example of SSH in multi-threads

I need to execute commands on several remote servers at the same time via ssh. Writing shell script to send commands to servers with code below is quite simple but has trouble to know when all the tasks are finished on all servers. ssh $ip “nohup sh go.sh > log 2>&1 &” Then I turn to perl, write fo…

2009-07-21 16:39 · python