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.

text.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. from difflib import SequenceMatcher
  4. from kombu import version_info_t
  5. from kombu.five import string_t
  6. def fmatch_iter(needle, haystack, min_ratio=0.6):
  7. for key in haystack:
  8. ratio = SequenceMatcher(None, needle, key).ratio()
  9. if ratio >= min_ratio:
  10. yield ratio, key
  11. def fmatch_best(needle, haystack, min_ratio=0.6):
  12. try:
  13. return sorted(
  14. fmatch_iter(needle, haystack, min_ratio), reverse=True,
  15. )[0][1]
  16. except IndexError:
  17. pass
  18. def version_string_as_tuple(s):
  19. v = _unpack_version(*s.split('.'))
  20. # X.Y.3a1 -> (X, Y, 3, 'a1')
  21. if isinstance(v.micro, string_t):
  22. v = version_info_t(v.major, v.minor, *_splitmicro(*v[2:]))
  23. # X.Y.3a1-40 -> (X, Y, 3, 'a1', '40')
  24. if not v.serial and v.releaselevel and '-' in v.releaselevel:
  25. v = version_info_t(*list(v[0:3]) + v.releaselevel.split('-'))
  26. return v
  27. def _unpack_version(major, minor=0, micro=0, releaselevel='', serial=''):
  28. return version_info_t(int(major), int(minor), micro, releaselevel, serial)
  29. def _splitmicro(micro, releaselevel='', serial=''):
  30. for index, char in enumerate(micro):
  31. if not char.isdigit():
  32. break
  33. else:
  34. return int(micro or 0), releaselevel, serial
  35. return int(micro[:index]), micro[index:], serial