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
dict
object. More exactly, it implementscollections.MutableMapping
protocol.¶ Redis commands Hash
methodsDEL 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
key
exists.Parameters: key – the key Returns: True
if thekey
exists orFalse
Return type: bool
Note
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
key
is 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
key
Raises: - exceptions.TypeError – if the given
key
is 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.Iterator
Note
It is directly mapped to Redis HKEYS command.
-
__len__
(*args, **kwargs)¶ Gets the number of items.
Returns: the number of items Return type: numbers.Integral
Note
It is directly mapped to Redis HLEN command.
-
__setitem__
(*args, **kwargs)¶ Sets the
key
with thevalue
.Parameters: - key – the key to set
- value – the value to set
Raises exceptions.TypeError: if the given
key
is not acceptable by itskey_type
or the givenvalue
is not acceptable by itsvalue_type
Note
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.ItemsView
Note
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 aSet
instead of iterable. There isn’t any meaningful order of keys.Returns: the set of its all keys Return type: collections.KeysView
Note
This method is directly mapped to Redis HKEYS command.
-
setdefault
(key, default=None)¶ Sets the given
default
value to thekey
if 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
key
doesn’t exist
Raises exceptions.TypeError: when the given
key
is not acceptable by itskey_type
or the givendefault
value is not acceptable by itsvalue_type
Note
This method internally uses Redis HSETNX command which is atomic.
-
update
(*args, **kwargs)¶ Updates the hash from the given
mapping
and keyword arguments.If
mapping
haskeys()
method, it does:for k in mapping: self[k] = mapping[k]
If
mapping
lackskeys()
method, it does:for k, v in mapping: self[k] = v
In either case, this is followed by (where
keywords
is adict
of 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_type
doesn’t accept byte strings (str
) it raisesTypeError
Raises: - exceptions.TypeError – if the
mapping
is not actually mapping or iterable, or the given keys and values to update aren’t acceptable by itskey_type
andvalue_type
- exceptions.ValueError – if the
mapping
is 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
list
but there isn’t any meaningful order of values.Returns: its all values Return type: collections.ValuesView
Note
This method is directly mapped to Redis HVALS command.
-