pyp - Simple Preprocessor for Python
Missing C's '#include' statement? Yearn no longer.
pyp is a simple preprocessor
that interprets markups (eg '##include') in python files, and generates
expanded python files from these markups.
Why?
OK - here's an example.
If you're familiar with the
PythonCard
graphical toolkit, you'll know that its resource editor works with files
in the form
myresources.rsrc.py.
Each of these files contain a Python data literal, being a dict
containing dicts and lists of the various resource components (windows,
buttons, fields etc).
Why is this an issue?
Well, you could just go with standard PythonCard practice, and let your
window class inherit the logic which assumes that if your module's name
is
fred.py, your resource file
will be called
fred.rsrc.py,
and load that in. But this has 2 problems:
- If you want to run your python program from outside of the
directory where fred.py resides,
PythonCard's code may not find fred.rsrc.py,
which will cause your program to fail.
- PythonCard loads resources at run-time. If you want to bundle your program
into a standalone executable for Windows, using Py2EXE or McMillan's
Installer, this could be a problem. Paths can be confused
(result: program failure), or you may simply not want your resource defs
to be sitting there in plain view.
My solution is to 'pre-process' my
Python code, and generate a Python module with the resource definitions
included in it.
For example, here's my version of PythonCard's minimal.py demo program:
from PythonCardPrototype import model
minimalRes = \
##include minimal.rsrc.py
class Minimal(model.Background):
def on_menuFileExit_select(self, event):
#pdb.set_trace()
self.Close()
if __name__ == '__main__':
app = model.PythonCardApp(Minimal, None, minimalRes)
app.MainLoop()
Notice the '##include minimal.rsrc.py'. This
works just like the #include
statement in C, recursive even.
When I run pyp over this code,
it generates a python file with the resource definitions included.
Result is a file that can be run from anywhere, and can be easily
packaged into a standalone EXE. Problem now nonexistent.
So as you can see, only '##include' is supported at the moment. Maybe
I'll add some more directives, perhaps even a '#define', '#if', '#ifdef'
etc. But even as it is, pyp can prove useful in many situations.
Download pyp here.
If you come up with any patches, suggestions etc, please email them to
me at david@rebirthing.co.nz.
When mailing me, you'll get a 'please confirm' response to my first
message. Reply to that, and you'll be permanantly added to my whitelist.
Privacy Policy