sider.hash — Hash objects¶
See also
- Redis Data Types
- The Redis documentation that explains about its data types: strings, lists, sets, sorted sets and hashes.
-
class
sider.hash.Hash(session, key, key_type=<class 'sider.types.ByteString'>, value_type=<class 'sider.types.ByteString'>)¶ The Python-side representaion of Redis hash value. It behaves such as built-in Python
dictobject. More exactly, it implementscollections.MutableMappingprotocol.Mappings of Redis commands– Hashmethods¶Redis commands HashmethodsDEL Hash.clear()HDEL del(Hash.__delitem__())HEXISTS in(Hash.__contains__())HGET Hash.__getitem__(),Hash.get()HGETALL Hash.items()HINCRBY N/A HINCRBYFLOAT N/A HKEYS iter()(Hash.__iter__()),Hash.keys()HLEN len()(Hash.__len__())HMGET N/A HMSET Hash.update()HSET =(Hash.__setitem__())HSETNX Hash.setdefault()HVALS Hash.values()N/A Hash.pop()N/A Hash.popitem()-
__contains__(*args, **kwargs)¶ Tests whether the given
keyexists.Parameters: key – the key Returns: Trueif thekeyexists orFalseReturn type: boolNote
It is directly mapped to Redis HEXISTS command.
-
__delitem__(key)¶ Removes the
key.Parameters: key – the key to delete
Raises: - exceptions.TypeError – if the given
keyis not acceptable by itskey_type - exceptions.KeyError – if there’s no such
key
Note
It is directly mapped to Redis HDEL command.
- exceptions.TypeError – if the given
-
__getitem__(*args, **kwargs)¶ Gets the value of the given
key.Parameters: key – the key to get its value
Returns: the value of the
keyRaises: - exceptions.TypeError – if the given
keyis not acceptable by itskey_type - exceptions.KeyError – if there’s no such
key
Note
It is directly mapped to Redis HGET command.
- exceptions.TypeError – if the given
-
__iter__(*args, **kwargs)¶ Iterates over its
keys().Returns: the iterator which yields its keys Return type: collections.IteratorNote
It is directly mapped to Redis HKEYS command.
-
__len__(*args, **kwargs)¶ Gets the number of items.
Returns: the number of items Return type: numbers.IntegralNote
It is directly mapped to Redis HLEN command.
-
__setitem__(*args, **kwargs)¶ Sets the
keywith thevalue.Parameters: - key – the key to set
- value – the value to set
Raises exceptions.TypeError: if the given
keyis not acceptable by itskey_typeor the givenvalueis not acceptable by itsvalue_typeNote
It is directly mapped to Redis HSET command.
-
clear(*args, **kwargs)¶ Removes all items from this hash.
Note
Under the hood it simply DEL the key.
-
items(*args, **kwargs)¶ Gets its all
(key, value)pairs. There isn’t any meaningful order of pairs.Returns: the set of (key, value)pairs (tuple)Return type: collections.ItemsViewNote
This method is mapped to Redis HGETALL command.
-
key_type= None¶ (
sider.types.Bulk) The type of hash keys.
-
keys()¶ Gets its all keys. Equivalent to
__iter__()except it returns aSetinstead of iterable. There isn’t any meaningful order of keys.Returns: the set of its all keys Return type: collections.KeysViewNote
This method is directly mapped to Redis HKEYS command.
-
setdefault(key, default=None)¶ Sets the given
defaultvalue to thekeyif it doesn’t exist and then returns the current value of thekey.For example, the following code is:
val = hash.setdefault('key', 'set this if not exist')
equivalent to:
try: val = hash['key'] except KeyError: val = hash['key'] = 'set this if not exist'
except
setdefault()method is an atomic operation.Parameters: - key – the key to get or set
- default – the value to be set if the
keydoesn’t exist
Raises exceptions.TypeError: when the given
keyis not acceptable by itskey_typeor the givendefaultvalue is not acceptable by itsvalue_typeNote
This method internally uses Redis HSETNX command which is atomic.
-
update(*args, **kwargs)¶ Updates the hash from the given
mappingand keyword arguments.If
mappinghaskeys()method, it does:for k in mapping: self[k] = mapping[k]
If
mappinglackskeys()method, it does:for k, v in mapping: self[k] = v
In either case, this is followed by (where
keywordsis adictof keyword arguments):for k, v in keywords.items(): self[k] = v
Parameters: - mapping (
collections.Mapping) – an iterable object of(key, value)pairs or a mapping object (which haskeys()method). default is empty - **keywords – the keywords to update as well.
if its
key_typedoesn’t accept byte strings (str) it raisesTypeError
Raises: - exceptions.TypeError – if the
mappingis not actually mapping or iterable, or the given keys and values to update aren’t acceptable by itskey_typeandvalue_type - exceptions.ValueError – if the
mappingis an iterable object which yields non-pair values e.g.[(1, 2, 3), (4,)]
-
value_type= None¶ (
sider.types.Bulk) The type of hash values.
-
values(*args, **kwargs)¶ Gets its all values. It returns a
listbut there isn’t any meaningful order of values.Returns: its all values Return type: collections.ValuesViewNote
This method is directly mapped to Redis HVALS command.
-