Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35454

Re: Parsing files in python

From Kene Meniru <Kene.Meniru@illom.org>
Subject Re: Parsing files in python
Followup-To gmane.comp.python.general
Date 2012-12-24 06:35 -0500
Organization illom.org
References (1 earlier) <CAPTjJmrWAZbb-rmxyCfc3RerK75s4ar0RY2ZCuy5o6PBOVMm6w@mail.gmail.com> <50d82440.d441340a.3779.ffffe491@mx.google.com> <CAPTjJmqmxD383n34AotdzAm7P-Jq8j+eHtxNSzCPih0-_VgDsg@mail.gmail.com> <kb9avp$6d1$1@ger.gmane.org> <CAPTjJmpVwftxT5Di8pyvy-uyyULYY-6mF456YfwiYiTdUPXepQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1252.1356348917.29569.python-list@python.org> (permalink)

Followups directed to: gmane.comp.python.general

Show all headers | View raw


Chris Angelico wrote:

> On Mon, Dec 24, 2012 at 9:32 PM, Kene Meniru <Kene.Meniru@illom.org>
> wrote:
>> You are saying I can create a python module that can parse this file
>> format without using a system like python-ply? I know how to parse
>> strings using python but considering that text files that describe a
>> whole building may be quite large I thought perhaps the re module may not
>> be adequate.
> 
> Effectively, what you do is leverage the Python parser. Your script
> would look like this:
> 
> ------------possible user file content for parsing ------------
> # Boiler-plate to make this work
> from pypovray import *
> 
> # in the following the python interface program reads
> # the contents of the file "other.file" as if its content
> # were located at this point.
> import other.file
> 
> #In the following the python interface makes "snap_size" a
> #  global parameter
> snap_size = 10
> 
> 
> # In the following "buildingLevel" is a class (or function) that is
> #  called and passed the parameters in parenthesis.
> buildingLevel("FirstLevel", 3000)
> 
> # In the following "snapOffset" is a class that is
> #  called and passed the parameters in parenthesis.
> snapOffset("Closet-S1_r1", "Closet-S2_r3", (0,0,0))
> ------------end of user file content
> 
> Note the extreme similarity to your original example. Everything
> between the two snip-lines is perfectly legal Python code. (The
> semantics of a Python import aren't quite the same as a C preprocessor
> #include, so that might need a little tweaking, depending on what you
> wanted to achieve there. Possibly "from other.file import *" would do
> it.) Instead of writing a file parser, with all the complexities that
> that entails, all you need to write is a set of functions/classes that
> can be invoked.
> 
> The only part that doesn't work cleanly is the vector, since its
> syntax doesn't work in Python. You'll need to use round brackets
> instead of angle ones, as in the above example, and on output to
> Python, translate them. But that's fairly straight-forward, and by
> this method, you get *everything else* done for you - parsing, nesting
> of function calls, the entire Python standard library... the works.
> 
> ChrisA

Thanks. This makes sense and it is something I can start right away porting 
my code. Sincerely glad I voiced my thoughts. The import directive will have 
to be tackled later but that is not for at least a year or so :-)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Parsing files in python Kene Meniru <Kene.Meniru@illom.org> - 2012-12-24 06:35 -0500

csiph-web