Module jumpscale.clients.redis.redis
Redis client
Expand source code
"""
Redis client
"""
from redis import Redis, exceptions
from jumpscale.core import events
from jumpscale.clients.base import Client
from jumpscale.core.base import fields
from jumpscale.core.base.events import AttributeUpdateEvent
class RedisClientAttributeUpdated(AttributeUpdateEvent):
    pass
class RedisClient(Client):
    hostname = fields.String(default="localhost")
    port = fields.Integer(default=6379)
    password = fields.Secret(default="")
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__client = None
    def _attr_updated(self, name, value):
        super()._attr_updated(name, value)
        # this will allow other people to listen to this event too
        event = RedisClientAttributeUpdated(self, name, value)
        events.notify(event)
        # reset client
        self.__client = None
    def __dir__(self):
        return list(self._fields.keys()) + dir(self.redis_client)
    @property
    def redis_client(self):
        if not self.__client:
            if self.password:
                self.__client = Redis(self.hostname, self.port, password=self.password)
            else:
                self.__client = Redis(self.hostname, self.port)
        return self.__client
    def is_running(self):
        try:
            return self.redis_client.ping()
        except exceptions.ConnectionError:
            return False
    def __getattr__(self, k):
        # forward non found attrs to self.redis_client
        return getattr(self.redis_client, k)
Classes
class RedisClient (*args, **kwargs)- 
A simple attribute-based namespace.
SimpleNamespace(**kwargs)
base class implementation for any class with fields which supports getting/setting raw data for any instance fields.
any instance can have an optional name and a parent.
class Person(Base): name = fields.String() age = fields.Float() p = Person(name="ahmed", age="19") print(p.name, p.age)Args
parent_:Base, optional- parent instance. Defaults to None.
 instance_name_:str, optional- instance name. Defaults to None.
 **values- any given field values to initiate the instance with
 
Expand source code
class RedisClient(Client): hostname = fields.String(default="localhost") port = fields.Integer(default=6379) password = fields.Secret(default="") def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.__client = None def _attr_updated(self, name, value): super()._attr_updated(name, value) # this will allow other people to listen to this event too event = RedisClientAttributeUpdated(self, name, value) events.notify(event) # reset client self.__client = None def __dir__(self): return list(self._fields.keys()) + dir(self.redis_client) @property def redis_client(self): if not self.__client: if self.password: self.__client = Redis(self.hostname, self.port, password=self.password) else: self.__client = Redis(self.hostname, self.port) return self.__client def is_running(self): try: return self.redis_client.ping() except exceptions.ConnectionError: return False def __getattr__(self, k): # forward non found attrs to self.redis_client return getattr(self.redis_client, k)Ancestors
Instance variables
var hostname- 
getter method this property
will call
_get_value, which would if the value is already defined and will get the default value if notReturns
any- the field value
 
Expand source code
def getter(self): """ getter method this property will call `_get_value`, which would if the value is already defined and will get the default value if not Returns: any: the field value """ return self._get_value(name, field) var password- 
getter method this property
will call
_get_value, which would if the value is already defined and will get the default value if notReturns
any- the field value
 
Expand source code
def getter(self): """ getter method this property will call `_get_value`, which would if the value is already defined and will get the default value if not Returns: any: the field value """ return self._get_value(name, field) var port- 
getter method this property
will call
_get_value, which would if the value is already defined and will get the default value if notReturns
any- the field value
 
Expand source code
def getter(self): """ getter method this property will call `_get_value`, which would if the value is already defined and will get the default value if not Returns: any: the field value """ return self._get_value(name, field) var redis_client- 
Expand source code
@property def redis_client(self): if not self.__client: if self.password: self.__client = Redis(self.hostname, self.port, password=self.password) else: self.__client = Redis(self.hostname, self.port) return self.__client 
Methods
def is_running(self)- 
Expand source code
def is_running(self): try: return self.redis_client.ping() except exceptions.ConnectionError: return False 
Inherited members
 class RedisClientAttributeUpdated (instance, name, new_value)- 
Expand source code
class RedisClientAttributeUpdated(AttributeUpdateEvent): passAncestors