File: searchParserAppDemo.py

package info (click to toggle)
pyparsing 2.0.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,196 kB
  • ctags: 6,279
  • sloc: python: 11,708; makefile: 35; sh: 33
file content (34 lines) | stat: -rw-r--r-- 935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from searchparser import SearchQueryParser

products = [ "grape juice", "grape jelly", "orange juice", "orange jujubees",
    "strawberry jam", "prune juice", "prune butter", "orange marmalade",
    "grapefruit juice" ]
    
class FruitSearchParser(SearchQueryParser):
    def GetWord(self, word):
        return set( p for p in products if p.startswith(word + " ") )

    def GetWordWildcard(self, word):
        return set( p for p in products if p.startswith(word[:-1]) )

    def GetQuotes(self, search_string, tmp_result):
        result = Set()
        # I have no idea how to use this feature...
        return result

    def GetNot(self, not_set):
        return set( products ) - not_set


parser = FruitSearchParser()

tests = """\
    grape or orange
    grape*
    not(grape*)
    prune and grape""".splitlines()

for t in tests:
    print(t.strip())
    print(parser.Parse(t))
    print()