0

I have the below directory structure in my python project.

checks/
  __init__.py
  check_1.py
  check_2.py
main.py

Contents inside init.py file are as below where Check1 and Check2 are classes inside check_1.py and check_2.py respectively -

__init__.py - 

from .check_1 import Check1
from .check_2 import Check2

Inside main.py, i am creating the below dictionary -

import checks
check_dict={obj:name for name, obj in vars(checks).items() if isinstance(obj, type)}

Current Output -

{<class 'checks.check_1.Check1'>: 'Check1', <class 'checks.check_2.Check2'>: 'Check2'}

Expect Output -

{check_1: 'Check1',check_2: 'Check2'}
1
  • 1
    You have asked essentially the same question 4 times now. Perhaps you might step back and think if there is a more fundamental question you need answered Commented May 26, 2021 at 20:08

1 Answer 1

1

__module__ will get you the name of the module, eg. checks.check_1. If you only need check_1 you can split the string:

check_dict={obj.__module__.split('.')[1]:name for name, obj in vars(checks).items() if isinstance(obj, type)}
Sign up to request clarification or add additional context in comments.

1 Comment

The problem is that check1 is coming as a string value but in reality it is a class name of type BaseCheck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.