_utils.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright 2016 Julien Danjou
  2. # Copyright 2016 Joshua Harlow
  3. # Copyright 2013-2014 Ray Holder
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import sys
  17. import typing
  18. # sys.maxsize:
  19. # An integer giving the maximum value a variable of type Py_ssize_t can take.
  20. MAX_WAIT = sys.maxsize / 2
  21. def find_ordinal(pos_num: int) -> str:
  22. # See: https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
  23. if pos_num == 0:
  24. return "th"
  25. elif pos_num == 1:
  26. return "st"
  27. elif pos_num == 2:
  28. return "nd"
  29. elif pos_num == 3:
  30. return "rd"
  31. elif 4 <= pos_num <= 20:
  32. return "th"
  33. else:
  34. return find_ordinal(pos_num % 10)
  35. def to_ordinal(pos_num: int) -> str:
  36. return f"{pos_num}{find_ordinal(pos_num)}"
  37. def get_callback_name(cb: typing.Callable[..., typing.Any]) -> str:
  38. """Get a callback fully-qualified name.
  39. If no name can be produced ``repr(cb)`` is called and returned.
  40. """
  41. segments = []
  42. try:
  43. segments.append(cb.__qualname__)
  44. except AttributeError:
  45. try:
  46. segments.append(cb.__name__)
  47. except AttributeError:
  48. pass
  49. if not segments:
  50. return repr(cb)
  51. else:
  52. try:
  53. # When running under sphinx it appears this can be none?
  54. if cb.__module__:
  55. segments.insert(0, cb.__module__)
  56. except AttributeError:
  57. pass
  58. return ".".join(segments)