def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, separator=None, literal=False): """ Acts like a python dictionary. Params are: host: Tyrant Host address port: Tyrant port number separator: If this parameter is set, you can put and get lists as values. literal: If is set string is returned instead of unicode """ # We want to make protocol public just in case anyone need any # specific option self.proto = TyrantProtocol(host, port) self.dbtype = self.get_stats()['type'] if self.dbtype == 'skeleton': self.dbtype = DBTYPETABLE self.separator = separator self.literal = literal
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, separator=None, literal=False): """ Acts like a python dictionary. Params are: host: Tyrant Host address port: Tyrant port number separator: If this parameter is set, you can put and get lists as values. literal: If is set string is returned instead of unicode """ # We want to make protocol public just in case anyone need any # specific option self.proto = TyrantProtocol(host, port) self.dbtype = self.get_stats()["type"] self.separator = separator self.literal = literal
class Tyrant(dict): """Main class of Tyrant implementation. Acts like a python dictionary, so you can query object using normal subscript operations. """ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, separator=None, literal=False): """ Acts like a python dictionary. Params are: host: Tyrant Host address port: Tyrant port number separator: If this parameter is set, you can put and get lists as values. literal: If is set string is returned instead of unicode """ # We want to make protocol public just in case anyone need any # specific option self.proto = TyrantProtocol(host, port) self.dbtype = self.get_stats()['type'] if self.dbtype == 'skeleton': self.dbtype = DBTYPETABLE self.separator = separator self.literal = literal def __contains__(self, key): try: self.proto.vsiz(key) except TyrantError: return False else: return True def __delitem__(self, key): try: return self.proto.out(key) except TyrantError: raise KeyError(key) def __getitem__(self, key): try: return _parse_elem(self.proto.get(key, self.literal), self.dbtype, self.separator) except TyrantError: raise KeyError(key) def get(self, key, default=None): """Allow for getting with a default. >>> t = Tyrant() >>> t['foo'] = {'a': 'z', 'b': 'y'} >>> print t.get('foo', {}) {u'a': u'z', u'b': u'y'} >>> print t.get('bar', {}) {} """ try: return self[key] except KeyError: return default def __len__(self): return self.proto.rnum() def __repr__(self): return object.__repr__(self) def __setitem__(self, key, value): if isinstance(value, dict): flat = _itertools.chain([key], *value.iteritems()) self.proto.misc('put', list(flat)) elif isinstance(value, (list, tuple)): assert self.separator, "Separator is not set" flat = self.separator.join(value) self.proto.put(key, flat) else: self.proto.put(key, value) def call_func(self, func, key, value, record_locking=False, global_locking=False): """Call specific function. """ opts = ((record_locking and TyrantProtocol.RDBXOLCKREC) | (global_locking and TyrantProtocol.RDBXOLCKGLB)) return self.proto.ext(func, opts, key, value) def clear(self): """Used in order to remove all records of a remote database object""" self.proto.vanish() def concat(self, key, value, width=None): """Concatenate columns of the existing record""" if width is None: self.proto.putcat(key, value) else: self.proto.putshl(key, value, width) def get_size(self, key): """Get the size of the value of a record""" try: return self.proto.vsiz(key) except TyrantError: raise KeyError(key) def get_stats(self): """Get the status string of the database. The return value is the status message of the database.The message format is a dictionary. """ return dict(l.split('\t', 1) \ for l in self.proto.stat().splitlines() if l) def iterkeys(self): """Iterate keys using remote operations""" self.proto.iterinit() try: while True: yield self.proto.iternext() except TyrantError: pass def keys(self): """Return the list of keys in database""" return list(self.iterkeys()) def update(self, other, **kwargs): """Update/Add given objects into database""" self.multi_set(other.iteritems()) if kwargs: self.update(kwargs) def multi_del(self, keys, no_update_log=False): """Remove given records from database""" opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0) if not isinstance(keys, (list, tuple)): keys = list(keys) self.proto.misc("outlist", keys, opts) def multi_get(self, keys, no_update_log=False): """Returns a list of records that match given keys""" opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0) if not isinstance(keys, (list, tuple)): keys = list(keys) rval = self.proto.misc("getlist", keys, opts) if len(rval) <= len(keys): # 1.1.10 protocol, may return invalid results if len(rval) < len(keys): raise KeyError("Missing a result, unusable response in 1.1.10") return rval # 1.1.11 protocol returns interleaved key, value list d = dict((rval[i], _parse_elem(rval[i + 1], self.dbtype, self.separator)) \ for i in xrange(0, len(rval), 2)) return d def multi_set(self, items, no_update_log=False): """Store given records into database""" opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0) lst = [] for k, v in items.iteritems(): if isinstance(v, (list, tuple)): assert self.separator, "Separator is not set" v = self.separator.join(v) lst.extend((k, v)) self.proto.misc("putlist", lst, opts) def get_int(self, key): """Get an integer for given key. Must been added by addint""" return self.proto.getint(key) def get_double(self, key): """Get a double for given key. Must been added by adddouble""" return self.proto.getdouble(key) def prefix_keys(self, prefix, maxkeys=None): """Get forward matching keys in a database. The return value is a list object of the corresponding keys. """ if maxkeys is None: maxkeys = len(self) return self.proto.fwmkeys(prefix, maxkeys) def sync(self): """Synchronize updated content into database""" self.proto.sync() def _get_query(self): return Query(self.proto, self.dbtype, self.literal) query = property(_get_query)
class Tyrant(dict): """Main class of Tyrant implementation. Acts like a python dictionary, so you can query object using normal subscript operations. """ def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, separator=None, literal=False): """ Acts like a python dictionary. Params are: host: Tyrant Host address port: Tyrant port number separator: If this parameter is set, you can put and get lists as values. literal: If is set string is returned instead of unicode """ # We want to make protocol public just in case anyone need any # specific option self.proto = TyrantProtocol(host, port) self.dbtype = self.get_stats()['type'] if self.dbtype == 'skeleton' : self.dbtype = DBTYPETABLE self.separator = separator self.literal = literal def __contains__(self, key): try: self.proto.vsiz(key) except TyrantError: return False else: return True def __delitem__(self, key): try: return self.proto.out(key) except TyrantError: raise KeyError(key) def __getitem__(self, key): try: return _parse_elem(self.proto.get(key, self.literal), self.dbtype, self.separator) except TyrantError: raise KeyError(key) def get(self, key, default=None): """Allow for getting with a default. >>> t = Tyrant() >>> t['foo'] = {'a': 'z', 'b': 'y'} >>> print t.get('foo', {}) {u'a': u'z', u'b': u'y'} >>> print t.get('bar', {}) {} """ try: return self[key] except KeyError: return default def __len__(self): return self.proto.rnum() def __repr__(self): return object.__repr__(self) def __setitem__(self, key, value): if isinstance(value, dict): flat = _itertools.chain([key], *value.iteritems()) self.proto.misc('put', list(flat)) elif isinstance(value, (list, tuple)): assert self.separator, "Separator is not set" flat = self.separator.join(value) self.proto.put(key, flat) else: self.proto.put(key, value) def call_func(self, func, key, value, record_locking=False, global_locking=False): """Call specific function. """ opts = ((record_locking and TyrantProtocol.RDBXOLCKREC) | (global_locking and TyrantProtocol.RDBXOLCKGLB)) return self.proto.ext(func, opts, key, value) def clear(self): """Used in order to remove all records of a remote database object""" self.proto.vanish() def concat(self, key, value, width=None): """Concatenate columns of the existing record""" if width is None: self.proto.putcat(key, value) else: self.proto.putshl(key, value, width) def get_size(self, key): """Get the size of the value of a record""" try: return self.proto.vsiz(key) except TyrantError: raise KeyError(key) def get_stats(self): """Get the status string of the database. The return value is the status message of the database.The message format is a dictionary. """ return dict(l.split('\t', 1) \ for l in self.proto.stat().splitlines() if l) def iterkeys(self): """Iterate keys using remote operations""" self.proto.iterinit() try: while True: yield self.proto.iternext() except TyrantError: pass def keys(self): """Return the list of keys in database""" return list(self.iterkeys()) def update(self, other, **kwargs): """Update/Add given objects into database""" self.multi_set(other.iteritems()) if kwargs: self.update(kwargs) def multi_del(self, keys, no_update_log=False): """Remove given records from database""" opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0) if not isinstance(keys, (list, tuple)): keys = list(keys) self.proto.misc("outlist", keys, opts) def multi_get(self, keys, no_update_log=False): """Returns a list of records that match given keys""" opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0) if not isinstance(keys, (list, tuple)): keys = list(keys) rval = self.proto.misc("getlist", keys, opts) if len(rval) <= len(keys): # 1.1.10 protocol, may return invalid results if len(rval) < len(keys): raise KeyError("Missing a result, unusable response in 1.1.10") return rval # 1.1.11 protocol returns interleaved key, value list d = dict((rval[i], _parse_elem(rval[i + 1], self.dbtype, self.separator)) \ for i in xrange(0, len(rval), 2)) return d def multi_set(self, items, no_update_log=False): """Store given records into database""" opts = (no_update_log and TyrantProtocol.RDBMONOULOG or 0) lst = [] for k, v in items.iteritems(): if isinstance(v, (list, tuple)): assert self.separator, "Separator is not set" v = self.separator.join(v) lst.extend((k, v)) self.proto.misc("putlist", lst, opts) def get_int(self, key): """Get an integer for given key. Must been added by addint""" return self.proto.getint(key) def get_double(self, key): """Get a double for given key. Must been added by adddouble""" return self.proto.getdouble(key) def prefix_keys(self, prefix, maxkeys=None): """Get forward matching keys in a database. The return value is a list object of the corresponding keys. """ if maxkeys is None: maxkeys = len(self) return self.proto.fwmkeys(prefix, maxkeys) def sync(self): """Synchronize updated content into database""" self.proto.sync() def _get_query(self): return Query(self.proto, self.dbtype, self.literal) query = property(_get_query)