Import tools for python projects
Project description
shimport | |
Import utilities for python
|
Overview
Import utilities for python
Installation
See pypi for available releases.
pip install shimport
Usage
Simple lazy modules
>>> import shimport
>>> pathlib = shimport.lazy('pathlib')
>>> print(pathlib.Path('.').absolute)
<bound method Path.absolute of PosixPath('.')>
>>>
Filtering module contents
Filtering for Public Functions
Suppose you want to retrieve just the function-definitions from a module namespace.
Using shimport.wrapper
let's you slice and dice:
>>> import shimport
>>> wrapper = shimport.wrapper("os.path")
>>> wrapper = wrapper.prune(only_functions=True)
>>> print(wrapper.namespace.keys())
dict_keys(['abspath', 'basename', 'commonpath', 'commonprefix', 'dirname', 'exists', 'expanduser', 'expandvars', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', 'ismount', 'join', 'lexists', 'normcase', 'normpath', 'realpath', 'relpath', 'samefile', 'sameopenfile', 'samestat', 'split', 'splitdrive', 'splitext'])
>>>
Filtering using Chaining / Fluid Style
Some use-cases for shimport
involve scenarios that aren't great with declarative-style development.
So, there's good support for chaining (aka fluent) programming style as you can see below. (Note that indention here follows fluent-style that shed/black should support)
>>> import shimport
>>> (
... shimport
... .wrapper('os.path')
... .prune(only_data=True)
... .prune(val_predicate=lambda v: v=='/')
... .namespace.keys()
... )
dict_keys(['sep'])
>>>
>>> import typing, shimport
>>> (
... shimport
... .wrapper("os.path")
... .prune(
... exclude_private=True,
... filter_module_origin=True)
... )
<ModulesWrapper[os.path]>
>>>
Filtering for Classes
Grab only the classes from the given namespace:
>>> import shimport
>>> namespace=shimport.wrapper('pathlib').filter(only_classes=True)
>>> assert 'Path' in namespace
# Grab only subclasses of a given class from the given namespace
#>>> plugins = shimport.wrapper('my_app.plugins').prune(...)
#>>>
Filtering for Data
Grab only the classes from the given namespace:
>>> import shimport
>>> namespace = shimport.wrapper('os.path').filter(only_data=True)
>>> assert 'sep' in namespace
>>>
Automatically importing submodules
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
shimport-2024.2.10.0.12.tar.gz
(11.3 kB
view hashes)
Built Distribution
Close
Hashes for shimport-2024.2.10.0.12-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64c06746ccc73e98bdef4a9d1a686c37329e5d86adf8154e0abfd030a119d13e |
|
MD5 | 05e54c79d965116117a44cd626aaff5e |
|
BLAKE2b-256 | 180f92567875c2ec26605eb1a9d56e8ed3a087cdbf1452c990dd1a9061cdb873 |