mode.utils.times
Time, date and timezone related utilities.
Bucket
Bases: AsyncContextManager
Rate limiting state.
A bucket "pours" tokens at a rate of rate
per second (or over').
Calling bucket.pour()
, pours one token by default, and returns
:const:True
if that amount can be poured now, or :const:False
if the
caller has to wait.
If this returns :const:False
, it's prudent to either sleep or raise
an exception:
if not bucket.pour():
await asyncio.sleep(bucket.expected_time())
If you want to consume multiple tokens in one go then specify the number:
if not bucket.pour(10):
await asyncio.sleep(bucket.expected_time(10))
This class can also be used as an async. context manager, but in that case can only consume one tokens at a time:
async with bucket:
# do something
By default the async. context manager will suspend the current coroutine and sleep until as soon as the time that a token can be consumed.
If you wish you can also raise an exception, instead of sleeping, by
providing the raises
keyword argument:
# hundred tokens in one second, and async with: raises TimeoutError
class MyError(Exception):
pass
bucket = Bucket(100, over=1.0, raises=MyError)
async with bucket:
# do something
Source code in mode/utils/times.py
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 167 168 169 170 171 172 |
|
TokenBucket
Bases: Bucket
Rate limiting using the token bucket algorithm.
Source code in mode/utils/times.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
|
humanize_seconds(secs, *, prefix='', suffix='', sep='', now='now', microseconds=False)
Show seconds in human form.
For example, 60 becomes "1 minute", and 7200 becomes "2 hours".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
secs |
float
|
Seconds to format (as |
required |
prefix |
str
|
can be used to add a preposition to the output (e.g., 'in' will give 'in 1 second', but add nothing to 'now'). |
''
|
suffix |
str
|
same as prefix, adds suffix unless 'now'. |
''
|
sep |
str
|
separator between prefix and number. |
''
|
now |
str
|
Literal 'now'. |
'now'
|
microseconds |
bool
|
Include microseconds. |
False
|
Source code in mode/utils/times.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
humanize_seconds_ago(secs, *, prefix='', suffix=' ago', sep='', now='just now', microseconds=False)
Show seconds in "3.33 seconds ago" form.
If seconds are less than one, returns "just now".
Source code in mode/utils/times.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
|
rate(r)
Convert rate string ("100/m"
, "2/h"
or "0.5/s"
) to seconds.
Source code in mode/utils/times.py
210 211 212 213 |
|
rate_limit(rate, over=1.0, *, bucket_type=TokenBucket, raises=None, loop=None)
Create rate limiting manager.
Source code in mode/utils/times.py
232 233 234 235 236 237 238 239 240 241 |
|
want_seconds(s)
Convert Seconds
to float.
Source code in mode/utils/times.py
244 245 246 247 |
|