Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. >>> import base64, binascii, uuid
  2. >>>
  3. >>> def encode(uid: uuid.UUID) -> str:
  4. ... return base64.urlsafe_b64encode(uid.bytes).decode().rstrip("=")
  5. ...
  6. >>> def decode(uid: str) -> uuid.UUID:
  7. ... return uuid.UUID(binascii.b2a_hex(base64.urlsafe_b64decode(uid_b64 + "==")).decode())
  8. ...
  9. >>> uid = uuid.UUID("12546fd8-6dd9-4923-9c0a-f1fefadd3e2b")
  10. >>> uid
  11. UUID('12546fd8-6dd9-4923-9c0a-f1fefadd3e2b')
  12. >>> uid_b64 = encode(uid)
  13. >>> uid_b64
  14. 'ElRv2G3ZSSOcCvH--t0-Kw'
  15. >>> decode(uid_b64)
  16. UUID('12546fd8-6dd9-4923-9c0a-f1fefadd3e2b')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement