def distance(self, key_one, key_two): """ Calculate the XOR result between two string variables returned as a long type value. """ val_key_one = hex_to_long(key_one) val_key_two = hex_to_long(key_two) return val_key_one ^ val_key_two
def test_hex_to_long(self): """ Ensure a valid hex value produces the correct result. """ raw = 'abcdef0123456789' result = hex_to_long(raw) self.assertEqual(raw, long_to_hex(result))
def key_in_range(self, key): """ Checks if a key is within the range covered by this k-bucket. Returns a boolean to indicate if a certain key should be placed within this k-bucket. """ if isinstance(key, str): key = hex_to_long(key) return self.range_min <= key < self.range_max
def _kbucket_index(self, key): """ Returns the index of the k-bucket responsible for the specified key string. """ if isinstance(key, str): key = hex_to_long(key) # Bound check for key too small. if key < 0: raise ValueError('Key out of range') for i, bucket in enumerate(self._buckets): if bucket.key_in_range(key): return i # Key was too big given the key space. raise ValueError('Key out of range.')