Objects

flutils offers the following object utility functions:

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.

Return type

bool

  • True if any of the given *attrs exist on the given obj;

  • False otherwise.

Example

>>> from flutils.objutils import has_any_attrs
>>> has_any_attrs(dict(),'get','keys','items','values','something')
True
has_any_callables(obj, *attrs)[source]

Check if the given obj has ANY of the given attrs and are callable.

Parameters
  • obj (Any) – The object to check.

  • *attrs (str) – The names of the attributes to check.

Return type

bool

  • True if ANY of the given *attrs exist on the given obj and ANY are callable;

  • False otherwise.

Example

>>> from flutils.objutils import has_any_callables
>>> has_any_callables(dict(),'get','keys','items','values','foo')
True
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.

Return type

bool

  • True if all the given *attrs exist on the given obj;

  • False otherwise.

Example

>>> from flutils.objutils import has_attrs
>>> has_attrs(dict(),'get','keys','items','values')
True
has_callables(obj, *attrs)[source]

Check if given obj has all the given attrs and are callable.

Parameters
  • obj (Any) – The object to check.

  • *attrs (str) – The names of the attributes to check.

Return type

bool

  • True if all the given *attrs exist on the given obj and all are callable;

  • False otherwise.

Example

>>> from flutils.objutils import has_callables
>>> has_callables(dict(),'get','keys','items','values')
True
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:

Parameters

obj (Any) – The object to check.

Return type

bool

  • True if the given obj is list-like; :

  • False otherwise.

Examples

>>> from flutils.objutils 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
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.

Return type

bool

  • True if the given obj is an instance of ANY given *classes;

  • False otherwise.

Example

>>> from flutils.objutils 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