[pylucene-dev] testing PyLucene's pythonic extensions

Ofer Nave ofer at smarter.com
Thu Mar 29 23:01:15 PST 2007


I was reading through the Pythonic Extensions section of the README 
while porting it to the wiki, and I though I remembered one of them 
having caused an error a while back.  I didn't have a change to 
investigate at the time.

Now I do, so I've whipped up a test script that tests out the various 
pieces of code in the Pythonic Extensions section.  Extensions 3 and 4 
fail for me:

---
[ofer at rnd01 ~/proj/search/trunk]$ bin/tmp.py 3
PyLucene 2.1.0-1 Lucene 2.1.0-509013
Traceback (most recent call last):
  File "bin/tmp.py", line 48, in <module>
    main()
  File "bin/tmp.py", line 31, in main
    print len(reader)
TypeError: object of type 'PyLucene.IndexReader' has no len()
[ofer at rnd01 ~/proj/search/trunk]$ bin/tmp.py 4
PyLucene 2.1.0-1 Lucene 2.1.0-509013
Hail To The Thief
Traceback (most recent call last):
  File "bin/tmp.py", line 48, in <module>
    main()
  File "bin/tmp.py", line 40, in main
    print doc.title
AttributeError: 'PyLucene.Document' object has no attribute 'title'
---

The script:

---
#!/bin/env python2.5
import sys
import PyLucene

def main():
    print 'PyLucene', PyLucene.VERSION, 'Lucene', PyLucene.LUCENE_VERSION
    directory = '/tmp/crash'
    store = PyLucene.RAMDirectory()
    analyzer = PyLucene.StandardAnalyzer()
    writer = PyLucene.IndexWriter(store, analyzer, True)
    doc = PyLucene.Document()
    doc.add(PyLucene.Field('title', 'Hail To The Thief', 
PyLucene.Field.Store.YES, PyLucene.Field.Index.TOKENIZED))
    writer.addDocument(doc)
    writer.close()
    reader = PyLucene.IndexReader.open(store)
    searcher = PyLucene.IndexSearcher(store)
    parser = PyLucene.QueryParser('title', analyzer)
    hits = searcher.search(parser.parse('thief'))

    # "Hits instances are iterable in Python."
    if sys.argv[1] == '1':
        for i, doc in hits:
            print hits.score(i), ':', doc['title']
    # "Hits instances partially implement the Python 'list' protocol."
    elif sys.argv[1] == '2':
        print len(hits)
        doc = hits[0]
        print doc
    # IndexReader? instances partially implement the 'list' protocol and 
can be iterated over for their documents.
    elif sys.argv[1] == '3':
        print len(reader)
        doc = reader[0]
        print doc
        for i, doc in reader:
            print doc['title']
    # Document instances have fields whose values can be accessed 
through the dict and attribute protocol.
    elif sys.argv[1] == '4':
        for i, doc in reader:
            print doc['title']
            print doc.title
            del doc.title
    # Document instances can be iterated over for their fields
    elif sys.argv[1] == '5':
        for i, doc in reader:
            for field in doc:
                print field

main()
---

-ofer


More information about the pylucene-dev mailing list