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's a boolean
evaluating to False
, warnings will be ignored. If the attribute is a string, then
it is interpreted as an "action" to be performed on the categories specified.
Allowed values are as per warnings.simplefilter, which are:
default
, error
, ignore
, always
, all
, module
, once
??? Example "Suppress all warnings"
??? Example "Suppress onlyUserWarning
"
??? Example "Configuring behaviour at runtime"
class A:
def __init__(self, warn_enabled: bool):
self.warn_enabled = warn_enabled
@suppress_warnings(flag="warn_enabled")
def method(self, ...):
...
??? Example "Raising on RuntimeWarning"
class A:
def __init__(self, warnings: str = "error"):
self.warnings = warnings
@suppress_warnings(flag="warnings")
def method(self, ...):
...
A().method() # Raises RuntimeWarning
Args:
fun: Optional callable to decorate. If provided, the decorator is applied inline.
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 False
, warnings will be ignored. If
it evaluates to a str, then this action will be performed on the categories
specified. Allowed values are as per warnings.simplefilter, which are:
default
, error
, ignore
, always
, all
, module
, once
Returns: Either a decorator (if no function is provided) or the decorated callable.
Source code in src/pydvl/utils/functional.py
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
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
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 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 |
|