get_obj_attr

pyhelpers.ops.get_obj_attr(obj, col_names=None, as_dataframe=False)[source]

Retrieve main attributes of an object.

Parameters:
  • obj (object) – Object from which to retrieve attributes, e.g. an instance of a class.

  • col_names (list | None) – List of column names for renaming the returned dataframe when as_dataframe=True; defaults to None.

  • as_dataframe (bool) – Whether to return the data in tabular format (dataframe); defaults to False.

Returns:

The main attributes of the given obj.

Return type:

list | pandas.DataFrame

Examples:

>>> from pyhelpers.ops import get_obj_attr
>>> from pyhelpers.dbms import PostgreSQL
>>> postgres = PostgreSQL()
Password (postgres@localhost:5432): ***
Connecting postgres:***@localhost:5432/postgres ... Successfully.
>>> obj_attr = get_obj_attr(postgres, as_dataframe=True)
>>> obj_attr.head()
          attribute       value
0  DEFAULT_DATABASE    postgres
1   DEFAULT_DIALECT  postgresql
2    DEFAULT_DRIVER    psycopg2
3      DEFAULT_HOST   localhost
4      DEFAULT_PORT        5432
>>> obj_attr.attribute.to_list()
['DEFAULT_DATABASE',
 'DEFAULT_DIALECT',
 'DEFAULT_DRIVER',
 'DEFAULT_HOST',
 'DEFAULT_PORT',
 'DEFAULT_SCHEMA',
 'DEFAULT_USERNAME',
 'address',
 'database_info',
 'database_name',
 'engine',
 'host',
 'port',
 'url',
 'username']