Skip to content

Commit

Permalink
Add get_obj_attr() to ops.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeqfu committed Dec 20, 2020
1 parent 77252b6 commit 9f10906
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pyhelpers/ops.py
Expand Up @@ -4,6 +4,7 @@

import collections.abc
import datetime
import inspect
import itertools
import math
import os
Expand Down Expand Up @@ -69,6 +70,48 @@ def confirmed(prompt=None, resp=False, confirmation_required=True):
return True


def get_obj_attr(obj, col_names=None):
"""
Get main attributes of an object.
:param obj: a object, e.g. a class
:type obj: object
:param col_names: a list of column names
:type col_names: list
:return: tabular data of the main attributes of the given object
:rtype: pandas.DataFrame
**Test**::
>>> from pyhelpers import get_obj_attr, PostgreSQL
>>> postgres = PostgreSQL(host='localhost', port=5432, username='postgres',
... database_name='postgres')
Password (postgres@localhost:5432): ***
Connecting postgres:***@localhost:5432/postgres ... Successfully.
>>> obj_attr = get_obj_attr(postgres)
>>> print(obj_attr.head())
Attribute Value
0 address postgres:***@localhost:5432/postgres
1 backend postgresql
2 connection <sqlalchemy.pool.base._ConnectionFairy object ...
3 database_info {'drivername': 'postgresql+psycopg2', 'host': ...
4 database_name postgres
"""

if col_names is None:
col_names = ['Attribute', 'Value']

all_attrs = inspect.getmembers(obj, lambda x: not (inspect.isroutine(x)))

attrs = [x for x in all_attrs if not re.match(r'^__?', x[0])]

attrs_tbl = pd.DataFrame(attrs, columns=col_names)

return attrs_tbl


""" Basic data manipulation -------------------------------------------------------- """


Expand Down

0 comments on commit 9f10906

Please sign in to comment.