Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatic redis lookup for python objects
    text
    copied!<p>I'm trying to implement classes for which redis actually holds the attributes, but the user of the class is not aware of this (ie. object persistence across multiple clients). I know there are a few libs that wrap redis for python but none do exactly this in this simple way (but please correct me if I'm wrong on this!)</p> <p>I've successfully implemented automatic redis storage of attributes but I can't seem to get retrieval to work using <code>__getattribute__</code> without infinite recursion blowing it up. I think I am being careful about using <code>object.__getattribute__</code> etc. but obviously I must be missing something:</p> <pre><code>class redisStored(object): global redis # A class method to implement object retrieval from redis # Call &lt;Classname&gt;.load(&lt;id&gt;) to create a new instance which exists in redis @classmethod def load(cls,id): # Create the object obj = cls.__new__(cls) # Set id without calling obj's __setattr__ object.__setattr__(obj,'id',int(id)) # Return the object we created return obj def __str__(self): # Return "&lt;ClassName&gt;:&lt;id&gt;" to differentiate ids in redis # You can use this to say redis.hgetall(g) where g is the instance return "%s:%s" % (self.__class__.__name__, str(object.__getattribute__(self,'id'))) # self.id here ^ would cause infinite recursion # This works fine def __setattr__(self,name,value): redis.hset(self,name,value) return object.__setattr__(self,name,value) # This blows up with infinite recursion, WHY?? def __getattribute__(self,name): _val = redis.hget(self,name) if not _val: return object.__getattribute__(self,name) else: return _val </code></pre> <p>If I trace this it blows up inside <code>_val = redis.hget(self,name)</code> but I can't figure out why. Thanks for any help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload