[Tutor] Search package for path to class

Dennis Lee Bieber wlfraed at ix.netcom.com
Wed May 11 18:47:14 EDT 2022


On Wed, 11 May 2022 23:13:36 +0200, Julius Hamilton
<juliushamilton100 at gmail.com> declaimed the following:

>find(“ServiceRunner”)
>
>and return the result
>
>“yapapi.services.service_runner” - assuming I already imported yapapi.
>
>How would this be possible?
>

	I suspect you would have to perform a recursive walk of each visible
module, keeping track of the levels (parent modules), checking if the
target is in the dir(modulepath), and return the module path for each match
(I say each as I'd recommend not bailing out on the first find -- you may
discover names are used in multiple modules, or a parent did a "from xyz
import name").

	Problem: dir() returns strings, and type() just says it is a string.

	Instead, use sys.modules (which appears to obviate the need for
recursion)

	Unfortunately, that is a long list even for an empty "shell"

C:\Users\Wulfraed>python
Python ActivePython 3.8.2 (ActiveState Software Inc.) based on
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for item in sys.modules:
...   print(item)
...
sys
builtins
_frozen_importlib
_imp
_warnings
_frozen_importlib_external
_io
marshal
nt
_thread
_weakref
winreg
time
zipimport
_codecs
codecs
encodings.aliases
encodings
encodings.utf_8
encodings.cp1252
_signal
__main__
encodings.latin_1
_abc
abc
io
_stat
stat
_collections_abc
genericpath
ntpath
os.path
os
_sitebuiltins
_locale
_bootlocale
types
importlib._bootstrap
importlib._bootstrap_external
warnings
importlib
importlib.machinery
importlib.abc
_operator
operator
keyword
_heapq
heapq
itertools
reprlib
_collections
collections
_functools
functools
contextlib
importlib.util
paste
google
google.cloud
google.logging
google.iam
logilab
mpl_toolkits
pywin32_bootstrap
repoze
sphinxcontrib
zope
site
__future__
pyreadline.py3k_compat
pyreadline.unicode_helper
_socket
collections.abc
math
select
selectors
enum
errno
socket
_sre
sre_constants
sre_parse
sre_compile
copyreg
re
token
tokenize
linecache
traceback
_weakrefset
weakref
_string
string
threading
atexit
logging
_struct
struct
_compat_pickle
_pickle
pickle
_queue
queue
copy
logging.handlers
pyreadline.logger
_ctypes
ctypes._endian
ctypes
ctypes.wintypes
pyreadline.keysyms.winconstants
pyreadline.keysyms.common
pyreadline.keysyms.keysyms
pyreadline.keysyms
pyreadline.clipboard.win32_clipboard
pyreadline.clipboard
pyreadline.lineeditor
pyreadline.lineeditor.wordmatcher
pyreadline.lineeditor.lineobj
pyreadline.lineeditor.history
posixpath
fnmatch
glob
pyreadline.error
pyreadline.modes.basemode
pyreadline.modes.emacs
pyreadline.modes.notemacs
pyreadline.modes.vi
pyreadline.modes
pyreadline.console.ansi
zlib
_compression
_bz2
bz2
_lzma
lzma
shutil
signal
msvcrt
_winapi
subprocess
ctypes.util
pyreadline.console.event
pyreadline.console.console
pyreadline.console
pyreadline.release
pyreadline.rlmain
pyreadline
readline
rlcompleter
>>>

	For each, check if dir() has the sought after name... (I just did a
loop over some -- you probably want something like
		if findname in dir(item):
			print(item, findname)
)

>>> for item in sys.modules:
... 	if item.startswith("google"):
... 		for names in dir(item):
... 			print(item, names)
... 			
google __add__
google __class__
google __contains__
google __delattr__
google __dir__
google __doc__
google __eq__
google __format__
google __ge__
google __getattribute__
google __getitem__
google __getnewargs__
google __gt__
google __hash__
google __init__
google __init_subclass__
google __iter__
google __le__
google __len__
google __lt__
google __mod__
google __mul__
google __ne__
google __new__
google __reduce__
google __reduce_ex__
google __repr__
google __rmod__
google __rmul__
google __setattr__
google __sizeof__
google __str__
google __subclasshook__
google capitalize
google casefold
google center
google count
google encode
google endswith
google expandtabs
google find
google format
google format_map
google index
google isalnum
google isalpha
google isascii
google isdecimal
google isdigit
google isidentifier
google islower
google isnumeric
google isprintable
google isspace
google istitle
google isupper
google join
google ljust
google lower
google lstrip
google maketrans
google partition
google replace
google rfind
google rindex
google rjust
google rpartition
google rsplit
google rstrip
google split
google splitlines
google startswith
google strip
google swapcase
google title
google translate
google upper
google zfill
google.cloud __add__
google.cloud __class__
google.cloud __contains__
google.cloud __delattr__
google.cloud __dir__
google.cloud __doc__
google.cloud __eq__
google.cloud __format__
google.cloud __ge__
google.cloud __getattribute__
google.cloud __getitem__
google.cloud __getnewargs__
google.cloud __gt__
google.cloud __hash__
google.cloud __init__
google.cloud __init_subclass__
google.cloud __iter__
google.cloud __le__
google.cloud __len__
google.cloud __lt__
google.cloud __mod__
google.cloud __mul__
google.cloud __ne__
google.cloud __new__
google.cloud __reduce__
google.cloud __reduce_ex__
google.cloud __repr__
google.cloud __rmod__
google.cloud __rmul__
google.cloud __setattr__
google.cloud __sizeof__
google.cloud __str__


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list