utils.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. def cmp_to_key(mycmp):
  4. """
  5. List sort related function
  6. Convert a cmp= function into a key= function
  7. """
  8. class K(object):
  9. def __init__(self, obj, *args):
  10. self.obj = obj
  11. def __lt__(self, other):
  12. return mycmp(self.obj, other.obj) < 0
  13. def __gt__(self, other):
  14. return mycmp(self.obj, other.obj) > 0
  15. def __eq__(self, other):
  16. return mycmp(self.obj, other.obj) == 0
  17. def __le__(self, other):
  18. return mycmp(self.obj, other.obj) <= 0
  19. def __ge__(self, other):
  20. return mycmp(self.obj, other.obj) >= 0
  21. def __ne__(self, other):
  22. return mycmp(self.obj, other.obj) != 0
  23. return K
  24. def current_date_for_filename() -> str:
  25. """
  26. ISO8601 current date, adapted to be used in filename (for
  27. webdav feature for example), with trouble-free characters.
  28. :return: current date as string like "2018-03-19T15.49.27.246592"
  29. """
  30. # INFO - G.M - 19-03-2018 - As ':' is in transform_to_bdd method in
  31. # webdav utils, it may cause trouble. So, it should be replaced to
  32. # a character which will not change in bdd.
  33. return datetime.datetime.now().isoformat().replace(':', '.')