|
|
Documentation
Instead of an explicit documentation, let's have a look just at the
example files shipped with the project.
File example.pyd
#
# example.pyd -- demonstration of plind files
#
# define a string
doc 'This is a little demonstration'
# define a tuple
proportions tuple
0.5
((5 ** .5) - 1) / 2
# define a list
languages list
'C'
'C++'
'Python'
# you may also specify this
otherlanguages 'Basic Fortran Perl'.split()
# define a dictionary
diameters dict
'Mercury' : 4878.
'Venus' : 12103.6
'Earth' : 12765.28
'Mars' : 4794.4
'Jupiter' : 142984.
'Saturn' : 120536.
'Uranus' : 51200.
'Neptune' : 49532.
'Pluto' : 2274.
# the addition feature
addingdict dict
strval : 'Hello'
strval :+ ', world!'
intval : 89
intval :+ 55
# a class `Movie' has to be defined here
myfavourite Movie
title 'Rear Window'
director 'Alfred Hitchcock'
producer 'Alfred Hitchcock'
screenplay 'John Michael Hayes, novel by Cornell Woolrich'
actors dict
'James Stewart' : 'L. B. Jeffries'
'Grace Kelly' : 'Lisa Feremont'
'Raymond Burr' : 'Lars Thorwald'
length 112
# using functions:
anothermovie : makeinstance( Movie, title = 'One, Two, Three', \
director = 'Billy Wilder')
# an include example
chicken dict
!include 'chicken.pyd'
# another include example
tracks list
!include 'highvoltage.pyd'
#
# ... and here some sophisticated definition applications. (Don't be
# frightened: the complicated part actually is the TeX code.)
#
!define texsum = r'$\sum_{n=0}^{\infty}{%s}^n$'
excercise1 : 'Calculate ' + texsum % r'(1\over2)' + '!'
# doing it with a lambda function
!define texsuml = lambda x: r'$\sum_{n=0}^{\infty}{%s}^n$' % x
excercise2 : 'Calculate %s!' % texsuml( r'(1\over3)')
# To define funcions of lists, dicts or classes, use the genreplace
# mechanism.
!define planexcontact = Contact
name '@name@'
company 'Planet Express Ltd.'
phone '030/123456-@ext@'
!define atplanex = lambda name, ext: \
genreplace( planexcontact, { '@name@' : name, '@ext@' : ext })
mycontacts list
atplanex( 'Philip J. Fry', '12')
Contact
name 'Kif Kroker'
phone '030/987654321'
atplanex( 'Hermes Conrad', '37')
# vim:syntax=python
# Local Variables:
# tab-width: 4
# mode: Python
# End:
File chicken.pyd
#
# chicken.pyd -- some useful facts about chicken
#
!tabsize 4
title 'Chicken Run'
length 84
characters dict
farmers tuple
'Mr Tweedy'
'Mrs Tweedy'
chicken list
'Ginger'
'Bunty'
'Babs'
'Mac'
'Rocky'
'Fowler'
rats tuple
'Nick'
'Fetcher'
# vim:syntax=python tabstop=4
# Local Variables:
# tab-width: 4
# mode: Python
# End:
File highvoltage.pyd
#
# highvoltage.pyd -- tracks of an LP
#
# This file contains a list and so has to be !included. (It cannot be
# read in with the `indflies' functions.)
'It\'s a Long Way to the Top (If You Wanna Rock \'N\' Roll)'
'Rock \'N\' Roll Singer'
'Jack'
'Live Wire'
'T.N.T.'
'Can I Sit Next to You Girl'
'Little Lover'
'She\'s Got Balls'
'High Voltage'
# vim:syntax=python
# Local Variables:
# tab-width: 4
# mode: Python
# End:
File demo.py
'''\
demo.py -- demonstrate indented files
'''
version = '1.0'
copyright = '''\
Copyright (C) 2004 Bertram Scharpf, <software@bertram-scharpf.de>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
'''
license = '''\
This file is part of the project:
`Plind' -- Python-like indented data
%s
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Or you can find it at: <http://www.gnu.org/copyleft>.
''' % copyright
bugs = '''\
Report bugs to <software@bertram-scharpf.de>.
'''
class Movie:
pass
class Contact:
pass
class Class:
pass
# First, we create a dictonary that contains all classes that are used
# in the datafile. This is for speed and for security reasons; we
# could also hand over the whole `globals()' dictonary.
classes = {}
for k, v in vars().items():
if type( v) == type( Class):
classes[ k] = v
del k, v
# This function puts the path of the current modules file name in
# front of all filenames given as arguments. This makes it easy to
# find your data when the module is somewhere else as the place you
# execute it from.
def under_modpath( *l):
import os
p = os.path
mp = p.abspath( p.dirname( __file__))
if len( l) == 1:
return p.normpath( p.join( mp, l[ 0]))
else:
return [ p.normpath( p.join( mp, x)) for x in l]
# Our input file is `example.pyd', it will be cached in `example.pyp',
# but only if this module is imported, not if it is executed as a
# standalone file.
infile, cachefile = 'example.pyd', 'example.pyp'
def readindfile():
i, c = under_modpath( infile, cachefile)
import indfiles
if __name__ == '__main__':
return indfiles.readfile( classes, i)
else:
return indfiles.readfile( classes, i, c)
data = readindfile()
# Now, `data' contains the data given in the file `example.pyd' and in
# those included. Only classes mentioned in `classes' are recognized.
# Finally, we write out our dictonary as another Python-like indented
# file, that could be re-scanned.
outfile = 'demo.pyd'
def produces():
'Give the names of the files produced by this module.'
return [ cachefile, outfile]
def main():
'Write the data out to a new file.'
indfiles.IndentedOut( outfile, data)
print '`%s\' written.' % outfile
# read it again ...
samedata = indfiles.readfile( classes, outfile)
if __name__ == '__main__':
main()
<previous next>
|