customized code blocks (suiltes)

Amir Hadar amir at clockwise3d.com
Wed Mar 12 12:53:51 EST 2003


Hi all

I have programmed a patch to the python 2.2.2 that extends the syntax of the
python language to contain customized code block. The patch enable a new
keyword in the python syntax which is cwblock. This keyword is used for
customized code blocks.
A customized code block is a block which it execution is controlled by a
block handler.

You can download the patch and examples from this link:
http://www.clockwise3d.com/samples/cwblock.tar.gz

Example:

def ForkBlockHandler(block):
    t = Thread(target = block)
    t.start()

This is a block handler that execute its block in a new thread. Here is how
to use this new block handler

cwblock ForkBlockHandler:
    print currentThread().getName()
    print "yes, this is not the main thread"


This is very similar to the work of  Holger Krekel. Information about his
work can be found in http://codespeak.net/moin/moin.cgi/IndentedExecution.
But there is a MAJOR difference. With this type of handlers that receive the
block as a function, one can control not only what will happen before and
after the block execution but also WHEN and IF the block execution will take
place. In the example given the block is executed in a new thread. This can
only be done with this kind of block handlers. Also I think that the syntax
(which is negotiable) does not break the syntax style of python.

I think that this kind of customization made by the programmer gives python
an edge on other languages. The power that the programmer has with this tool
not only create a clearer and more understandable code but also prevent
bugs. The most common use of the customized blocks is the LockBlk which
locks a lock before running the block and unlock it at the end. Using the
LockBlk can prevent bugs of not releasing the lock at the end of the
critical section. There are infinite uses for the custom blocks and I think
this is a feature that does not exists Java or C++. In Smalltalk however
there is a code block with free variables that can resemble in some way to
this custom code block. But the syntax of the custom blocks is clean and is
very readable (what cannot be said about Smalltalk code). It is not
sophisticated and is not hiding complex code, but it is a very simple
statement that the programmer know exactly what will happen without thinking
mach.

The way I have implemented it is by using a pre-processor to the python
parse tree. I needed it to work with the internal compiler and with the
compiler module written in python. Therefore the parser was the best entry
point. I know that a real implementation would have to go through the
compiler.c but I didn't have the time to do so. This implementation answers
all my needs and I cant wait to start using it.

I wanted to share my revelation of this new programming style with you all,
and I hope maybe this kind of programming will be part of python in future
versions. Those of you who liked this style please download the patch and
try it.
http://www.clockwise3d.com/samples/cwblock.tar.gz

Any comment / bugs can be sent to amir at clockwise3d.com

Regards,
    Amir Hadar






More information about the Python-list mailing list