utility.py 712 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # -*- coding: utf-8 -*-
  2. """
  3. utility functions
  4. """
  5. def cmp(a, b):
  6. return (a > b) - (a < b)
  7. def cmp_str(element1, element2):
  8. """compare number in str format correctley
  9. """
  10. return cmp(float(element1), float(element2))
  11. def get_num_list_from_str(stemp):
  12. """
  13. if float(stemp) works, return [stemp]
  14. else return, stemp.split(',')
  15. """
  16. try:
  17. float(stemp)
  18. return [stemp]
  19. except ValueError:
  20. return stemp.split(',')
  21. def qid_to_key(value_list, sep=';'):
  22. """convert qid list to str key
  23. value (splited by sep). This fuction is value safe, which means
  24. value_list will not be changed.
  25. return str list.
  26. """
  27. return sep.join(value_list)