agglovar.dev.imports

General development utilities.

Classes

GlobalImport

Mantains a stack of import frames.

Functions

alias_to_obj_dict(→ dict[str, Any])

Find objects for relative imports.

find_imports(→ set[str])

Locate objects defined in a module.

get_defined_names(mod)

Get names defined in a module.

get_module_definitions(→ Generator[str, None, None])

Get names defined in a module object ignoring imports.

librl(→ None)

Reload libraries.

Module Contents

class agglovar.dev.imports.GlobalImport

Mantains a stack of import frames.

__bool__() bool

Determine if the stack is non-empty.

__len__() int

Get the number of stack items.

clear() None

Clear all scopes.

pop() bool

Pop the last scope from the stack and restore the global scope to the state before it was applied.

Returns:

True if a scope was popped, False if the global import stack is empty.

push(package_path: str, import_statements: str | None = None, overwrite: bool = True, mod_imports: bool = True, mod_private: bool = True, mod_dunder: bool = False) None

Push a new scope to the global import stack and update the global scope.

Parameters:
  • package_path – Name of the module where this import statement is found (e.g. ‘pav3.lgsv.interval’ if the import statements are in ‘pav3/lgsv/interval.py’).

  • import_statements – A string of newline-separated import statements.

  • overwrite – Whether to overwrite existing global variables.

  • mod_imports – Whether to import objects from modules.

  • mod_private – Whether to import private objects from modules.

  • mod_dunder – Whether to import dunder objects from modules.

refresh() None

Undo and re-do all scopes.

Run when objects might have changed.

caller_globals
import_stack = []
agglovar.dev.imports.alias_to_obj_dict(package_path: str, import_statements: str | None = None, mod_imports: bool = True, mod_private: bool = True, mod_dunder: bool = False) dict[str, Any]

Find objects for relative imports.

Parameters:
  • package_path – Name of the module where this import statement is found (i.e. ‘pav3.lgsv.interval’ if the).

  • import_statements – A string of newline-separated import statements.

  • mod_imports – Whether to import objects from modules.

  • mod_private – Whether to import private objects from modules.

  • mod_dunder – Whether to import dunder objects from modules.

Returns:

A set of new global names that were imported.

agglovar.dev.imports.find_imports(module_name: str, public_only: bool = True, all_only: bool = False) set[str]

Locate objects defined in a module.

Parameters:
  • module_name – Name of the module to inspect.

  • public_only – Whether to include only public objects (i.e. not starting with ‘_’).

  • all_only – Whether to include only objects defined in ‘__all__’. Overrides public_only if __all__ has values.

Returns:

Set of object names defined in the module.

agglovar.dev.imports.get_defined_names(mod: types.ModuleType)

Get names defined in a module.

Parameters:

mod – Module to inspect.

Yields:

Tuples of (name, type, level) where “type” is “func”, “class”, “def”, or “import”. “level” is defined for imports (None for others) and is the level of the import with 0 being the top-level and 1 or more being a relative import.

agglovar.dev.imports.get_module_definitions(module: types.ModuleType, include_dunder: bool = False) Generator[str, None, None]

Get names defined in a module object ignoring imports.

Parameters:
  • module – Module object to inspect.

  • include_dunder – Whether to include dunder objects (starting with ‘__’).

agglovar.dev.imports.librl(packages: collections.abc.Iterable[str] | str = 'agglovar') None

Reload libraries.

Reloads all modules in the given packages and their dependencies. Attempts to resolve import order by inspecting members of each package.

Parameters:

packages – Names of packages to reload. Must be top-level package names (name before the first dot).