def load(cls, primary_key, convert_key=True): """ Retrieve a model instance by primary key. :param primary_key: The primary key of the model instance. :returns: Corresponding :py:class:`Model` instance. :raises: ``KeyError`` if object with given primary key does not exist. """ if convert_key: primary_key = cls._query.get_primary_hash_key(primary_key) if not cls.database.hash_exists(primary_key): raise KeyError('Object not found.') raw_data = cls.database.hgetall(primary_key) data = {} for name, field in cls._fields.items(): if isinstance(field, _ContainerField): continue elif name in raw_data: data[name] = field.python_value(raw_data[name]) elif PY3 and encode(name) in raw_data: data[name] = field.python_value(raw_data[encode(name)]) else: data[name] = None return cls(**data)
def assertData(ret, idxs, is_multi=False): if is_multi: ret = dict(ret) accum = {} for idx in idxs: sname = encode('abc'[idx % 3]) accum.setdefault(sname, []) accum[sname].append(( docids[idx], {b'k': encode('v%s' % idx)})) else: accum = [] for idx in idxs: accum.append((docids[idx], {b'k': encode('v%s' % idx)})) self.assertEqual(ret, accum)
def search(self, phrase, limit=None, boosts=None, chunk_size=1000): """ Perform a search for the given phrase. Objects whose title matches the search will be returned. The values returned will be whatever you specified as the ``data`` parameter when you called :py:meth:`~Autocomplete.store`. :param phrase: One or more words or substrings. :param int limit: Limit size of the result set. :param dict boosts: A mapping of object id/object type to floating point multipliers. :returns: A list containing the object data for objects matching the search phrase. """ cleaned = self.tokenize_title(phrase, stopwords=False) if not cleaned: return all_boosts = self._load_saved_boosts() if PY3 and boosts: for key in boosts: all_boosts[encode(key)] = boosts[key] elif boosts: all_boosts.update(boosts) if len(cleaned) == 1 and not all_boosts: result_key = self.word_key(cleaned[0]) else: result_key = self.get_cache_key(cleaned, all_boosts) if result_key not in self.database: self.database.zinterstore( result_key, list(map(self.word_key, cleaned))) self.database.expire(result_key, self._cache_timeout) results = self.database.ZSet(result_key) if all_boosts: for raw_id, score in results[0:0, True]: orig_score = score for identifier in raw_id.split(encode('\x01'), 1): if identifier and identifier in all_boosts: score *= 1 / all_boosts[identifier] if orig_score != score: results[raw_id] = score for result in self._load_objects(results, limit, chunk_size): yield result
def search(self, phrase, limit=None, boosts=None, chunk_size=1000): """ Perform a search for the given phrase. Objects whose title matches the search will be returned. The values returned will be whatever you specified as the ``data`` parameter when you called :py:meth:`~Autocomplete.store`. :param phrase: One or more words or substrings. :param int limit: Limit size of the result set. :param dict boosts: A mapping of object id/object type to floating point multipliers. :returns: A list containing the object data for objects matching the search phrase. """ cleaned = self.tokenize_title(phrase, stopwords=False) if not cleaned: raise StopIteration all_boosts = self._load_saved_boosts() if PY3 and boosts: for key in boosts: all_boosts[encode(key)] = boosts[key] elif boosts: all_boosts.update(boosts) if len(cleaned) == 1 and not all_boosts: result_key = self.word_key(cleaned[0]) else: result_key = self.get_cache_key(cleaned, all_boosts) if result_key not in self.database: self.database.zinterstore(result_key, list(map(self.word_key, cleaned))) self.database.expire(result_key, self._cache_timeout) results = self.database.ZSet(result_key) if all_boosts: for raw_id, score in results[0:0, True]: orig_score = score for identifier in raw_id.split(encode('\x01'), 1): if identifier and identifier in all_boosts: score *= 1 / all_boosts[identifier] if orig_score != score: results[raw_id] = score for result in self._load_objects(results, limit, chunk_size): yield result
def __contains__(self, item): """ Return a boolean indicating whether the given item is stored in the array. O(n). """ item = encode(item) for value in self: if value == item: return True return False
def _load_saved_boosts(self): boosts = {} for combined_id, score in self._boosts: obj_id, obj_type = combined_id.split(encode('\x01'), 1) score = float(score) if obj_id and obj_type: boosts[combined_id] = score elif obj_id: boosts[obj_id] = score elif obj_type: boosts[obj_type] = score return boosts
class ByteField(Field): """Store arbitrary bytes.""" _coerce = lambda self, value: encode(value)
def db_value(self, value): return encode(json.dumps(value))
def db_value(self, value): return encode(value.hex if value is not None else '')
def db_value(self, value): return b'' if value is None else encode(value)
def assertData(items, expected): self.assertEqual(items, [(item_ids[e], { b'k': encode('v%s' % e) }) for e in expected])
def assertData(items, expected): self.assertEqual(items, [(item_ids[e], {b'k': encode('v%s' % e)}) for e in expected])