
...another project with a dire need for a better logo
| Author: | Rick Copeland |
|---|---|
| Date: | April 9, 2009 |
Code quoting:
defcode foo:
print "hello, world"
Macros (“import-time code”):
$from foo import bar
$for x in ...:
$:
...
$macro_call(a,?<b>,c)
Common example for Lisp/Scheme: while:
(defmacro while (expression &body body)
...)
I told him we already got one!
Example: collections.namedtuple in Python 2.6
unittest.TestCase factories
One TestCase per browser
Example:
log.debug('Some value: %s',
expensive_computation(...))
Possible (ugly) fix:
if loglevel >= logging.DEBUG:
log.debug(...)
Defines code to be run that is used to generate the code that will be imported
Always denoted with a $ token:
$import namedtuple
$namedtuple(...)
$:
# entire
# block
# to be run
# at import time
Rather than executing code, create an object that represents some code
Example:
defcode foo:
print "hello"
Becomes:
foo = _mpy.Block()
foo.add('print "hello"', locals())
Can also use inline quoting ?<expr>
Enable MetaPython import hook:
import metapython
metapython.install_import_hook()
Now create files with the .mpy extension
These can be imported just like .py files
Original Code
def namedtuple(typename, field_names):
# ... lots of setup
template = '''class %(typename)s(tuple):
... lots of text ...''' % locals()
for i, name in enumerate(field_names):
template += '...'
# ... snip ...
try:
exec template in namespace
except SyntaxError, e:
raise SyntaxError(e.message...)
return result = namespace[typename]
Original Usage:
Point = namedtuple('Point', 'x y')
Easy to get wrong:
MyPoint = namedtuple('Point', 'x y')
New Code
def namedtuple(typename, *field _names):
...
defcode result:
class $<typename>(tuple):
...
return result
New Usage
$namedtuple(?Point, ?x, ?y)
Version 0.1 uses Jinja2 Templates for Code
Version 0.2 uses more consistent syntax
Documentation, Code, etc