PEP 0263
Defining Python Source Code Encodings
Python will default to ASCII as standard encoding if no other
encoding hints are given.
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:
# coding=<encoding name>
or (using formats recognized by popular editors)
#!/usr/bin/python
# -*- coding: <encoding name> -*-
or
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
More precisely, the first or second line must match the regular
expression "coding[:=]\s*([-\w.]+)". The first group of this
expression is then interpreted as encoding name.
To aid with platforms such as Windows, which add Unicode BOM
marks to the beginning of Unicode files, the UTF-8 signature
'\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well
(even if no magic encoding comme ......
为了google的google appengine而学习python
发现他真的不如c语言好学,倒不是他难懂,而是相关文档太少,而且不规范。
也许是我的问题吧,我还是喜欢c语言的头文件,还有msdn的那种帮助。一眼就知道使用方法。
说c语言难学,可能是因为没有仔细看这两个东西吧 ......
2009-11-16
Collin Winter是Python社区一位颇具影响力的开发者,他曾是CPython项目的核心开发者之一、也曾是Unladen Swallow(见文末注释)的核心开发者,参与了很多Python项目的开发。近来传闻Google将在其新项目中限制Python的使用,为此有开发者(以K表示)在Google 论坛中公开询问了Collin Winter,Collin Winter就很多尖锐的问题做了解答。这篇帖子同时也吸引了很多高质量的跟帖。
K:我听说Google将在其新项目中限制Python的使用,无疑这将大大减少Python代码和Python得到的支持。这是否确有其事还是只是谣传?
Collin Winter:的确,Google将限制Python的应用因为:Python不如Java和C++快,线程占有、内存使用都很高在使用Python开发新系统的时候,我们深知如果负载增加了10倍或者100倍系统会怎样,开发出的服务会有多糟糕我想Python已经发展到了一个狭缝中,因此在选择时我们应当权衡其优点和缺点,也许开发人员使用Python会很有效率,但随着系统的增大却会遇到许多平台级的性能限制。
K:Unladen Swallow会改变这一切么?你的期望是什么呢?
Collin Winter:Unladen Swallow旨在尽可能地将Python用在更多它现在尚未涉足的地方,而且Unladen Swallow也并非 ......
import Queue, threading, sys
from threading import Thread
import time,urllib
# working thread
class Worker(Thread):
worker_count = 0
def __init__( self, workQueue, resultQueue, timeout = 0, **kwds):
Thread.__init__( self, **kwds )
self.id = Worker.worker_count
Worker.worker_count += 1
self.setDaemon( True )
self.workQueue = workQueue
self.resultQueue = resultQueue
self.timeout = timeout
self.start( )
def run( self ):
''' the get-some-work, do-some-work main loop of worker threads '''
while True:
try:
callable, args, kwds = self.workQueue.get(timeout ......
#!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
# encoding: utf-8
import sys, time
import thread
SLEEP_TIME = 0.0001
def run_benchmark(n, m):
# print(">> Python 2.5.1, stackless 3.1b3 here (N=%d, M=%d)!\n" % (n, m))
locks = [thread.allocate_lock() for i in xrange(n)]
firstP = cin = []
cin_lock_id = 0
for s in xrange(1, n):
seqn = s
cout = []
cout_lock_id = s
# print("*> s = %d" % (seqn, ))
thread.start_new_thread(loop, (seqn, locks, cin, cin_lock_id, cout, cout_lock_id))
cin = cout
cin_lock_id = cout_lock_id
else:
seqn = s+1
# print("$> s = %d" % (seqn, ))
t ......
目前为止,据我所知,在python中对象持久化有以下几种方法:
1. 使用(dbhash/bsddb, dbm, gdbm, dumbdbm 等)以及它们的"管理器"( anydbm )。只提供了 Python 字
符串的永久性储存. 提供一个类似字典和文件的对象,可以完成字符串的永久性存储。
2. 使用marshal和pickle来序列化python对象,并具备存储到介质上的功能。两者的区别在于:marshal只能处理简单的Python对象,包括数字、序列、映射、以及代码对象;而pickle还可以处理递归对象,被不同地方多次引用的对象,以及用户定义的类和实例。其中,pickle有一个C语言实现的版本——cPickle,具有更高的效率,建议使用cPickle。
3. 虽然pickle提供非常强大的功能了,已经可以满足我们大部分的需求了,但是,人类的需求是无止境的,光序列化不行啊,只用 pickle 不能解决命名和查找 pickle 文件这样的问题,要是可以对序列化的对象提供管理功能,支持并发访问就好了。因此,人们发明了shelve模块,它是前两者的综合。shelve模块使用 anydbm模块寻找合适的DBM模块,然后使用cPickle来完成对象存储转换过程。shelve模块允许对数据库文件进行并发的读访问,但不允许共享读/写访问。
4. 还有一种方案,是在I ......