Example #1
0
 def __init__(self, filename=':memory:', encoding='utf-8'):
     self._filename = filename
     self._encoding = encoding
     self._db = Rlite(path=filename, encoding=encoding)
     self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()
     for callback in self._invalid_callbacks:
         del self.response_callbacks[callback]
Example #2
0
class WalrusLite(Walrus):
    _invalid_callbacks = ('SET', 'MSET', 'LSET')

    def __init__(self, filename=':memory:', encoding='utf-8'):
        self._filename = filename
        self._encoding = encoding
        self._db = Rlite(path=filename, encoding=encoding)
        self.response_callbacks = self.__class__.RESPONSE_CALLBACKS.copy()
        for callback in self._invalid_callbacks:
            del self.response_callbacks[callback]

    def execute_command(self, *args, **options):
        command_name = args[0]
        result = self._db.command(*args)
        return self.parse_response(result, command_name, **options)

    def parse_response(self, result, command_name, **options):
        try:
            return self.response_callbacks[command_name.upper()](
                result, **options)
        except KeyError:
            return result

    def __repr__(self):
        if self._filename == ':memory:':
            db_file = 'in-memory database'
        else:
            db_file = self._filename
        return '<WalrusLite: %s>' % db_file

    def _filtered_scan(self, results, match=None, count=None):
        if match is not None:
            results = fnmatch.filter(results, match)
        if count:
            results = results[:count]
        return results

    def hscan_iter(self, key, match=None, count=None):
        return self._filtered_scan(self.hgetall(key), match, count)

    def sscan_iter(self, key, match=None, count=None):
        return self._filtered_scan(self.smembers(key), match, count)

    def zscan_iter(self, key, match=None, count=None):
        return self._filtered_scan(self.zrange(key, 0, -1), match, count)