Commits

Jordan Sherer committed a26fcf5

Compatibility fixes part 1.

Comments (0)

Files changed (10)

src/whoosh/filedb/filetables.py

             magic = 0
             p = -1 if self.postings is None else self.postings
             st += pack_long(p)
-        return chr(magic) + st
+        return b(chr(magic)) + st
 
     @classmethod
     def from_string(cls, s):
             if hbyte == 0:
                 p = unpack_long(pstr)[0]
             else:
-                p = loads(pstr + ".")
+                p = loads(pstr + b("."))
         else:
             # Old format was encoded as a variable length pickled tuple
             v = loads(s + b("."))

src/whoosh/searching.py

                 continue
             
             if keyfns:
-                for name, keyfn in keyfns.iteritems():
+                for name, keyfn in iteritems(keyfns):
                     if name not in groups:
                         groups[name] = defaultdict(list)
                     key = keyfn(offsetid - offset)

src/whoosh/support/levenshtein.py

 Contains functions implementing edit distance algorithms.
 """
 
+from whoosh.compat import xrange
+
 
 def levenshtein(seq1, seq2, limit=None):
     """Returns the Levenshtein edit distance between two strings.
     """
     
     oneago = None
-    thisrow = range(1, len(seq2) + 1) + [0]
+    thisrow = list(range(1, len(seq2) + 1)) + [0]
     for x in xrange(len(seq1)):
         # Python lists wrap around for negative indices, so put the
         # leftmost column at the *end* of the list. This matches with

tests/test_parse_plugins.py

-from __future__ import with_statement
+from __future__ import with_statement, print_function
 import inspect
 from datetime import datetime
 import sys
     assert_equal(q.startdate, adatetime(2010, 3, 30).floor())
     assert_equal(q.enddate, None)
     
-    print "!!!!!!!!!!!!!!!!!!!!"
+    print("!!!!!!!!!!!!!!!!!!!!")
     q = qp.parse(u("date:[30 march to next wednesday]"))
-    print "q=", q
+    print("q=", q)
     assert_equal(q.__class__, query.DateRange)
     assert_equal(q.startdate, adatetime(2010, 3, 30).floor())
     assert_equal(q.enddate, adatetime(2010, 9, 22).ceil())

tests/test_parsing.py

+from __future__ import print_function
+
 from nose.tools import assert_equal, assert_not_equal  #@UnresolvedImport
 
 from whoosh import analysis, fields, query
     assert_equal(q[3].text, "hi^5x")
     
     q = qp.parse("alfa (bravo OR charlie)^2.5 ^3")
-    print "q=", q
+    print("q=", q)
     assert_equal(len(q), 3)
     assert_equal(q[0].boost, 1.0)
     assert_equal(q[1].boost, 2.5)

tests/test_quality.py

 from nose.tools import assert_equal, assert_almost_equal, assert_not_equal
 
 from whoosh import analysis, fields, formats, matching, scoring
-from whoosh.compat import u
+from whoosh.compat import b, u, xrange
 from whoosh.filedb.filepostings import FilePostingWriter, FilePostingReader
 from whoosh.filedb.filestore import RamStorage
 from whoosh.filedb.filetables import FileTermInfo
     schema = fields.Schema(t=fields.TEXT(phrase=False))
     ix = st.create_index(schema)
     w = ix.writer()
-    w.add_document(t=u"alfa bravo charlie delta alfa bravo alfa")
+    w.add_document(t=u("alfa bravo charlie delta alfa bravo alfa"))
     w.commit()
     
     with ix.reader() as r:
-        ti = r.termsindex["t", u"alfa"]
+        ti = r.termsindex["t", u("alfa")]
         assert_equal(ti.weight(), 3.0)
         assert_equal(ti.doc_frequency(), 1)
         assert_equal(ti.min_length(), 7)
         assert_equal(ti.max_length(), 7)
         assert_equal(ti.max_weight(), 3.0)
         assert_almost_equal(ti.max_wol(), 3.0 / 7)
-        assert_equal(ti.postings, ((0, ), (3.0, ), ('\x00\x00\x00\x03', )))
+        assert_equal(ti.postings, ((0, ), (3.0, ), (b('\x00\x00\x00\x03'), )))
 
     w = ix.writer()
-    w.add_document(t=u"alfa charlie alfa")
+    w.add_document(t=u("alfa charlie alfa"))
     w.commit()
     
     with ix.reader() as r:
-        ti = r.termsindex["t", u"alfa"]
+        ti = r.termsindex["t", u("alfa")]
         assert_equal(ti.weight(), 5.0)
         assert_equal(ti.doc_frequency(), 2)
         assert_equal(ti.min_length(), 3)
     ix = st.create_index(schema)
     for i in xrange(1, 200, 7):
         w = ix.writer()
-        w.add_document(t=u" ".join(["word"] * i))
+        w.add_document(t=u(" ").join(["word"] * i))
         w.commit()
         
         with ix.reader() as r:
         count = random.randint(1, 100)
         least = min(count, least)
         most = max(count, most)
-        w.add_document(t=u" ".join(["word"] * count))
+        w.add_document(t=u(" ").join(["word"] * count))
         w.commit()
         
         with ix.reader() as r:
     schema = fields.Schema(t=fields.TEXT)
     ix = RamStorage().create_index(schema)
     w = ix.writer()
-    w.add_document(t=u"alfa bravo charlie delta echo")
-    w.add_document(t=u"bravo charlie delta echo foxtrot")
-    w.add_document(t=u"charlie delta echo foxtrot golf")
-    w.add_document(t=u"delta echo foxtrot")
-    w.add_document(t=u"echo foxtrot golf hotel india juliet")
-    w.add_document(t=u"foxtrot alfa alfa alfa")
+    w.add_document(t=u("alfa bravo charlie delta echo"))
+    w.add_document(t=u("bravo charlie delta echo foxtrot"))
+    w.add_document(t=u("charlie delta echo foxtrot golf"))
+    w.add_document(t=u("delta echo foxtrot"))
+    w.add_document(t=u("echo foxtrot golf hotel india juliet"))
+    w.add_document(t=u("foxtrot alfa alfa alfa"))
     w.commit()
 
     with ix.reader() as r:
-        ti = r.term_info("t", u"alfa")
+        ti = r.term_info("t", u("alfa"))
         assert_equal(ti.weight(), 4.0)
         assert_equal(ti.doc_frequency(), 2)
         assert_equal(ti.min_length(), 4)
         assert_equal(ti.max_weight(), 3.0)
         assert_equal(ti.max_wol(), 3.0 / 4.0)
         
-        assert_equal(r.term_info("t", u"echo").min_length(), 3)
+        assert_equal(r.term_info("t", u("echo")).min_length(), 3)
         
         assert_equal(r.doc_field_length(3, "t"), 3)
         assert_equal(r.min_field_length("t"), 3)
         assert_equal(r.max_field_length("t"), 6)
         
     w = ix.writer()
-    w.add_document(t=u"alfa")
-    w.add_document(t=u"bravo charlie")
-    w.add_document(t=u"echo foxtrot tango bravo")
-    w.add_document(t=u"golf hotel")
-    w.add_document(t=u"india")
-    w.add_document(t=u"juliet alfa bravo charlie delta echo foxtrot")
+    w.add_document(t=u("alfa"))
+    w.add_document(t=u("bravo charlie"))
+    w.add_document(t=u("echo foxtrot tango bravo"))
+    w.add_document(t=u("golf hotel"))
+    w.add_document(t=u("india"))
+    w.add_document(t=u("juliet alfa bravo charlie delta echo foxtrot"))
     w.commit(merge=False)
 
     with ix.reader() as r:
-        ti = r.term_info("t", u"alfa")
+        ti = r.term_info("t", u("alfa"))
         assert_equal(ti.weight(), 6.0)
         assert_equal(ti.doc_frequency(), 4)
         assert_equal(ti.min_length(), 1)
         assert_equal(ti.max_weight(), 3.0)
         assert_equal(ti.max_wol(), 1.0)
         
-        assert_equal(r.term_info("t", u"echo").min_length(), 3)
+        assert_equal(r.term_info("t", u("echo")).min_length(), 3)
         
         assert_equal(r.min_field_length("t"), 1)
         assert_equal(r.max_field_length("t"), 7)

tests/test_queries.py

+from __future__ import print_function
+
 from nose.tools import assert_equal, assert_not_equal  #@UnresolvedImport
 
 from whoosh import fields
     q = QueryParser("value", None).parse(u('alfa hotel tango "sierra bravo"'))
     
     ts = q.existing_terms(r, phrases=False)
-    print "ts=", sorted(ts)
+    print("ts=", sorted(ts))
     assert_equal(sorted(ts), [("value", "alfa"), ("value", "hotel")])
     
     ts = q.existing_terms(r)
     
     w = ix.writer()
     w.update_document(
-        id=u'1',
-        title=u'Life Aquatic',
-        content=u'A nautic film crew sets out to kill a gigantic shark.',
+        id=u('1'),
+        title=u('Life Aquatic'),
+        content=u('A nautic film crew sets out to kill a gigantic shark.'),
         released=datetime(2004,12,25)
     )
     w.update_document(
-        id=u'2',
-        title=u'Darjeeling Limited',
-        content=u'Three brothers meet in India for a life changing train journey.',
+        id=u('2'),
+        title=u('Darjeeling Limited'),
+        content=u('Three brothers meet in India for a life changing train journey.'),
         released=datetime(2007,10,27)
     )
     w.commit()
     
     s = ix.searcher()
-    r = s.search(Term('content', u'train'))
+    r = s.search(Term('content', u('train')))
     assert_equal(len(r), 1)
     assert_equal(r[0]["id"], "2")
     assert_equal(r[0].highlights("content"), 'India for a life changing <b class="match term0">train</b> journey')

tests/test_reading.py

 from nose.tools import assert_equal
 
 from whoosh import analysis, fields, reading
-from whoosh.compat import u
+from whoosh.compat import u, xrange
 from whoosh.filedb.filestore import RamStorage
 from whoosh.support.testing import TempIndex
 

tests/test_searching.py

     with ix.searcher() as s:
         q = Or([Term("a", u("alfa")), Term("b", u("alfa"))])
         q = q.accept(field_booster("a", 100.0))
-        assert_equal(unicode(q), u"(a:alfa^100.0 OR b:alfa)")
+        assert_equal(text_type(q), text_type("(a:alfa^100.0 OR b:alfa)"))
         r = s.search(q)
         assert_equal([hit["id"] for hit in r], [2, 5, 6, 3, 0, 1, 4])
     

tests/test_tables.py

     with TempStorage("termkey") as st:
         tw = TermIndexWriter(st.create_file("test.trm"))
         tw.add(("alfa", u("bravo")), FileTermInfo(1.0, 3))
-        tw.add((u"alfa", u('\xc3\xa6\xc3\xaf\xc5\ufffd\xc3\xba')), FileTermInfo(4.0, 6))
-        tw.add((u"text", u('\xe6\u2014\xa5\xe6\u0153\xac\xe8\xaa\u017e')), FileTermInfo(7.0, 9))
+        tw.add((u("alfa"), u('\xc3\xa6\xc3\xaf\xc5\ufffd\xc3\xba')), FileTermInfo(4.0, 6))
+        tw.add((u("text"), u('\xe6\u2014\xa5\xe6\u0153\xac\xe8\xaa\u017e')), FileTermInfo(7.0, 9))
         tw.close()
         
         tr = TermIndexReader(st.open_file("test.trm"))