ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: easy file parsing
Submitter: Michael Haimes (other recipes)
Last Updated: 2006/09/18
Version no: 1.2
Category: Files

 

2 stars 1 vote(s)


Description:

This is useful for making easy and human to write definition files for whatever use. For example, lets say you are writing a simulator and you need an easy, human way to define parts about a given world like light sources, obstacles, and dimensions. see the discussion for this example fleshed out in code.

Source: Text Source

#parse a python file in a given environment, and get out some results
from os import dirname

def parseFile(filename, envin, envout = {}):
	exec "from sys import path" in envin
	exec "path.append(\"" + dirname(filename) + "\")" in envin
	envin.pop("path")
	lines = open(filename, 'r').read()
	exec lines in envin
	returndict = {}
	for key in envout:
		returndict[key] = envin[key]
	return returndict

Discussion:

For the example above, You might write something like the following:

from parsefile import parseFile

class World:

def dimensions(self, width, height):
self.width = width
self.height = height

def __init__(self, filename):
self.lightsources = []
self.obstacles = []
envin = {}
envin['dimensions'] = self.dimensions
envin['newobstacle'] = lambda x,y: self.obstacles.append((x,y))
envin['newlightsource'] = lambda x,y: self.lightsources.append((x,y))
parseFile(filename, envin)


If you were to then call something like World(file) in the runtime of your program where file is as follows:
####################
dimensions(4, 5)
newlightsource(1, 2)
newlightsource(2, 3)
newobstacle(0, 1)
####################

You will end up with an instance of class World with the appropriate data, thus it is simple to create new ways to specify data for your program



Add comment

Number of comments: 5

Oh my, N N, 2006/07/27

        lines = open(filename, 'r').readlines()
	total = ""
	for line in lines:
		total += line
Is a rather long way to spell:
       total=open(filename,'r').read()

Add comment

Michael Haimes, 2006/07/31
very true.. fortunately, it would only eat up a humanly unidentifiable amount of processor time unless the file was a ridiculous length. I made the change though
Add comment

Can't get this example working, Ilan Copelyn, 2006/09/18
Sorry - I'm new to Python.

I get:-

    envin['dimensions'] = dimensions
NameError: global name 'dimensions' is not defined
And if i change the line so the function name "dimensions" is qualified by the class "World", then I get problems when trying to exec the text read from the file.txt:-
    exec lines in envin
  File "", line 2, in ?
TypeError: unbound method dimensions() must be called with World instance as first argument (got int instance instead)
BTW I'm using Python 2.4

Add comment

Michael Haimes, 2006/09/18
again, a typo on my end. It should have been self.dimensions instead of dimensions the dimensions method is indeed a method of the class World, but Python doesn't have static methods like java. Plus, this method needs an instance of world in order to edit instance data like width and height. the self keyword in Python is equivalent to the this keyword in Java, and just refers to the instance of the class that the method was called in.
Add comment

Thanks, Ilan Copelyn, 2006/09/21
that did the trick cheers
Add comment



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.