m2m模型翻译
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.

76 lines
2.1 KiB

6 months ago
  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. from datetime import timedelta
  19. # sys.maxsize:
  20. # An integer giving the maximum value a variable of type Py_ssize_t can take.
  21. MAX_WAIT = sys.maxsize / 2
  22. def find_ordinal(pos_num: int) -> str:
  23. # See: https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
  24. if pos_num == 0:
  25. return "th"
  26. elif pos_num == 1:
  27. return "st"
  28. elif pos_num == 2:
  29. return "nd"
  30. elif pos_num == 3:
  31. return "rd"
  32. elif 4 <= pos_num <= 20:
  33. return "th"
  34. else:
  35. return find_ordinal(pos_num % 10)
  36. def to_ordinal(pos_num: int) -> str:
  37. return f"{pos_num}{find_ordinal(pos_num)}"
  38. def get_callback_name(cb: typing.Callable[..., typing.Any]) -> str:
  39. """Get a callback fully-qualified name.
  40. If no name can be produced ``repr(cb)`` is called and returned.
  41. """
  42. segments = []
  43. try:
  44. segments.append(cb.__qualname__)
  45. except AttributeError:
  46. try:
  47. segments.append(cb.__name__)
  48. except AttributeError:
  49. pass
  50. if not segments:
  51. return repr(cb)
  52. else:
  53. try:
  54. # When running under sphinx it appears this can be none?
  55. if cb.__module__:
  56. segments.insert(0, cb.__module__)
  57. except AttributeError:
  58. pass
  59. return ".".join(segments)
  60. time_unit_type = typing.Union[int, float, timedelta]
  61. def to_seconds(time_unit: time_unit_type) -> float:
  62. return float(time_unit.total_seconds() if isinstance(time_unit, timedelta) else time_unit)