473,659 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pyparsing: crash on empty element

gry
[python 2.3.3, pyparsing 1.3]
I have:

def unpack_sql_arra y(s):
# unpack a postgres "array", e.g. "{'w1','w2','w3 '}" into a
list(str)
import pyparsing as pp
withquotes = pp.dblQuotedStr ing.setParseAct ion(pp.removeQu otes)
withoutquotes = pp.CharsNotIn(' ",{}')
parser = pp.StringStart( ) + \
pp.Literal('{') .suppress() + \
pp.delimitedLis t(withquotes | withoutquotes) + \
pp.Literal('}') .suppress() + \
pp.StringEnd()
return parser.parseStr ing(s).asList()

which works beautifully, except on the input: "{}". How can I neatly
modify the parser to return an empty list in this case?
Yes, obviously, I could say
if s=='{}': return []
It just seems like I'm missing some simple intrinsic way to get this
out of the parser. I am hoping to become more skillful in using the
wonderful pyparsing module!

-- George Young

Mar 6 '06 #1
1 1492
To indicate that a particular parse expression may be empty, pyparsing
includes the Optional element.

Change:
pp.delimitedLis t(withquotes | withoutquotes) + \

to:
pp.Optional( pp.delimitedLis t(withquotes | withoutquotes) ) + \

and I think this will have the desired effect.

-- Paul
<gr*@ll.mit.edu > wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.com.. .
[python 2.3.3, pyparsing 1.3]
I have:

def unpack_sql_arra y(s):
# unpack a postgres "array", e.g. "{'w1','w2','w3 '}" into a
list(str)
import pyparsing as pp
withquotes = pp.dblQuotedStr ing.setParseAct ion(pp.removeQu otes)
withoutquotes = pp.CharsNotIn(' ",{}')
parser = pp.StringStart( ) + \
pp.Literal('{') .suppress() + \
pp.delimitedLis t(withquotes | withoutquotes) + \
pp.Literal('}') .suppress() + \
pp.StringEnd()
return parser.parseStr ing(s).asList()

which works beautifully, except on the input: "{}". How can I neatly
modify the parser to return an empty list in this case?
Yes, obviously, I could say
if s=='{}': return []
It just seems like I'm missing some simple intrinsic way to get this
out of the parser. I am hoping to become more skillful in using the
wonderful pyparsing module!

-- George Young

Mar 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2348
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could. Or how can I replace the html &entities; in a string "blablabla&amp;blablabal&amp;balbalbal" with the chars they mean using re.sub? I found out they are stored in an dict . I though about this functionality:
2
2580
by: Peter Fein | last post by:
I'm trying to use pyparsing write a screenscraper. I've got some arbitrary HTML text I define as opener & closer. In between is the HTML data I want to extract. However, the data may contain the same characters as used in the closer (but not the exact same text, obviously). I'd like to get the *minimal* amount of data between these. Here's an example (whitespace may differ): from pyparsing import *
3
1462
by: Paul McGuire | last post by:
"The best laid plans o' mice an' men / Gang aft a-gley" So said Robert Burns (who really should do something about that speech impediment!). And so said I about 6 weeks ago, when I thought that I would leave pyparsing alone for awhile, after the 1.3.1 release. Well, here we are, and I'm announcing the 1.3.2 release, with some changes that I'd like to get released quickly. Here is the excerpt from the change log:
1
1858
by: Khoa Nguyen | last post by:
I am trying to come up with a grammar that describes the following: record = f1,f2,...,fn END_RECORD All the f(i) has to be in that order. Any f(i) can be absent (e.g. f1,,f3,f4,,f6 END_RECORD) Number of f(i)'s can vary. For example, the followings are allowed: f1,f2 END_RECORD f1,f2,,f4,,f6 END_RECORD Any suggestions?
13
2055
by: 7stud | last post by:
To the developer: 1) I went to the pyparsing wiki to download the pyparsing module and try it 2) At the wiki, there was no index entry in the table of contents for Downloads. After searching around a bit, I finally discovered a tiny link buried in some text at the top of the home page. 3) Link goes to sourceforge. At sourceforge, there was a nice, green 'download' button that stood out from the page. 4) I clicked on the download...
0
2040
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
1
353
by: Neal Becker | last post by:
I'm just trying out pyparsing. I get stack overflow on my first try. Any help? #/usr/bin/python from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList first_line = '' def main():
18
4712
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: from pyparsing import * grammar = OneOrMore(Word(alphas)) + Literal('end') grammar.parseString('First Second Third end')
4
5272
by: Marek Kubica | last post by:
Hi, I am trying to get this stuff working, but I still fail. I have a format which consists of three elements: \d{4}M?-\d (4 numbers, optional M, dash, another number) EMPTY (the <EMPTYtoken) (the <PAGEBREAKtoken. The line may contain whitespaces, but nothing else)
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.