4 ms·
To be fair, you could have done much the same thing in Python: basic_execute_command = { # snip ### SET COMMANDS ### "sadd": "Add ``value
by BrandonM 16y ago
To be fair, you could have done much the same thing in Python:
basic_execute_command = {
# snip
### SET COMMANDS ###
"sadd": "Add ``value`` to set ``name``"
, "scard": "Return the number of elements in set ``name``"
, "sismember": "Return a boolean indicating if ``value`` is a member of set ``name``"
, "smembers": "Return all members of the set ``name``"
, "smove": "Move ``value`` from set ``src`` to set ``dst`` atomically"
, "spop": "Remove and return a random member of set ``name``"
, "srandmember": "Return a random member of set ``name``"
, "srem": "Remove ``value`` from set ``name``"
# snip
}
def _basic_execute_command (self, cmd):
return lambda *args: self.execute_command(cmd.upper(), *args)
# snip
def __init__ (self, *args, **kwargs):
# snip
for cmd in self.basic_execute_command:
self.__dict__[cmd] = self._basic_execute_command(cmd)
# snip
This is pretty memory-inefficient if you have several redis objects, but I'm sure you could come up with something better than what I hacked together in a few minutes.
I know that Lisp ranks above Python on the metaprogramming scale, but simply adding easy object introspection can allow for cool factoring constructs that a Java (or similar) programmer wouldn't even consider. It's possible to make copy-pasta in any language.
- silentbicycle 16y agoYeah, if you can interpret a data structure at runtime, you don't need to write out the corresponding code at all. You could have all the redis connection instances share the same command dict, or make it a variable in the package.