def __init__(self, id, salt='', min_length=0, alphabet=Hashids.ALPHABET): self._salt = salt self._min_length = min_length self._alphabet = alphabet self._hashids = Hashids(salt=self._salt, min_length=self._min_length, alphabet=self._alphabet) # First see if we were given an already-encoded and valid Hashids string value = self.decode(id) # Since zero also legit unsigned int if value is not None: self._id = value self._hashid = id else: # Next see if it's a positive integer try: id = int(id) except (TypeError, ValueError): raise ValueError( "id must be a positive integer or valid Hashid value") if not _is_uint(id): raise ValueError("id must be a positive integer") # Finally, set our internal values self._id = id self._hashid = self.encode(id)
def __init__(self, id, salt='', min_length=0, alphabet=Hashids.ALPHABET, hashids=None): if hashids is not None and salt: raise ValueError("Cannot use hashids and salt at the same time") self._salt = salt self._min_length = min_length self._alphabet = alphabet self._hashids = hashids or Hashids(salt=self._salt, min_length=self._min_length, alphabet=self._alphabet) # First see if we were given an already-encoded and valid Hashids string value = self.decode(id) if value is not None: self._id = value self._hashid = id else: # Next see if it's a positive integer try: id = int(id) except (TypeError, ValueError): raise ValueError( "id must be a positive integer or valid Hashid value") if not _is_uint(id): raise ValueError("id must be a positive integer") # Finally, set our internal values self._id = id self._hashid = self.encode(id)
def set(self, id): value = self.decode(id) if value: self.id = value self.hashid = id return self.hashid try: id = int(id) except (TypeError, ValueError): raise ValueError( "id must be a positive integer or valid Hashid value") if not _is_uint(id): raise ValueError("id must be a positive integer") self.id = id self.hashid = self.encode(id) return self.hashid
def __init__(self, id, salt='', min_length=0, alphabet=Hashids.ALPHABET, hashids=None, hashids_cpp=None, prefix=None): if hashids is None: self._salt = salt self._min_length = min_length self._alphabet = alphabet self._prefix = prefix self._hashids = Hashids(salt=self._salt, min_length=self._min_length, alphabet=self._alphabet) self._hashids_cpp = Hashids_cpp(self._salt, self._min_length, self._alphabet) else: self._hashids = hashids self._hashids_cpp = hashids_cpp self._salt = hashids._salt self._min_length = hashids._min_length self._alphabet = hashids._alphabet self._prefix = prefix # First see if we were given an already-encoded and valid Hashids string value = self.decode(id) if value is not None: self._id = value self._hashid = id else: # Next see if it's a positive integer try: id = int(id) except (TypeError, ValueError): raise ValueError( "id must be a positive integer or valid Hashid value") if not _is_uint(id): raise ValueError("id must be a positive integer") # Finally, set our internal values self._id = id # If the id is numerical, we only lazily compute the hashid itself # as it might not be always needed self._hashid = None