May 29, 2007 at 7:59 pm · tags: Concurrency Python
Generators are great if you ask me and so are the power ups that Python 2.5 adds to them. Since the last version of Python you have:
- yield as an expression so you can store a returned value
- values can be sent to the generator with gen.send(value), so you can somewhat handle the generator from the outside easily
- exceptions can be raised inside the generator from the outside with gen.throw()
- the generation process can be stopped anytime with gen.close()
This way Python 2.3 simple generators turn into full fledged coroutines.
Recently a library called multitask appeared on PyPi index. With multitask you can do cooperative multitasking and async I/O only with bare generators.
So fifteen minutes later I rewrote the classic producer/consumer example with it.
The source code is not really different from the generators only example I demoed last month:
import multitask
class Producer(object):
# removed for brevity #
class Consumer(object):
def __init__(self, buffer, name):
self.buffer = buffer
self.name = name
def run(self):
while not self.buffer.empty():
# task will be resumed after an element is available in the queue
data = (yield self.buffer.get())
print “%s has got %s from the buffer.” % (self.name, data)
# the task will be resumed after 1.5 seconds
yield multitask.sleep(1.5)
print “%s is doing something with %s” % (self.name, data)
def main():
buffer = multitask.Queue()
prod = Producer(buffer, “Producer”)
cons = Consumer(buffer, “Consumer”)
multitask.add(prod.run())
multitask.add(cons.run())
multitask.run()
This time, anyway, the scheduler is provided by multitask which basically iterates on the tasks and sends in the values getting the outputs. Generators can make your code really simple
Here’s the full example: prodcons_multitask.py
May 29, 2007 at 3:37 pm · tags: Python Python SVN
Towards Python 3.0
There’s a new shiny feature: you can track what’s going to disappear in Python 3.x with the -3 switch on the command line. See:
rhymes@groove % ./python.exe -3
Python 2.6a0 (trunk, May 29 2007, 13:55:02)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> callable(int)
__main__:1: DeprecationWarning: callable() not supported in 3.x
True
Things like coerce(), apply(), callable(), execfile(), reduce(), reload(), dict.has_key() and more have been deprecated.
Python 2.6 supports the new Python 3.0 exception syntax:
>>> try:
… raise Exception(“something went wrong”)
… except Exception as t:
… print t
…
something went wrong
Notice the as keyword. You can know it all by reading the PEP 3110.
Modules
- The already deprecated rgbimg module is no longer in the standard library and posixfile has been deprecated too, see:
>>> import posixfile
__main__:1: DeprecationWarning: The posixfile module is deprecated; fcntl.lockf() provides better locking
gopherlib has been also removed and so is the gopher support from urllib/urllib2.
Platform specific macfs, panel, amoeba and stdwin modules have been removed.
plat-freebsd2 and plat-freebsd3 directories have been removed.
httplib.HTTPSConnection supports a timeout parameter for the underline socket.
Some support for swig modules has been added in distutils.
.bz2 extension has been added to mimetypes module:
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import mimetypes
>>> mimetypes.guess_type(“pcre-7.1.tar.bz2″)
(None, None)
Python 2.6a0 (trunk, May 29 2007, 13:55:02)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import mimetypes
>>> mimetypes.guess_type(“pcre-7.1.tar.bz2″)
(‘application/x-tar’, ‘bzip2′)
tarfile module supports Unicode input names. A couple of attributes have been added too.
urllib.ftpwrapper supports an optional timeout parameter.
shlex.split() now has an optional “posix” parameter to work as close as possible as POSIX rules.
Other
All the function objects now have a set of attributes that mirrors func_* into __ * __, see:
>>> def f(): pass
…
>>> f.func_name
‘f’
>>> f.__name__
‘f’
>>> dir(f)
[‘__call__’, ‘__class__’, ‘__closure__’, ‘__code__’, ‘__defaults__’, ‘__delattr__’, ‘__dict__’, ‘__doc__’, ‘__get__’, ‘__getattribute__’, ‘__globals__’, ‘__hash__’, ‘__init__’, ‘__module__’, ‘__name__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__str__’, ‘func_closure’, ‘func_code’, ‘func_defaults’, ‘func_dict’, ‘func_doc’, ‘func_globals’, ‘func_name’]
May 19, 2007 at 2:09 am · tags: Python
Kshipra Singh of Packt Publishing kindly sent me a review copy of the brand new book about CherryPy 3.0: a really nice and self contained HTTP framework.
The author of the book is Sylvain Hellegouarch and I think he did the right thing writing it. It’s perfect if you want to start with CP or want to get your feet wet not knowing anything much about web development.
Let me say that I enjoyed reading this book. It doesn’t pretend to be the bible about anything. It’s just a good introduction to CherryPy and the issues around web development as a whole. All in 250 pages. Really cool if I may say so.
The things I expected to be slightly better are fonts and layout but I can live with that.
The book is divided in two main sections: the first four chapters are all about the framework helping you start developing web apps from scratch. The second section of the book is a huge use case: the development of a photo-blog application.
The first chapter talks about the history of CherryPy (CP from now on), its community, its strengths and what’s there beyond it.
The installation chapter is a pretty thorough explanation of the many ways you can have CP working on your operating system.
The overview chapter is a list of what CP is: a web application server, an HTTP server, an easily configurable tool, an engine to publish objects. CP also has a lot of separate modules to deal with the various aspects of web development: caching, auto-reloading, character handling, test coverage, low level HTTP support, HTTP authentication, profiling, sessions (memory, file system, DBMS out of the box), serving of static resources, tidy support, WSGI, XML-RPC and more.
The last chapter of the first part goes into how CP really works: its HTTP support, multiple servers, URI dispatching, virtual hosting support, a list of tools (kinda like middlewares in Django parlance) and how to create them. It explains also clearly how to embed a WSGI application into CP and vice-versa.
The fifth chapter contains a nice introduction about three major ORM tools: SQLObject, SQLAlchemy and DejaVu. The author lets code speak for itself and the choice to develop the sample application is the latter.
The sixth chapter is a nice introduction to REST and how to properly design a web application interface. I’m not really sure though that introducing Atom Publishing Protocol is a right choice, maybe it’s too much meat for the newbie (and it’s not used later in the book anyway).
The seventh and eighth chapter are the ones I liked less. I didn’t really enjoy all the magic in the front end code. I know Ajax it’s a must on any recent book about web development but the use case is kinda diverted towards it only to give a reason to exists to the eighth chapter. There’s too much of client code I think.
The ninth chapter is all about testing your web app and that’s a must. If it’s not tested it doesn’t work, right? Ok… almost :-). Unittest, doctest, web testing, functional testing, stress testing, testing with Selenium. There’s a lot of stuff going on in this chapter. Make sure to read it before trying to develop any real world application.
The last and tenth chapter is a nice overview of deployment options.
That’s all. Before this book CP didn’t really came under my radar but I’m sure it’s a viable option to anyone starting developing web apps
May 18, 2007 at 12:22 pm · tags: PyConIt Python
The first Python conference in Italy, ever, is finally approaching. Our aim is to promote, spread and get to know people in the Python world.
The conference will be held in Florence on June, the 9th and 10th.
If you understand Italian and are remotely interested in Python just come. I bet it will be great.
The conference is divided in two parallel tracks: the first one is a tutorial track and the second one is based on advanced material and use cases.
We’ll have a lot of speakers and a lot of known people in Italy among them (like the author of psycopg). We’ll also have two keynotes: one by Alex Martelli (Management of software development and lessons from opensource) and one by Marco Pesenti Gritti of Gnome fame talking about OLPC.
More Python in Italy means more fun and maybe more jobs
May 12, 2007 at 2:05 am · tags: Python Python SVN
logging.SMTPHandler now supports authentication credentials through (username, password) tuple as the last argument of the constructor.
logging.SocketHandler.makeSocket has an optional timeout argument.
Added logging.WatchedFileHandler to better support external log file rotation. Useful only in Unix/Linux environments.
BaseException.message has been deprecated (see PEP 352 for the rationale).
The popen2 module and os.popen* are now deprecated in favor of the subprocess module.