mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
jwutils.misc: Add get_derived_classes()
get_derived_classes(mod, baseclass) returns a list of class definitions from a module which are subclasses of baseclass. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
fb6b205440
commit
6d6c48e6b8
1 changed files with 16 additions and 0 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
import os, errno
|
import os, errno
|
||||||
import atexit
|
import atexit
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import inspect
|
||||||
|
from jwutils import log
|
||||||
|
|
||||||
_tmpfiles = set()
|
_tmpfiles = set()
|
||||||
|
|
||||||
|
|
@ -46,4 +48,18 @@ def object_builtin_name(o, full=True): # export
|
||||||
return o.__class__.__name__ # Avoid reporting __builtin__
|
return o.__class__.__name__ # Avoid reporting __builtin__
|
||||||
return module + '.' + o.__class__.__name__
|
return module + '.' + o.__class__.__name__
|
||||||
|
|
||||||
|
def get_derived_classes(mod, base): # export
|
||||||
|
members = inspect.getmembers(mod, inspect.isclass)
|
||||||
|
r = []
|
||||||
|
for name, c in members:
|
||||||
|
log.slog(log.DEBUG, "found ", name)
|
||||||
|
if inspect.isabstract(c):
|
||||||
|
log.slog(log.DEBUG, " is abstract")
|
||||||
|
continue
|
||||||
|
if not base in inspect.getmro(c):
|
||||||
|
log.slog(log.DEBUG, " is not derived from", base, "only", inspect.getmro(c))
|
||||||
|
continue
|
||||||
|
r.append(c)
|
||||||
|
return r
|
||||||
|
|
||||||
atexit.register(_cleanup)
|
atexit.register(_cleanup)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue