Skip to content

Commit

Permalink
Moved utilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
coady committed Dec 29, 2017
1 parent cb94204 commit 50901f0
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 72 deletions.
4 changes: 1 addition & 3 deletions docs/engine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ TokenStream
TokenFilter
^^^^^^^^^^^^^
.. autoclass:: TokenFilter
:show-inheritance:
:members:

Analyzer
^^^^^^^^^^^^^
.. autoclass:: Analyzer
:members:


indexers
---------
.. automodule:: lupyne.engine.indexers
Expand All @@ -46,7 +46,6 @@ IndexReader
IndexSearcher
^^^^^^^^^^^^^
.. autoclass:: IndexSearcher
:show-inheritance:
:members:

.. automethod:: __getitem__
Expand All @@ -69,7 +68,6 @@ MultiSearcher
IndexWriter
^^^^^^^^^^^^^
.. autoclass:: IndexWriter
:show-inheritance:
:members:

.. attribute:: fields
Expand Down
3 changes: 1 addition & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ Contents
^^^^^^^^^^

.. toctree::
:maxdepth: 2
:maxdepth: 3

engine
server
utils

.. toctree::
:hidden:
Expand Down
9 changes: 0 additions & 9 deletions docs/utils.rst

This file was deleted.

6 changes: 1 addition & 5 deletions lupyne/engine/analyzers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
"""
Analyzers and tokenizers.
"""

import collections
from java.io import StringReader
from java.lang import Float
Expand All @@ -11,7 +7,7 @@
from org.apache.pylucene.analysis import PythonAnalyzer, PythonTokenFilter
from org.apache.pylucene.queryparser.classic import PythonQueryParser
from six import string_types
from ..utils import method
from .utils import method


class TokenStream(analysis.TokenStream):
Expand Down
18 changes: 2 additions & 16 deletions lupyne/engine/documents.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
"""
Wrappers for lucene Fields and Documents.
"""

import calendar
import collections
import datetime
import operator
import lucene # noqa
from java.lang import Double, Float, Long, Number, Object
from java.lang import Long
from java.util import Arrays, HashSet
from org.apache.lucene import document, index, search, util
from org.apache.lucene.search import grouping
from six.moves import map, range
from .queries import Query
from ..utils import long
from .utils import convert, long
FieldType = document.FieldType


Expand Down Expand Up @@ -259,16 +255,6 @@ def dict(self, *names, **defaults):
return defaults


def convert(value):
"""Return python object from java Object."""
if util.BytesRef.instance_(value):
return util.BytesRef.cast_(value).utf8ToString()
if not Number.instance_(value):
return value.toString() if Object.instance_(value) else value
value = Number.cast_(value)
return value.doubleValue() if Float.instance_(value) or Double.instance_(value) else int(value.longValue())


class Hit(Document):
"""A Document from a search result, with :attr:`id`, :attr:`score`, and optional sort :attr:`keys`."""
def __init__(self, doc, id, score, keys=()):
Expand Down
13 changes: 2 additions & 11 deletions lupyne/engine/indexers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
"""
Wrappers for lucene Index{Read,Search,Writ}ers.
The final `Indexer`_ classes exposes a high-level Searcher and Writer.
"""

import contextlib
import itertools
import operator
Expand All @@ -17,12 +11,9 @@
from six import string_types
from six.moves import filter, map, range, zip
from .analyzers import Analyzer
from .queries import lucene6, suppress, Query, DocValues, SpellParser
from .queries import Query, DocValues, SpellParser
from .documents import Field, Document, Hits, GroupingSearch
from ..utils import long, Atomic, SpellChecker

for cls in (analysis.TokenStream, lucene.JArray_byte):
Atomic.register(cls)
from .utils import long, lucene6, suppress, Atomic, SpellChecker


class closing(set):
Expand Down
21 changes: 2 additions & 19 deletions lupyne/engine/queries.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
"""
Query wrappers and search utilities.
"""

import contextlib
import lucene
import lucene # noqa
from java.lang import Double, Integer, Long
from java.util import Arrays
from org.apache.lucene import document, index, search, util
from org.apache.lucene.search import spans
from org.apache.pylucene.queryparser.classic import PythonQueryParser
from six import string_types
from six.moves import map, range
from ..utils import long, method

lucene6 = lucene.VERSION.startswith('6.')
from .utils import long, lucene6, method


class Query(object):
Expand Down Expand Up @@ -251,16 +244,6 @@ def within(self, other):
return SpanQuery(spans.SpanWithinQuery, self, other)


@contextlib.contextmanager
def suppress(exception):
"""Suppress specific lucene exception."""
try:
yield
except lucene.JavaError as exc:
if not exception.instance_(exc.getJavaException()):
raise


class Base(object):
def __init__(self, docvalues, size, type):
self.docvalues, self.size, self.type = docvalues, size, type
Expand Down
33 changes: 27 additions & 6 deletions lupyne/utils.py → lupyne/engine/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""
Common utilities with no lucene dependencies.
"""

import abc
import bisect
import collections
import contextlib
import heapq
import itertools
import types
import lucene
import six
from java.lang import Double, Float, Number, Object
from org.apache.lucene import analysis, util
long = int if six.PY3 else long # noqa
lucene6 = lucene.VERSION.startswith('6.')


class Atomic(six.with_metaclass(abc.ABCMeta)):
Expand All @@ -19,8 +20,8 @@ def __subclasshook__(cls, other):
return not issubclass(other, collections.Iterable) or NotImplemented


for string in six.string_types:
Atomic.register(string)
for cls in six.string_types + (analysis.TokenStream, lucene.JArray_byte):
Atomic.register(cls)


class method(staticmethod):
Expand Down Expand Up @@ -49,3 +50,23 @@ def complete(self, prefix, count=None):
return heapq.nlargest(count, words, key=self.__getitem__)
words.sort(key=self.__getitem__, reverse=True)
return words


@contextlib.contextmanager
def suppress(exception):
"""Suppress specific lucene exception."""
try:
yield
except lucene.JavaError as exc:
if not exception.instance_(exc.getJavaException()):
raise


def convert(value):
"""Return python object from java Object."""
if util.BytesRef.instance_(value):
return util.BytesRef.cast_(value).utf8ToString()
if not Number.instance_(value):
return value.toString() if Object.instance_(value) else value
value = Number.cast_(value)
return value.doubleValue() if Float.instance_(value) or Double.instance_(value) else int(value.longValue())
2 changes: 1 addition & 1 deletion tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def test_fields(indexer, constitution):
with pytest.raises(AttributeError):
engine.Field('', invalid=None)
with pytest.raises(lucene.JavaError):
with engine.queries.suppress(search.TimeLimitingCollector.TimeExceededException):
with engine.utils.suppress(search.TimeLimitingCollector.TimeExceededException):
document.Field('name', 'value', document.FieldType())
assert str(engine.Field.String('')) == str(document.StringField('', '', document.Field.Store.NO).fieldType())
assert str(engine.Field.Text('')) == str(document.TextField('', '', document.Field.Store.NO).fieldType())
Expand Down

0 comments on commit 50901f0

Please sign in to comment.