Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_vendor/tenacity/_utils.py: 26%

38 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:48 +0000

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 

17import sys 

18import typing 

19from datetime import timedelta 

20 

21 

22# sys.maxsize: 

23# An integer giving the maximum value a variable of type Py_ssize_t can take. 

24MAX_WAIT = sys.maxsize / 2 

25 

26 

27def find_ordinal(pos_num: int) -> str: 

28 # See: https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers 

29 if pos_num == 0: 

30 return "th" 

31 elif pos_num == 1: 

32 return "st" 

33 elif pos_num == 2: 

34 return "nd" 

35 elif pos_num == 3: 

36 return "rd" 

37 elif 4 <= pos_num <= 20: 

38 return "th" 

39 else: 

40 return find_ordinal(pos_num % 10) 

41 

42 

43def to_ordinal(pos_num: int) -> str: 

44 return f"{pos_num}{find_ordinal(pos_num)}" 

45 

46 

47def get_callback_name(cb: typing.Callable[..., typing.Any]) -> str: 

48 """Get a callback fully-qualified name. 

49 

50 If no name can be produced ``repr(cb)`` is called and returned. 

51 """ 

52 segments = [] 

53 try: 

54 segments.append(cb.__qualname__) 

55 except AttributeError: 

56 try: 

57 segments.append(cb.__name__) 

58 except AttributeError: 

59 pass 

60 if not segments: 

61 return repr(cb) 

62 else: 

63 try: 

64 # When running under sphinx it appears this can be none? 

65 if cb.__module__: 

66 segments.insert(0, cb.__module__) 

67 except AttributeError: 

68 pass 

69 return ".".join(segments) 

70 

71 

72time_unit_type = typing.Union[int, float, timedelta] 

73 

74 

75def to_seconds(time_unit: time_unit_type) -> float: 

76 return float(time_unit.total_seconds() if isinstance(time_unit, timedelta) else time_unit)