def by_import_as(self): """ Map from C{import_as} to L{Import}. >>> ImportSet('from aa.bb import cc as dd').by_import_as {'dd': (Import('from aa.bb import cc as dd'),)} @rtype: C{dict} mapping from C{str} to tuple of L{Import}s """ d = defaultdict(list) for imp in self._importset: d[imp.import_as].append(imp) return dict( (k, tuple(sorted(stable_unique(v)))) for k, v in six.iteritems(d))
def by_import_as(self): """ Map from ``import_as`` to `Import`. >>> ImportSet('from aa.bb import cc as dd').by_import_as {'dd': (Import('from aa.bb import cc as dd'),)} :rtype: ``dict`` mapping from ``str`` to tuple of `Import` s """ d = defaultdict(list) for imp in self._importset: d[imp.import_as].append(imp) return dict( (k, tuple(sorted(stable_unique(v)))) for k, v in six.iteritems(d) )
def _get_python_path(env_var_name, default_path, target_dirname): ''' Expand an environment variable specifying pyflyby input config files. - Default to C{default_path} if the environment variable is undefined. - Process colon delimiters. - Replace "-" with C{default_path}. - Expand triple dots. - Recursively traverse directories. @rtype: C{tuple} of C{Filename}s ''' pathnames = _get_env_var(env_var_name, default_path) if pathnames == ["EMPTY"]: # The special code PYFLYBY_PATH=EMPTY means we intentionally want to # use an empty PYFLYBY_PATH (and don't fall back to the default path, # nor warn about an empty path). return () for p in pathnames: if re.match("/|[.]/|[.][.][.]/|~/", p): continue raise ValueError( "{env_var_name} components should start with / or ./ or ~/ or .../. " "Use {env_var_name}=./{p} instead of {env_var_name}={p} if you really " "want to use the current directory.".format( env_var_name=env_var_name, p=p)) pathnames = [os.path.expanduser(p) for p in pathnames] pathnames = _expand_tripledots(pathnames, target_dirname) pathnames = [Filename(fn) for fn in pathnames] pathnames = stable_unique(pathnames) pathnames = expand_py_files_from_args(pathnames) if not pathnames: logger.warning( "No import libraries found (%s=%r, default=%r)" % (env_var_name, os.environ.get(env_var_name), default_path)) return tuple(pathnames)
def test_stable_unique_1(): assert stable_unique([1, 4, 6, 4, 6, 5, 7]) == [1, 4, 6, 5, 7]