Development of an internal social media platform with personalised dashboards for students
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.

compat.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. kombu.utils.compat
  3. ==================
  4. Helps compatibility with older Python versions.
  5. """
  6. from __future__ import absolute_import
  7. # ############# timedelta_seconds() -> delta.total_seconds ###################
  8. from datetime import timedelta
  9. HAVE_TIMEDELTA_TOTAL_SECONDS = hasattr(timedelta, 'total_seconds')
  10. if HAVE_TIMEDELTA_TOTAL_SECONDS: # pragma: no cover
  11. def timedelta_seconds(delta):
  12. """Convert :class:`datetime.timedelta` to seconds.
  13. Doesn't account for negative values.
  14. """
  15. return max(delta.total_seconds(), 0)
  16. else: # pragma: no cover
  17. def timedelta_seconds(delta): # noqa
  18. """Convert :class:`datetime.timedelta` to seconds.
  19. Doesn't account for negative values.
  20. """
  21. if delta.days < 0:
  22. return 0
  23. return delta.days * 86400 + delta.seconds + (delta.microseconds / 10e5)
  24. # ############# socket.error.errno ###########################################
  25. def get_errno(exc):
  26. """:exc:`socket.error` and :exc:`IOError` first got
  27. the ``.errno`` attribute in Py2.7"""
  28. try:
  29. return exc.errno
  30. except AttributeError:
  31. try:
  32. # e.args = (errno, reason)
  33. if isinstance(exc.args, tuple) and len(exc.args) == 2:
  34. return exc.args[0]
  35. except AttributeError:
  36. pass
  37. return 0
  38. # ############# collections.OrderedDict ######################################
  39. try:
  40. from collections import OrderedDict
  41. except ImportError:
  42. from ordereddict import OrderedDict # noqa