10b34d6b33
instead of recursive bytes2unicode use a proper JSONEncoder to conver bytes->unicode for json.dumps() removing excessive isinstance(data, bytes) checks py3: / -> // and list() around .items() that gets modified in loop moved lbrynet.undecorated to where its actually used and hopefully we can delete it eventually removed build/upload_assets.py, travis can do all this now
29 lines
885 B
Python
29 lines
885 B
Python
from binascii import hexlify
|
|
|
|
from lbrynet.dht import constants
|
|
|
|
|
|
class Distance:
|
|
"""Calculate the XOR result between two string variables.
|
|
|
|
Frequently we re-use one of the points so as an optimization
|
|
we pre-calculate the value of that point.
|
|
"""
|
|
|
|
def __init__(self, key):
|
|
if len(key) != constants.key_bits // 8:
|
|
raise ValueError("invalid key length: %i" % len(key))
|
|
self.key = key
|
|
self.val_key_one = int(hexlify(key), 16)
|
|
|
|
def __call__(self, key_two):
|
|
val_key_two = int(hexlify(key_two), 16)
|
|
return self.val_key_one ^ val_key_two
|
|
|
|
def is_closer(self, a, b):
|
|
"""Returns true is `a` is closer to `key` than `b` is"""
|
|
return self(a) < self(b)
|
|
|
|
def to_contact(self, contact):
|
|
"""A convenience function for calculating the distance to a contact"""
|
|
return self(contact.id)
|