当前位置: 首页>>代码示例>>Python>>正文


Python Index._kw方法代码示例

本文整理汇总了Python中index.Index._kw方法的典型用法代码示例。如果您正苦于以下问题:Python Index._kw方法的具体用法?Python Index._kw怎么用?Python Index._kw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在index.Index的用法示例。


在下文中一共展示了Index._kw方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: register_model

# 需要导入模块: from index import Index [as 别名]
# 或者: from index.Index import _kw [as 别名]
  def register_model(self, *fields, **kw):
    """Registers a single model for fulltext search. This basically creates
    a simple PonyWhoosh.Index for the model and calls self.register_index on it.

    Args:
        *fields: all the fields indexed from the model.
        **kw: The options for each field, sortedby, stored ...
    """

    index = PonyWhooshIndex(pw=self)

    index._kw = kw
    index._fields = fields

    def inner(model):
      """This look for the types of each field registered in the index,
      whether if it is Numeric, datetime, boolean or just text.

      Args:
          model (PonyORM.Entity): A model defined on the database with PonyORM

      Returns:
          model (PonyORM.Entity): A model modified for handle the appropriate
          search methods.
      """

      index._name = model._table_
      if not index._name:
        index._name  = model.__name__

      self._entities[index._name]     = model
      index._schema_attrs             = {}
      index._primary_key_is_composite =  model._pk_is_composite_
      index._primary_key              = [f.name for f in model._pk_attrs_]
      index._primary_key_type         = 'list'
      type_attribute                  = {}

      for field in model._attrs_:
        if field.is_relation:
          continue

        assert hasattr(field, "name") and hasattr(field, "py_type")

        fname = field.name
        if hasattr(field.name, "__name__"):
            fname = field.name.__name__

        stored = kw.get("stored", False)
        if fname in index._primary_key:
            kw["stored"] = True
        # we're not supporting this kind of data
        ftype = field.py_type.__name__
        if ftype in ['date', 'datetime', 'datetime.date']:
            kw["stored"] = stored
            continue

        fwhoosh = fwhoosh = whoosh.fields.TEXT(**kw)

        if field == model._pk_:
            index._primary_key_type = ftype
            fwhoosh = whoosh.fields.ID(stored=True, unique=True)

        if fname in index._fields:
          if not field.is_string:
            if ftype in ['int', 'float']:
              fwhoosh = whoosh.fields.NUMERIC(**kw)
            elif ftype == 'bool':
              fwhoosh = whoosh.fields.BOOLEAN(stored=True)

        type_attribute[fname]       = ftype
        index._schema_attrs[fname]  = fwhoosh
        kw["stored"]                = stored

      index._schema = whoosh.fields.Schema(**index._schema_attrs)

      self.register_index(index)

      def _middle_save_(obj, status):
        """A middle-in-middle method to intercept CRUD operations from PonyORM
        over the current object model to update the appropriate whoosh index.

        Args:
            obj (EntityInstance): An instance of a current model.
            status (str): Type of transaction on the database. A CRUD operation.

        Returns:
            obj (EntityInstance): The same object as the input.
        """
        writer = index._whoosh.writer(timeout=self.writer_timeout)

        dict_obj = obj.to_dict()

        def dumps(v):
          if isinstance(v, int):
            return unicode(v)
          if isinstance(v, float):
            return '%.9f' % v
          return unicode(v)

        attrs = {}
#.........这里部分代码省略.........
开发者ID:compiteing,项目名称:ponywhoosh,代码行数:103,代码来源:core.py


注:本文中的index.Index._kw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。