Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#6 build(deps): bump tqdm from 4.66.1 to 4.66.3

Open
dependabot[bot] wants to merge 1 commits into ncusi:main from ncusi:dependabot/pip/tqdm-4.66.3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  1. """Module with various generic functional tools and function-related utilities
  2. """
  3. import cProfile
  4. import contextlib
  5. import inspect
  6. import io
  7. import pstats
  8. import time
  9. from datetime import datetime, timedelta
  10. from functools import wraps
  11. from pathlib import Path
  12. def describe_func_call(func, *args, **kwargs):
  13. """Return string with function call details
  14. Based on https://stackoverflow.com/a/6278457/46058 response to
  15. https://stackoverflow.com/questions/6200270/decorator-that-prints-function-call-details-argument-names-and-values
  16. NOTE: does not work for built-in functions like 'print':
  17. >>> describe_func_call(print, "message")
  18. [...]
  19. ValueError: no signature found for builtin <built-in function print>.
  20. Example:
  21. >>> def foo(bar):
  22. ... pass
  23. ...
  24. >>> describe_func_call(foo, "message")
  25. "foo(bar='message')"
  26. :param func: Called function to describe
  27. :type: typing.Callable
  28. :param args: Positional parameters to function call being described
  29. :param kwargs: Named parameters to function call being described
  30. :return: Function call details
  31. :rtype: str
  32. """
  33. func_args = inspect.signature(func).bind(*args, **kwargs).arguments
  34. func_args_str = ", ".join(map("{0[0]}={0[1]!r}".format, func_args.items()))
  35. if func.__module__ == "__main__":
  36. return f"{func.__qualname__}({func_args_str})"
  37. else:
  38. return f"{func.__module__}.{func.__qualname__}({func_args_str})"
  39. def throttled(delay):
  40. """Delay returning value of decorated function by `delay` seconds.
  41. It checks if the `delay` is greater than zero, because you cannot
  42. time.sleep() for negative time.
  43. :param float delay: time in seconds to sleep before returning value
  44. """
  45. def decorator_throttled(func):
  46. @wraps(func)
  47. def wrapper_throttle(*args, **kwargs):
  48. value = func(*args, **kwargs)
  49. if delay > 0:
  50. time.sleep(delay)
  51. return value
  52. return wrapper_throttle
  53. return decorator_throttled
  54. def timed(func):
  55. """Decorator that times wrapped function, and prints its execution time
  56. Example:
  57. >>> @timed
  58. ... def bar():
  59. ... time.sleep(1.1)
  60. ... return True
  61. ...
  62. >>> bar()
  63. Start time: 2023-08-23 12:00:56.486206 ==============================
  64. End time: 2023-08-23 12:00:57.602275 ================================
  65. Function bar() {} took 1.1028 seconds = 0:00:01.102849
  66. True
  67. Based on https://dev.to/kcdchennai/python-decorator-to-measure-execution-time-54hk
  68. """
  69. @wraps(func)
  70. def wrapper_timed(*args, **kwargs):
  71. print(f"Start time: {datetime.now()} ==============================")
  72. start_time_ns = time.perf_counter_ns()
  73. result = func(*args, **kwargs)
  74. end_time_ns = time.perf_counter_ns()
  75. print(f"End time: {datetime.now()} ================================")
  76. total_time_sec = (end_time_ns - start_time_ns)/1e9
  77. # NOTE: it could have used `describe_func_call()`
  78. print(f'Function {func.__name__}{args} {kwargs} took {total_time_sec:.4f} seconds',
  79. f'= {timedelta(seconds=total_time_sec)}')
  80. return result
  81. return wrapper_timed
  82. @contextlib.contextmanager
  83. def profile(basename: Path, *args, **kwargs):
  84. """Context manager for profiling
  85. Usage:
  86. >>> def my_function():
  87. ... with profile(Path("/home/ubuntu/profiles/prof")):
  88. ... return 1
  89. ...
  90. Based on code by Ricardo Ander-Egg Aguilar (polyrand).
  91. https://ricardoanderegg.com/posts/python-profiling-timing-utils/
  92. https://gist.github.com/polyrand/bb39fb93246ced7464abf52d87fec3a7
  93. :param Path basename: results are saved in `basename`.txt and `basename`.prof
  94. :param args: positional params passed to `cProfile.Profile`
  95. :param kwargs: keyword params passed to `cProfile.Profile`
  96. :rtype: None
  97. """
  98. prof = cProfile.Profile(*args, **kwargs)
  99. prof.enable()
  100. yield
  101. prof.disable()
  102. s = io.StringIO()
  103. sort_by = pstats.SortKey.CUMULATIVE
  104. ps = pstats.Stats(prof, stream=s).strip_dirs().sort_stats(sort_by)
  105. ps.print_stats()
  106. with open(basename.with_suffix(".txt"), "w") as f:
  107. f.write(s.getvalue())
  108. prof.dump_stats(basename.with_suffix(".prof"))
  109. def profiled(basename: Path):
  110. """Decorator for profiling
  111. Uses `profile` context manager, but doesn't allow to pass optional
  112. parameters to `cProfile.Profile` like `profile` did.
  113. Usage:
  114. >>> @profiled(Path("/home/ubuntu/profiles/prof"))
  115. >>> def my_function():
  116. ... return 1
  117. ...
  118. Based on code by Ricardo Ander-Egg Aguilar (polyrand).
  119. https://ricardoanderegg.com/posts/python-profiling-timing-utils/
  120. https://gist.github.com/polyrand/bb39fb93246ced7464abf52d87fec3a7
  121. :param Path basename: results are saved in `basename`.txt and `basename`.prof
  122. """
  123. def decorator_profiled(func):
  124. @wraps(func)
  125. def wrapper_profiled(*args, **kwargs):
  126. with profile(basename):
  127. return func(*args, **kwargs)
  128. return wrapper_profiled
  129. return decorator_profiled
Discard
Tip!

Press p or to see the previous file or, n or to see the next file