Discussion:
[Pyparsing] How to get to a specific block of text I need
Malahal Naineni
2017-02-26 05:04:03 UTC
Permalink
I am trying to implement a config editor for a project. The config
syntax is very simple. It consists of "key value" pairs in blocks.
Blocks may have sub-blocks (only one level at this time).

An example:

export {
path = path1;
key2 = value2;
client {
clientid = value3;
key4 = value4;
}
client {
clientid = value6;
}
}

The "export" is uniquely identified by a specific "path" value. Same
is the case with "client" block inside the "export" block which is
identified by the "clientid" value.

Given an export path, I would like to fetch the corresponding export
block. Of course, I can easily match all exports and search each such
block for a matching "path". Similarly, for a given path and clientid,
I would like to get the corresponding "client" block quickly as there
may be 1000's of these sub-blocks in the export block.


Regards, Malahal.
Ralph Corderoy
2017-02-26 12:10:56 UTC
Permalink
Hi Malahal,
Post by Malahal Naineni
export {
path = path1;
key2 = value2;
client {
clientid = value3;
key4 = value4;
}
client {
clientid = value6;
}
}
...
Post by Malahal Naineni
Given an export path, I would like to fetch the corresponding export
block.
This sounds more like a Python programming problem than Pyparsing. You
want to build a `dict' indexed by an export's `path' as you're parsing.
https://docs.python.org/3/tutorial/datastructures.html#dictionaries

The value for a key would be a data of your design that describes an
export. Lookup would then be

exports['path1']
Post by Malahal Naineni
Similarly, for a given path and clientid, I would like to get the
corresponding "client" block quickly
Same here, except you have a dict of dicts.

clients['path1']['value3']

Or you might make the information available as part of your exports dict
if the value was an object with a `clients' attribute that was a dict.

exports['path1'].clients['value3']
--
Cheers, Ralph.
https://plus.google.com/+RalphCorderoy
Continue reading on narkive:
Loading...