-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.md.j2
128 lines (96 loc) · 3.48 KB
/
README.md.j2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<table>
<tr>
<td colspan=2>
<strong>{{project.name}}</strong>
<a href={{pypi.project_url}}><img src="https://img.shields.io/pypi/l/{{project.name}}.svg"></a>
<a href={{pypi.project_url}}><img src="https://badge.fury.io/py/{{project.name}}.svg"></a>
{%for action in github.actions%}<a href="{{action.url}}"><img src="{{action.url}}/badge.svg"></a>{%endfor%}
</td>
</tr>
<tr>
<td width=15%><img src={{github.raw_url}}/master/img/icon.png style="width:150px"></td>
<td>
Import utilities for python
</td>
</tr>
</table>
---------------------------------------------------------------------------------
{{markdown_toc(__template__, level=2)}}
---------------------------------------------------------------------------------
## Overview
Import utilities for python
---------------------------------------------------------------------------------
## Installation
See [pypi](https://pypi.org/project/shimport/) for available releases.
```bash
pip install shimport
```
---------------------------------------------------------------------------------
## Usage
### Simple lazy modules
```pycon
>>> 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:
```pycon
>>> 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)](https://en.wikipedia.org/wiki/Fluent_interface) programming style as you can see below. (Note that indention here follows fluent-style that shed/black should support)
```pycon
>>> import shimport
>>> (
... shimport
... .wrapper('os.path')
... .prune(only_data=True)
... .prune(val_predicate=lambda v: v=='/')
... .namespace.keys()
... )
dict_keys(['sep'])
>>>
```
```pycon
>>> 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:
```pycon
>>> 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:
```pycon
>>> import shimport
>>> namespace = shimport.wrapper('os.path').filter(only_data=True)
>>> assert 'sep' in namespace
>>>
```
---------------------------------------------------------------------------------