pydvl.utils.functional
¶
Supporting utilities for manipulating functions.
free_arguments
¶
Computes the set of free arguments for a function or [[functools.partial]] object.
All arguments of a function are considered free unless they are set by a
partial. For example, if f = partial(g, a=1)
, then a
is not a free
argument of f
.
PARAMETER | DESCRIPTION |
---|---|
fun
|
A callable or a partial object. |
RETURNS | DESCRIPTION |
---|---|
Set[str]
|
The set of free arguments of |
New in version 0.7.0
Source code in src/pydvl/utils/functional.py
maybe_add_argument
¶
Wraps a function to accept the given keyword parameter if it doesn't already.
If fun
already takes a keyword parameter of name new_arg
, then it is
returned as is. Otherwise, a wrapper is returned which merely ignores the
argument.
PARAMETER | DESCRIPTION |
---|---|
fun
|
The function to wrap
TYPE:
|
new_arg
|
The name of the argument that the new function will accept (and ignore).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Callable
|
A new function accepting one more keyword argument. |
Changed in version 0.7.0
Ability to work with partials.
Source code in src/pydvl/utils/functional.py
suppress_warnings
¶
suppress_warnings(
fun: Callable[P, R] | None = None,
*,
categories: Sequence[Type[Warning]] = (Warning,),
flag: str = "",
) -> Union[Callable[[Callable[P, R]], Callable[P, R]], Callable[P, R]]
Decorator for class methods to conditionally suppress warnings.
The decorated method will execute with warnings suppressed for the specified
categories. If the instance has the attribute named by flag
, and it evaluates to
True
, then suppression will be deactivated.
Suppress only UserWarning
Configuring behaviour at runtime
PARAMETER | DESCRIPTION |
---|---|
fun
|
Optional callable to decorate. If provided, the decorator is applied inline.
TYPE:
|
categories
|
Sequence of warning categories to suppress. |
flag
|
Name of an instance attribute to check for enabling warnings. If the
attribute exists and evaluates to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Callable[[Callable[P, R]], Callable[P, R]], Callable[P, R]]
|
Either a decorator (if no function is provided) or the decorated callable. |
Source code in src/pydvl/utils/functional.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
timed
¶
timed(fun: Callable[P, R]) -> TimedCallable[P, R]
timed(
fun: Callable[P, R] | None = None,
*,
accumulate: bool = False,
logger: Logger | None = None,
) -> Union[
Callable[[Callable[P, R]], TimedCallable[P, R]], TimedCallable[P, R]
]
A decorator that measures the execution time of the wrapped function. Optionally logs the time taken.
Decorator usage
Inline usage
PARAMETER | DESCRIPTION |
---|---|
fun
|
TYPE:
|
accumulate
|
If
TYPE:
|
logger
|
If provided, the execution time will be logged at the logger's level.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Callable[[Callable[P, R]], TimedCallable[P, R]], TimedCallable[P, R]]
|
A decorator that wraps a function, measuring and optionally logging its |
Union[Callable[[Callable[P, R]], TimedCallable[P, R]], TimedCallable[P, R]]
|
execution time. The function will have an attribute |
Union[Callable[[Callable[P, R]], TimedCallable[P, R]], TimedCallable[P, R]]
|
either the time of the last execution or the accumulated total is stored. |
Source code in src/pydvl/utils/functional.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|