Re: Parsing a search string
Reinhold Birkenfeld wrote:
Freddie wrote:
Happy new year! Since I have run out of alcohol, I'll ask a question that I
haven't really worked out an answer for yet. Is there an elegant way to turn
something like:
> moo cow "farmer john" -zug
into:
['moo', 'cow', 'farmer john'], ['zug']
I'm trying to parse a search string so I can use it for SQL WHERE constraints,
preferably without horrifying regular expressions. Uhh yeah.
The shlex approach, finished:
searchstring = 'moo cow "farmer john" -zug'
lexer = shlex.shlex(searchstring)
lexer.wordchars += '-'
poslist, neglist = [], []
while 1:
token = lexer.get_token()
# token is '' on eof
if not token: break
# remove quotes
if token[0] in '"\'':
token = token[1:-1]
# select in which list to put it
if token[0] == '-':
neglist.append(token[1:])
else:
poslist.append(token)
regards,
Reinhold
Thanks for this, though there was one issue:
>>> lexer = shlex.shlex('moo cow +"farmer john" -dog')
>>> lexer.wordchars += '-+'
>>> while 1:
... tok = lexer.get_token()
... if not tok: break
... print tok
...
moo
cow
+"farmer
john"
-dog
The '+"farmer john"' part would be turned into two seperate words, '+"farmer'
and 'john"'. I ended up using shlex.split() (which the docs say is new in
Python 2.3), which gives me the desired result. Thanks for the help from
yourself and M.E.Farmer :)
Freddie
>>> shlex.split('moo cow +"farmer john" -"evil dog"')
['moo', 'cow', '+farmer john', '-evil dog']
>>> shlex.split('moo cow +"farmer john" -"evil dog" +elephant')
['moo', 'cow', '+farmer john', '-evil dog', '+elephant']
--
http://mail.python.org/mailman/listinfo/python-list
ANN: GallerPy 0.5.0
GallerPy is a fairly basic dynamic web gallery written in Python and uses the Python Imaging Library. It is licensed under the terms of the BSD License. Features include: * Fluid CSS layout * SCGI support * Other exciting stuff GallerPy is available for download from the MadCowDisease web site: http://www.madcowdisease.org/index.py/GallerPy -- http://mail.python.org/mailman/listinfo/python-list
ANN: GallerPy 0.6.0
GallerPy is a fairly basic dynamic web gallery written in Python and uses the Python Imaging Library. It is licensed under the terms of the BSD License. Features include: * Fluid CSS layout * SCGI support * Other exciting stuff GallerPy is available for download from the MadCowDisease web site: http://www.madcowdisease.org/index.py/GallerPy Changes in this release: * Added a 'header_file' option, which displays the contents of a text file if it exists in the current directory. See gallerpy.conf for details. * Added GZIP encoding support. See gallerpy.conf for details. * Added 'croncheck.py'. Edit the CHECKME section under the license, then run it regularly from your crontab to restart your SCGI daemons. Very useful if your web host ever gets restarted or whatever. * Updated the default template and some bits and pieces of internal code to use proper indenting, and also valid XHTML 1.1. * Updated the CSS to use 'safer' sizing values. Taller folder/thumbnail divs also help with IE5/6 and it's weird layout engine. * Changed scgi_daemon.py to use -L for log filename so it matches -P. * Fixed our except hook. We no longer need the nasty-looking cgitb, yay! * Fixed the pidfile writing/unlinking in scgi_daemon.py. * Fixed generate_thumbnails so it catches exceptions properly. * Fixed the encoded and 'normal' filenames for prev/next image being switched in image display mode. * Fixed image display not having prev/next links if there is a non-image as the prev/next file. * Various minor cleanups. -- http://mail.python.org/mailman/listinfo/python-list
Parsing a search string
Happy new year! Since I have run out of alcohol, I'll ask a question that I haven't really worked out an answer for yet. Is there an elegant way to turn something like: > moo cow "farmer john" -zug into: ['moo', 'cow', 'farmer john'], ['zug'] I'm trying to parse a search string so I can use it for SQL WHERE constraints, preferably without horrifying regular expressions. Uhh yeah. From 2005, Freddie -- http://mail.python.org/mailman/listinfo/python-list
ANN: blamehangle 0.1.0
(sorry if this appears several million times, Australian usenet servers are useless, and Newshosting seems to hate me) blamehangle is Yet Another IRC Bot Framework. Started in 2003 as a replacement for a nasty perl monster, we're just now getting around to releasing it. Useful features include: * Asynchronous HTTP client. * Threaded database lookups. * Threaded DNS lookups. * Multiple IRC network support. * Possibly understandable plugin deveopment model. * Useful collection of plugins. * Sometimes actively developed! blamehangle is available for download from the MadCowDisease web site: http://www.madcowdisease.org/mcd/blamehangle Or via Subversion at: svn://svn.madcowdisease.org/blamehangle/tags/0.1.0 svn://svn.madcowdisease.org/blamehangle/trunk (sometimes broken) -- http://mail.python.org/mailman/listinfo/python-list
ANN: blamehangle 0.1.0
(sorry if this appears several times, Australian usenet servers are atrocious) blamehangle is Yet Another IRC Bot Framework. Started in 2003 as a replacement for a nasty perl monster, we're just now getting around to releasing it. Useful features include: * Asynchronous HTTP client. * Threaded database lookups. * Threaded DNS lookups. * Multiple IRC network support. * Possibly understandable plugin deveopment model. * Useful collection of plugins. * Sometimes actively developed! blamehangle is available for download from the MadCowDisease web site: http://www.madcowdisease.org/mcd/blamehangle Or via Subversion at: svn://svn.madcowdisease.org/blamehangle/tags/0.1.0 or svn://svn.madcowdisease.org/blamehangle/trunk (for sometimes broken trunk) trunk is recommended, as there are several minor bugs fixed already. oops! -- http://mail.python.org/mailman/listinfo/python-list
ANN: GallerPy 0.7.0
GallerPy is a fairly basic dynamic web gallery written in Python and uses the Python Imaging Library. It is licensed under the terms of the BSD License. Features include: * Fluid CSS layout * SCGI support * Other exciting stuff GallerPy is available for download from the MadCowDisease web site: http://www.madcowdisease.org/mcd/GallerPy Or via Subversion: svn://svn.madcowdisease.org/GallerPy/tags/0.7.0 svn://svn.madcowdisease.org/GallerPy/trunk (occasionally broken) Changes in this release: * Add 'show_header' and 'show_footer' options to control the output of HTML header and footer information. * Add 'root_local' and 'root_web' options to allow easier embedding of GallerPy into other applications. * Include .BMP and .JPE in the list of recognised extensions. * Include '.svn' in the default hide_dirs listing. * Strip empty "" tags from output. * Change how we do top padding of thumbnail images so it works in that nasty cow, Internet Explorer. RAGE. * Change the default header_file from index.txt to header.txt, since index.txt is a bit confusing. * Fix ShowError() not working properly if GallerPy is running embedded in something else. -- http://mail.python.org/mailman/listinfo/python-list
