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 implements collections.MutableMapping protocol.

Mappings of Redis commands–Hash methods
Redis commands Hash methods
DEL 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 the key exists or False
Return type:bool

Note

It is directly mapped to Redis HEXISTS command.

__delitem__(key)

Removes the key.

Parameters:

key – the key to delete

Raises:

Note

It is directly mapped to Redis HDEL command.

__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:

Note

It is directly mapped to Redis HGET command.

__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 the value.

Parameters:
  • key – the key to set
  • value – the value to set
Raises exceptions.TypeError:
 

if the given key is not acceptable by its key_type or the given value is not acceptable by its value_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 a Set 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 the key if it doesn’t exist and then returns the current value of the key.

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 its key_type or the given default value is not acceptable by its value_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 has keys() method, it does:

    for k in mapping:
        self[k] = mapping[k]
    
  • If mapping lacks keys() method, it does:

    for k, v in mapping:
        self[k] = v
    
  • In either case, this is followed by (where keywords is a dict 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 has keys() method). default is empty
  • **keywords – the keywords to update as well. if its key_type doesn’t accept byte strings (str) it raises TypeError
Raises:
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.