Python Toolkit to imports introspection
Project description
Parse imports from .py file in a flexible way
Source Code: https://github.com/andresbena19/py-imports
Requirements
Python 3.7+
py-imports stands on the shoulders of giants:
- ast — Abstract Syntax Trees to traverse python code.
Installation
$ pip install py-imports
---> 100%
All it's ready to begin
Example
Introspect it
- Create a file
main.py
with:
from py_imports.manager import PyImports
myself = "main.py"
# Let's introspect myself
with PyImports() as manager:
manager.get_imports(myself)
imports = manager.imports_resume()
# Now you have access to the imports used in each file
print(imports)
{
'main.py': <py_imports.base.models.ImportsCollectionFile object at 0x10b889220>
}
# Get details about the absolute, relative and standard imports in the file
collector_object = imports.get(myself)
absolute_imports = collector_object.absolute_imports
relative_imports = collector_object.relative_imports
imports = collector_object.imports
# It's obvious that in this file there are just one absolute import
# --- from py_imports.manager import PyImports ---
# If we introspect the object, we will get the next
first_import = absolute_imports[0]
first_import.childs -> ['PyImports']
first_import.parent -> 'py_imports.manager'
first_import.statement -> 'from py_imports.manager import PyImports'
first_import.level -> 0
first_import.line -> 1
# Now you know more about you...
Notes
This library does not execute any part of the python target code, this just make a static analysis over the code to describe the meta information about the imports in the file.
License
This project is licensed under the terms of the MIT license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py_imports-1.1.0.tar.gz
(7.3 kB
view details)
Built Distribution
File details
Details for the file py_imports-1.1.0.tar.gz
.
File metadata
- Download URL: py_imports-1.1.0.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.0 Linux/5.11.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5202f1c335d7df253ba3eb34ebbace2ee8af6f53ac194efec54aad06a4aa7198 |
|
MD5 | 578ddbbf0f8a2aa51dcc9c5fe87e10de |
|
BLAKE2b-256 | c6abad4914fc42aed8200293200b2f42c3a8cfaf004034ad12eab556e7732d99 |
File details
Details for the file py_imports-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: py_imports-1.1.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.10.0 Linux/5.11.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f38b04c367f2348da2366159af050a64a5516139c51fb433d1b1e18e1a7388c2 |
|
MD5 | 024a221a59da48cef5f764b17359acc9 |
|
BLAKE2b-256 | c4869c433b1a7016e0dbf22985a738a71956231832380066385bdb7ffd34ba09 |