Objects¶
flutils offers the following object utility functions.
flutils.
has_any_attrs
(obj, *attrs)[source]¶Check if the given
obj
has ANY of the given*attrs
.
Parameters:
- obj (Any) – The object to check.
- *attrs (str) – The names of the attributes to check.
Returns:
True
if any of the given*attrs
exist on the givenobj
; otherwise,False
.Return type: Example
>>> from flutils import has_any_attrs >>> has_any_attrs(dict(),'get','keys','items','values','something') True
flutils.
has_any_callables
(obj, *attrs)[source]¶Check if the given
obj
has ANY of the givenattrs
and are callable.
Parameters:
- obj (Any) – The object to check.
- *attrs (str) – The names of the attributes to check.
Returns:
True
if ANY of the given*attrs
exist on the givenobj
and ANY are callable; otherwise,False
.Return type: Example
>>> from flutils import has_any_callables >>> has_any_callables(dict(),'get','keys','items','values','foo') True
flutils.
has_attrs
(obj, *attrs)[source]¶Check if given
obj
has all the given*attrs
.
Parameters:
- obj (Any) – The object to check.
- *attrs (str) – The names of the attributes to check.
Returns:
True
if all the given*attrs
exist on the givenobj
; otherwise,False
.Return type: Example
>>> from flutils import has_attrs >>> has_attrs(dict(),'get','keys','items','values') True
flutils.
has_callables
(obj, *attrs)[source]¶Check if given
obj
has all the givenattrs
and are callable.
Parameters:
- obj (Any) – The object to check.
- *attrs (str) – The names of the attributes to check.
Returns:
True
if all the given*attrs
exist on the givenobj
and all are callable; otherwise,False
.Return type: Example
>>> from flutils import has_callables >>> has_callables(dict(),'get','keys','items','values') True
flutils.
is_list_like
(obj)[source]¶Check that given
obj
acts like a list and is iterable.List-like objects are instances of:
List-like objects are NOT instances of:
None
bool
bytes
ChainMap
Counter
OrderedDict
UserDict
UserString
defaultdict
Decimal
dict
float
int
str
- etc…
Parameters: obj (Any) – The object to be checked. Returns: True
if the givenobj
is list-like;False
otherwise.Return type: bool Examples
>>> from flutils import is_list_like >>> is_list_like([1, 2, 3]) True>>> is_list_like(reversed([1, 2, 4])) True>>> is_list_like('hello') False>>> is_list_like(sorted('hello')) True
flutils.
is_subclass_of_any
(obj, *classes)[source]¶Check if the given
obj
is a subclass of any of the given*classes
.
Parameters:
- obj (Any) – The object to check.
- *classes (Any) – The classes to check against.
Returns:
True
if the givenobj
is an instance of ANY given*classes
; otherwiseFalse
.Return type: Example
>>> from flutils import is_subclass_of_any >>> from collections import ValuesView, KeysView, UserList >>> obj = dict(a=1, b=2) >>> is_subclass_of_any(obj.keys(),ValuesView,KeysView,UserList) True