Skip to content

Instantly share code, notes, and snippets.

@jtallieu
Created August 17, 2016 20:16
Show Gist options
  • Save jtallieu/dd77ae2e53726396fd92879cc611d1c3 to your computer and use it in GitHub Desktop.
Save jtallieu/dd77ae2e53726396fd92879cc611d1c3 to your computer and use it in GitHub Desktop.
import dhashpy
_dhash = None
_host = None
_port = None
class DHash(object):
"""Wrap the dhashpy to make the simple things simpler"""
def __init__(self, dhash):
self._dhash = dhash
def watch_new(self, key):
"""Get a watch"""
# TODO: handle exceptions?
return dhashpy.watch_new(self._dhash, key)
def get(self, key, return_on=dhashpy.DWATCH_EVENT_VALID):
"""get a value"""
# TODO: handle exceptions?
watch = self.watch_new(key)
return dhashpy.watch_get(watch, dhashpy.DWATCH_EVENT_VALID, return_on)
def set(self, key, value, return_on=dhashpy.DWATCH_EVENT_CONFIRMED):
"""Update a key"""
watch = self.watch_new(key)
res = dhashpy.watch_update(watch, return_on, value)
# TODO: What should we return here, do we need to do things with the result
return res
def get_and_watch(self, key, return_on=dhashpy.DWATCH_EVENT_VALID):
# TODO: handle exceptions?
watch = self.watch_new(key)
value = dhashpy.watch_get(watch, dhashpy.DWATCH_EVENT_VALID, return_on)
return value, watch
def get_value(key):
"""Convenience to get a key value"""
return get_dhash().get(key)
def set_value(key, value):
"""Set a value"""
return get_dhash().set(key, value)
def get_dhash():
global _dhash
if not _dhash:
raise Exception("No DHASH object available, try calling init_dhash")
return _dhash
# This is the method that should be called to setup DHASH
def init_dhash(host, port):
global _host, _port, _dhash
_host = host
_port = port
_dhash = DHash(dhashpy.dhash_new(ipaddr=_host, port=_port))
return _dhash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment