util.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import random, time
  2. from datetime import timezone, datetime
  3. UTC = timezone.utc
  4. def random_blob_idx():
  5. return "%030x" % random.randrange(16**30)
  6. def datetime_to_unix(dt):
  7. return int(time.mktime(dt.timetuple()))
  8. def now():
  9. return datetime_to_unix(datetime.now(tz=UTC))
  10. def month_to_unix(month=None):
  11. month_year = month if month is not None else datetime.utcnow().strftime("%m%y")
  12. return datetime.strptime(month_year,"%m%y").timestamp()
  13. def unix_to_datetime(timestamp):
  14. return datetime.utcfromtimestamp(int(timestamp))
  15. task_template = {
  16. "workspace": str,
  17. "title": str,
  18. "tags": list,
  19. "desc": str,
  20. "owner": str,
  21. "assign": list,
  22. "project": list,
  23. "due": int,
  24. "rank": float,
  25. "created_at": int,
  26. "state": str,
  27. "events": list,
  28. "comments": list,
  29. }
  30. def _enforce_task_format(task):
  31. for attr, val in task.items():
  32. val_type = task_template[attr]
  33. if val is None:
  34. assert val_type == list or attr not in ["created"]
  35. continue
  36. assert isinstance(val, val_type)