Message323838
My apologies for the tone of my remark. I am grateful to you and others
who donate their time to develop the code.
I'm attaching the wrapper code that I created to work around the problem.
Phillip
def expander(paths='./*'):
"""
OVERVIEW
This function is a generator, i.e., creates an iterator that recursively
searches a list of folders in an incremental fashion. This approach is
advantageous when the folder tree(s) to be searched are large and the
item of
interest is likely to be found early in the process.
INPUTS
`paths` must be either (a) a list of folder paths (each of which is a
string)
or (b) a single string containing one or more folder paths separated by
the
OS-specific path delimiter.
Each path in `paths` must be either (a) an existing folder or (b) an
existing
folder followed by '/*' or '\*'. In case (a), the folder string is
copied
from the input (`paths`) to the output result verbatim. In case (b), the
folder string is replaced by an expanded list that includes not only the
base (the portion of the path that remains after the '/*' or '\*' has
been
removed), but all subfolders as well.
RETURN VALUES
The returned value is an iterator.
Invoking the `next` method of the iterator produces one folder path at a
time.
"""
if isinstance(paths, basestring):
paths= paths.split(os.pathsep)
elif not isinstance(paths, list):
raise TypeError("`paths` must be either a string or a list of
strings.")
found= set()
for path in paths:
if path.endswith('/*') or path.endswith('\*'):
# A recursive search of subfolders is required:
for item in os.walk(path[:-2]):
base= os.path.abspath(item[0])
new= [os.path.join(base, nested) for nested in item[1]]
for item in new:
if not item in found:
found.add(item)
yield item
else:
# No recursive search is required:
if not item in found:
found.add(item)
yield item
# end for path in paths
def find_module(module_name, in_folders=[]):
"""
This function finds a module and return the fully-qualified file name.
Folders from `in_folders`, if specified, are search first, followed by
folders in the global `import_path` list.
If any folder name in `in_folders` or `import_path` ends with an
asterisk,
indicating that a recursive search is required, `files.expander` is
invoked to create iterators that return one folder at a time, and
`imp.find_module` is invoked separately for each of these folders.
EXPLICIT INPUTS
`module_name` is the unqualified name of the module to be found.
`in_folders` is an optional list of additional folders to be searched
before
the folders in `import_path` are searched.
IMPLICIT INPUTS
`import_path` is obtained from the global namespace.
RETURN VALUES
If `find_module` is able to find the requested module, it returns the
same
three return values (`f`, `filename`, and `description`) that
`imp.find_module` would return.
"""
if isinstance(in_folders, basestring):
in_folders= [in_folders]
elif not isinstance(in_folders, list):
raise TypeError("If specified, `in_folders` must be either a string
or a "
"list of strings. (A string is wrapped to produce a length-1
list).")
if any([item.endswith('*') for item in in_folders ]) or \
any([item.endswith('*') for item in import_path]):
ex= None
for folder in itertools.chain(
expander(in_folders), expander(import_path)):
try:
return imp.find_module(module_name, in_folders + import_path)
except Exception as ex:
pass
if ex:
raise ex
else:
return imp.find_module(module_name, in_folders + import_path)
On Tue, Aug 21, 2018 at 10:32 AM Brett Cannon <report@bugs.python.org>
wrote:
>
> Brett Cannon <brett@python.org> added the comment:
>
> Saying "the available functionality is massively inefficient" is
> unnecessarily hostile towards those of us who actually wrote and maintain
> that code. Without diving into the code, chances are that requirement is
> there so that the C code can use macros to access the list as efficiently
> as possible.
>
> Now if you want to propose specific changes to importlib's code for it to
> work with iterables instead of just lists then we would be happy to review
> the pull request.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue34417>
> _______________________________________
> |
|
Date |
User |
Action |
Args |
2018-08-21 18:37:08 | Phillip.M.Feldman@gmail.com | set | recipients:
+ Phillip.M.Feldman@gmail.com, brett.cannon, eric.snow |
2018-08-21 18:37:08 | Phillip.M.Feldman@gmail.com | link | issue34417 messages |
2018-08-21 18:37:08 | Phillip.M.Feldman@gmail.com | create | |
|