coady  committed b82e0e4

FormatField deprecated in favor of extensible MapField.

  • Participants
  • Parent commits 2598cd1

Comments (0)

Files changed (5)

File docs/engine.rst

 .. automodule:: lupyne.engine
   * `TokenFilter`_, `Analyzer`_, `IndexSearcher`_, `MultiSearcher`_, `IndexWriter`_, `Indexer`_, `ParallelIndexer`_
-  * `Document`_, `Field`_, `FormatField`_, `NestedField`_, `NumericField`_, `DateTimeField`_
+  * `Document`_, `Field`_, `MapField`_, `FormatField`_, `NestedField`_, `NumericField`_, `DateTimeField`_
   * `Query`_, `SortField`_, `TermsFilter`_
   * `PointField`_, `PolygonField`_
 .. autoclass:: Field
+.. autoclass:: MapField
+  :show-inheritance:
+  :members:
+.. deprecated:: 1.2+ use `MapField`_ with format method instead.
 .. autoclass:: FormatField

File lupyne/engine/

 warnings.simplefilter('default', DeprecationWarning)
 from .queries import Query, SortField, TermsFilter
-from .documents import Document, Field, FormatField, NestedField, NumericField, DateTimeField
+from .documents import Document, Field, MapField, FormatField, NestedField, NumericField, DateTimeField
 from .indexers import TokenFilter, Analyzer, IndexSearcher, MultiSearcher, IndexWriter, Indexer, ParallelIndexer
 from .spatial import PointField, PolygonField

File lupyne/engine/

 import datetime, calendar
 import operator
 import collections
+import warnings
 import lucene
 from .queries import Query
                 setattr(field, name, value)
             yield field
-class FormatField(Field):
+class MapField(Field):
+    """Field which applies a function across its values.
+    :param func: callable
+    """
+    def __init__(self, name, func, **kwargs):
+        Field.__init__(self, name, **kwargs)
+        self.func = func
+    def items(self, *values):
+        "Generate fields with mapped values."
+        return Field.items(self, *map(self.func, values))
+class FormatField(MapField):
     """Field which uses string formatting on its values.
     :param format: format string
     def __init__(self, name, format='{0}', **kwargs):
-        Field.__init__(self, name, **kwargs)
-        self.format = format.format
-    def items(self, *values):
-        "Generate fields with formatted values."
-        return Field.items(self, *map(self.format, values))
+        warnings.warn('FormatField will be removed in the next release; use MapField instead.', DeprecationWarning)
+        MapField.__init__(self, name, format.format, **kwargs)
+        self.format = self.func
 class NestedField(Field):
     """Field which indexes every component into its own field.
    - Support for contrib grouping collectors and faceting
    - FieldCache comparators optimized for memory and real-time searching
    - Caching for sort keys
+   - Field support for mapping functions
  * Server:

File test/

         attrs = 'indexed', 'tokenized', 'termVectorStored', 'storePositionWithTermVector', 'storeOffsetWithTermVector', 'omitNorms'
         assert all(getattr(field, attr) for attr in attrs)
         indexer = engine.Indexer(self.tempdir)
-        indexer.set('amendment', engine.FormatField, format='{0:02d}', store=True)
-        indexer.set('size', engine.FormatField, format='{0:04d}', store=True)
+        with assertWarns(DeprecationWarning):
+            indexer.set('amendment', engine.FormatField, format='{0:02d}', store=True)
+        indexer.set('size', engine.MapField, func='{0:04d}'.format, store=True)
         field = indexer.fields['date'] = engine.NestedField('Y-m-d', sep='-', store=True)
         for doc in
             if 'amendment' in doc: