Test callables without raising exceptions
Project description
applicable.py
applicable.py is a very basic package with a single function: applicable(). It is simply used to test if a callable can be called with a set of arguments without raising an error.
This package must be used with Python 3.6 or higher.
The signature of applicable() is:
applicable(callable: Callable,
*args: Any,
**kwargs: Any,
ret_result: bool = True,
ret_exc: bool = True) -> Any
* Note: This is not the actual signature, as you can probably tell. The actual signature (and the one type checkers and other tools will give you) is applicable(callable: Callable, *args: Any, **kwargs: Any). As noted in the docstring (applicable.__doc__), ret_result and ret_exc are special keyword arguments, and it would be preferred that they be used after the callable arguments.
Arguments:
callable: The callable to be testedret_result: Whether to returncallable(*args, **kwargs)upon no error.Trueby default. If false,applicable()will returnTrueupon no error.ret_exc: Type or value to return upon error. Defaults to_FalseException. With the exception of_FalseException(which is returned as_FalseException(exception_instance)), the default value for the type is returned, e.g.,int()(which is0),bool()(False),Noneifret_excis a type. Ifret_excis a value, the value is returned. Changed in version 1.1.0:ret_excwas previously a boolean value specifying whether to return a_FalseExceptionor not.argsandkwargs: The arguments that are passed tocallable().
This function will usually be used in an if statement like the following:
from applicable import applicable
val = applicable(SomeCallable, 'arg!', a_kwarg='kwarg!')
if val == False or isinstance(val, _FalseException):
# Do somthing with val
else:
# Do something with the exception; use val.cls for the exception class
More examples, to show the full functionality of applicable() (and _FalseException):
>>> from applicable import applicable
>>> a = applicable(int, '4')
>>> a
4
>>>
>>> a = applicable(int, 'whoops')
>>> a
applicable._FalseException(exc_inst=ValueError(...))
>>> a.cls
<class 'ValueError'>
>>> a.inst
ValueError("invalid literal for int() with base 10: 'whoops'")
>>> bool(a)
False
>>>
>>> a = applicable(complex, 4, imag=3)
>>> a
(4+3j)
>>>
>>> a = applicable(complex, 4, imag=3, ret_result=False)
>>> a
True
>>> a = applicable(int, 'whoops', ret_exc=bool)
>>> a
False
>>> applicable(int, 'whoops', ret_exc=None)
None
>>> applicable(int, 'whoops', ret_exc=4)
4
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file applicable-1.1.1.tar.gz.
File metadata
- Download URL: applicable-1.1.1.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
861b81463aea5e7003d8cd7728a488e80e4bdcb3558479c8ab623254bbc35153
|
|
| MD5 |
f7f42e1d4f79bde9bdfd4f1dc9c2819f
|
|
| BLAKE2b-256 |
adae0210cc130efed2200b6ccb3eece7eab7a739614d34b3c2813403dfa0a456
|
File details
Details for the file applicable-1.1.1-py3-none-any.whl.
File metadata
- Download URL: applicable-1.1.1-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c3f92bca12767eb3977e747d114478393cd118f3c6ce84d2278c0698bfc0226
|
|
| MD5 |
bd0b5b84ba13eb689a05c7fe51c71df3
|
|
| BLAKE2b-256 |
a58efad7f8b13755fa826558ac32838e53c74249628a95bb02f95565a8f9452e
|