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


Groups > comp.lang.python > #35452

Re: Parsing files in python

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'syntax': 0.03; 'output': 0.04; '------------': 0.07; 'method,': 0.07; 'parameter': 0.07; 'parsing': 0.07; 'works.': 0.07; 'python': 0.09; 'brackets': 0.09; 'library...': 0.09; 'semantics': 0.09; 'subject:files': 0.09; 'subject:python': 0.11; '(the': 0.15; 'dec': 0.15; '"from': 0.16; '24,': 0.16; 'angle': 0.16; 'cleanly': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'invoked.': 0.16; 'nesting': 0.16; 'parser.': 0.16; 'preprocessor': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'example.': 0.17; 'saying': 0.18; '(or': 0.18; 'module': 0.19; 'code.': 0.20; 'translate': 0.20; 'parameters': 0.20; 'import': 0.21; 'fairly': 0.21; 'parse': 0.22; 'this:': 0.23; 'script': 0.24; 'header:In-Reply-To:1': 0.25; 'possibly': 0.27; 'message- id:@mail.gmail.com': 0.27; 'interface': 0.27; "doesn't": 0.28; 'there.': 0.28; 'leverage': 0.29; 'class': 0.29; 'function': 0.30; 'located': 0.31; 'file': 0.32; 'skip:- 10': 0.32; "aren't": 0.33; 'ones,': 0.33; 'to:addr:python-list': 0.33; 'point.': 0.33; 'skip:- 20': 0.34; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'text': 0.34; 'done': 0.34; 'pm,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'wanted': 0.36; 'two': 0.37; 'passed': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'little': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'your': 0.60; "you'll": 0.62; 'between': 0.63; 'legal': 0.65; 'calls,': 0.84; 'effectively,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=1VmrSt2mmdD6Fp9kYnQq5dYjfKdUn+6ubhPwNbDCprk=; b=vPWEE9HkUazmyCr/hmE8VCNDpXxlWz/LtPISQgiYLfSLh9HvH5JsVCe9wVD6IKFiXW lAJEjt11w8Us14ImIffakYAHnpmEvQM+JHQ/Z9mxxm29e0kJZsc4lU4CJaTQaLHzIZcU JGk5hoiKNYHY69L+zDfAUJjYB43xuix8oelLAWfFuxP1l9dvzslTzvUMiLJXF3ncbxuV L16hZOLTpAVVzWI4izn6EojocEB/K3HnE65UYIpyEtWIUpKkfp+iPh/5nHJuBEUftsUg BxDx9j5yXbsgfy924su0i0+F4cxCz8BTsWuGRg6M0QeA6Dy3C4x/LLZr8BAoh6oEikAv MJZQ==
MIME-Version 1.0
In-Reply-To <kb9avp$6d1$1@ger.gmane.org>
References <20121223171955.GB32535@ifeobi.illom.net> <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>
Date Mon, 24 Dec 2012 21:56:18 +1100
Subject Re: Parsing files in python
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1250.1356346587.29569.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356346587 news.xs4all.nl 6947 [2001:888:2000:d::a6]:58941
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35452

Show key headers only | View raw


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

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


Thread

Re: Parsing files in python Chris Angelico <rosuav@gmail.com> - 2012-12-24 21:56 +1100

csiph-web