utils.py 1.2KB

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