473,378 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

pyparsing: odd listAllMatches behavior

I'm trying to write a parser for a simple query language (for a web
search form). Things are ok, except that if the first match is an
OR_clause, I only get one token back when I access by name, instead of
a ParseResults. See below. TIA!

from pyparsing import *

class Downcase(TokenConverter):
"""Converter to lower case all matching tokens."""
def postParse( self, instring, loc, tokenlist ):
return map( string.lower, tokenlist )

word=Downcase(Word(alphas))

phrase=Combine(Literal('"').suppress() + OneOrMore(word) +
Literal('"').suppress(),
adjacent=False, joinString=" ")

OR_clause=Literal('OR').suppress() + (word.setResultsName("OR_word",
listAllMatches=True) ^
phrase.setResultsName("OR_phrase",
listAllMatches=True))

term=OR_clause ^ \
word.setResultsName('word', listAllMatches=True) ^ \
phrase.setResultsName('phrase', listAllMatches=True)

query=OneOrMore(term)
Python 2.3.4 (#1, Sep 20 2004, 15:34:06)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright", "credits" or "license" for more information.
## working on region in file /usr/tmp/python-a7Bl5m...
r=query.parseString('foo OR bar')
r (['foo', 'bar'], {'OR_word': [('bar', 1)], 'word': [('foo', 0)]}) r.OR_word (['bar'], {}) r.word (['foo'], {})
r=query.parseString('OR bar foo')
r (['bar', 'foo'], {'OR_word': [('bar', 0)], 'word': [('foo', 1)]}) r.OR_word 'bar' r.word

'foo'

--
Peter Fein pf***@pobox.com 773-575-0694

Basically, if you're not a utopianist, you're a schmuck. -J. Feldman
Jul 18 '05 #1
0 924

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

Similar topics

1
by: Paul McGuire | last post by:
***This is of especial interest for those who are using the pyparsing module, and have defined grammars that make use of CaselessLiteral.*** One of the bugfix requests I recently got for...
5
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....
0
by: Paul McGuire | last post by:
I'm finishing a minor bug-fix release to version 1.2 for pyparsing, and have uploaded the pyparsing code to CVS on my SourceForge project page. There is one bug-fix that may change some behavior...
4
by: the.theorist | last post by:
Hey, I'm trying my hand and pyparsing a log file (named l.log): FIRSTLINE PROPERTY1 DATA1 PROPERTY2 DATA2 PROPERTYS LIST ID1 data1 ID2 data2
2
by: don pasquale | last post by:
hello, I'm using pyparsing and trying to parse something like: test="""Q(x,y,z):-Bloo(x,"Mitsis",y),Foo(y,z,1243),y>28,x<12,x>3""" and also have all comparison predicates in a separate list...
4
by: Bytter | last post by:
Hi, I'm trying to construct a parser, but I'm stuck with some basic stuff... For example, I want to match the following: letter = "A"..."Z" | "a"..."z" literal = letter+ include_bool := "+"...
13
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...
1
by: Steve | last post by:
Hi All (especially Paul McGuire!) Could you lend a hand in the grammar and paring of the output from the function win32pdhutil.ShowAllProcesses()? This is the code that I have so far (it is...
5
by: Paul McGuire | last post by:
I've just uploaded to SourceForge and PyPI the latest update to pyparsing, version 1.5.1. It has been a couple of months since 1.5.0 was released, and a number of bug-fixes and enhancements have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.