unbox.base¶
Base functionality of unbox
Module Attributes
an fnmatch pattern for a dotted-name segment, or a predicate on the full dotted module name. |
Functions
Will yield from a prepopulated list of builtin module (and submodule) names for the environment's python version. |
|
|
Get imported strings. |
|
|
|
|
|
|
|
Resolve |
Generates names of standard libs from python environment it was called from. |
Classes
|
Store of module names imported by some module. |
|
|
|
|
|
- unbox.base.ModuleImports¶
alias of
ModuleNamesImportedByModule
- unbox.base.ModuleNameFilter¶
an fnmatch pattern for a dotted-name segment, or a predicate on the full dotted module name.
- Type:
An
excludeelement
- class unbox.base.ModuleNamesImportedByModule(root, trackUnusedNames=False, all_unused=False, external_dependencies=True, warn_about_duplicates=False, verbose=False, ignore_parse_path_warnings=False)[source]¶
Bases:
StoreStore of module names imported by some module.
- class unbox.base.ModulesColl(root, trackUnusedNames=False, all_unused=False, external_dependencies=True, warn_about_duplicates=False, verbose=False, ignore_parse_path_warnings=False)[source]¶
Bases:
Collection
- class unbox.base.ModulesImportedByModule(root, trackUnusedNames=False, all_unused=False, external_dependencies=True, warn_about_duplicates=False, verbose=False, ignore_parse_path_warnings=False)[source]¶
Bases:
KvReader,ModulesColl
- class unbox.base.MyModuleGraph(root, trackUnusedNames=False, all_unused=False, external_dependencies=True, warn_about_duplicates=False, verbose=False, ignore_parse_path_warnings=False)[source]¶
Bases:
ModuleGraph
- unbox.base.documented_builtin_module_names()[source]¶
Will yield from a prepopulated list of builtin module (and submodule) names for the environment’s python version.
These were parsed from
https://docs.python.org/{version}/library/and saved in package’s data (this data, for all supported versions, can be found here too: https://github.com/i2mint/unbox/tree/master/unbox/data/standard_lib_names)Supported python versions: ‘2.7’, ‘3.5’, ‘3.6’, ‘3.7’, ‘3.8’, and ‘3.9’
I python version not supported, will return an empty set (with a warning).
See also
scan_locally_for_standard_lib_names
- unbox.base.imports_for(root, post=<class 'set'>, *, exclude=())[source]¶
Get imported strings.
- Parameters:
root – Module, package, or filepath/folderpath thereof.
post – Postprocess iterable. For example:
set, when order and repetition doesn’t mattercollections.Counter, to count number of modules where the module is imported,lambda module: set(x.split('.')[0] for x in module)if you only care about the top level packageexclude (
Iterable[Union[str,Callable[[str],bool]]]) – Modules to leave out of the scan, as fnmatch patterns matched against each segment of the dotted module name (e.g.'tests','test_*') and/or predicates on the full dotted name. Empty (the default) means “scan everything”, which is whatunboxhas always done. SeeDFLT_TEST_MODULE_PATTERNSand theimports_for.runtimepreset.
- Returns:
>>> import wave >>> imports = imports_for(wave) >>> sorted({'collections', 'struct', 'sys'} & imports) ['collections', 'struct', 'sys']Note
only a version-stable subset of
wave’s imports is asserted here – py3.10’swaveimportsaudioopandchunk(both removed in 3.13), while py3.12’s importsuuidinstead.An in-package
tests/imports things (pytest,hypothesis, …) that the package doesn’t need at runtime;imports_for.runtimeis the preset that scopes them out (seeDFLT_TEST_MODULE_PATTERNS).
- unbox.base.resolve_rootpath(obj)[source]¶
Resolve
objto an existing filesystem path.objcan be a module object, a module name, or a path (in which case it is returned as-is). A package resolves to its directory, while a plain (non-package) module resolves to its.pyfile.>>> import wave # a plain module: has __file__ but no __path__ >>> resolve_rootpath(wave).endswith('wave.py') True >>> import unbox # a package: has a __path__ >>> resolve_rootpath(unbox).endswith('unbox') True- Return type:
- unbox.base.scan_locally_for_standard_lib_names(include_underscored=True)[source]¶
Generates names of standard libs from python environment it was called from.
- Parameters:
include_underscored – Whether to include names that start with underscore or not. :keyword standard lib, builtins
See also
documented_builtin_module_names
>>> standard_lib_names = set(scan_locally_for_standard_lib_names(include_underscored=True)) >>> # verify that a few known libs are there (including three folders and three py files) >>> assert {'collections', 'asyncio', 'os', 'dis', '__future__'}.issubset(standard_lib_names) >>> # verify that other unwanted "decoys" are NOT in there >>> assert {'__pycache__', 'LICENSE.txt', 'config-3.8-darwin', '.DS_Store'}.isdisjoint(standard_lib_names)