m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

6 months ago
  1. from __future__ import absolute_import
  2. import binascii
  3. import weakref
  4. from kafka.vendor import six
  5. if six.PY3:
  6. MAX_INT = 2 ** 31
  7. TO_SIGNED = 2 ** 32
  8. def crc32(data):
  9. crc = binascii.crc32(data)
  10. # py2 and py3 behave a little differently
  11. # CRC is encoded as a signed int in kafka protocol
  12. # so we'll convert the py3 unsigned result to signed
  13. if crc >= MAX_INT:
  14. crc -= TO_SIGNED
  15. return crc
  16. else:
  17. from binascii import crc32
  18. class WeakMethod(object):
  19. """
  20. Callable that weakly references a method and the object it is bound to. It
  21. is based on https://stackoverflow.com/a/24287465.
  22. Arguments:
  23. object_dot_method: A bound instance method (i.e. 'object.method').
  24. """
  25. def __init__(self, object_dot_method):
  26. try:
  27. self.target = weakref.ref(object_dot_method.__self__)
  28. except AttributeError:
  29. self.target = weakref.ref(object_dot_method.im_self)
  30. self._target_id = id(self.target())
  31. try:
  32. self.method = weakref.ref(object_dot_method.__func__)
  33. except AttributeError:
  34. self.method = weakref.ref(object_dot_method.im_func)
  35. self._method_id = id(self.method())
  36. def __call__(self, *args, **kwargs):
  37. """
  38. Calls the method on target with args and kwargs.
  39. """
  40. return self.method()(self.target(), *args, **kwargs)
  41. def __hash__(self):
  42. return hash(self.target) ^ hash(self.method)
  43. def __eq__(self, other):
  44. if not isinstance(other, WeakMethod):
  45. return False
  46. return self._target_id == other._target_id and self._method_id == other._method_id
  47. class Dict(dict):
  48. """Utility class to support passing weakrefs to dicts
  49. See: https://docs.python.org/2/library/weakref.html
  50. """
  51. pass