On May 11, 6:20 am, fluence <ndudfi...@gmail.com> wrote: > @Paul > > I have been having a play with [py]parsing. What a nifty little > library! > > I read those 2 free tutes and liked what I saw so bought a > subscription to safari just so I could read your short cut. >
Glad to hear that pyparsing is giving you a jump start! I downloaded sqlalchemy, and eventually got the parameters straight to call your code (passing the search string, the "c" attribute of a Table, followed by a list of column names). Here's what I got: from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey metadata = MetaData() prod_table = Table('product', metadata, Column('id', Integer, primary_key=True), Column('name', String), Column('color', String), Column('size', String), Column('material', String), ) print search_fields_like("RED OR GREEN AND NOT BLUE", prod_table.c, "color size".split()) Gives: product.color LIKE :color_1 ESCAPE '\\' OR product.size LIKE :size_1 ESCAPE '\\' OR (product.color LIKE :color_2 ESCAPE '\\' OR product.size LIKE :size_2 ESCAPE '\\') AND NOT (product.color LIKE :color_3 ESCAPE '\\' OR product.size LIKE :size_3 ESCAPE '\\') (Where do the parsed values, like RED, GREEN, and BLUE go? You may at some point need to go beyond just "Word(alphas)" for search terms, such as "Word(alphanums)" (words made up of alphas or letters), or "Word(alphas, alphanums)" (words made up of alphas or letters, but must start with an alpha). Since your search string just takes search values, this is what makes it necessary for you to qualify the call with a list of potential search columns. This is okay if you are searching fields of an article (like say, title, subject, abstract, and/or body). But if the columns are dissimilar, such as fields of the products in a catalog, then you'll be searching fields like size and color with impossible or even misleading options ("size like '%RED%'" is a wasted search, but "color like '%L%'" will return YELLOW items, whether they are Large or not). For an application like the product catalog, then you could recast your query grammar to search for boolean combinations of conditional expressions like "field like value". Then you wouldn't need the additional parameter listing the fields to search, you can parse them out of the query string itself. This would also support conditional tests other than like, such as "price < 200". In any event, I hope pyparsing will help support your experimenting, and let you try some different application ideas while pyparsing offloads some of the dull parsing stuff. -- Paul --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---