python-argtools 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- LICENSE +21 -0
- argtools/__init__.py +276 -0
- argtools/py.typed +0 -0
- python_argtools-0.0.1.dist-info/LICENSE +21 -0
- python_argtools-0.0.1.dist-info/METADATA +42 -0
- python_argtools-0.0.1.dist-info/RECORD +7 -0
- python_argtools-0.0.1.dist-info/WHEEL +4 -0
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
argtools/__init__.py
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# coding: utf-8
|
|
3
|
+
|
|
4
|
+
"""This module provides several classes, which are used to collect
|
|
5
|
+
some arguments at one time and then use them repeatedly later.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
9
|
+
__version__ = (0, 0, 1)
|
|
10
|
+
__all__ = ["argcount", "Args", "UpdativeArgs"]
|
|
11
|
+
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from copy import copy
|
|
14
|
+
from inspect import getfullargspec
|
|
15
|
+
from typing import Any, Callable, Generic, ParamSpec, TypeVar
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
T = TypeVar("T")
|
|
19
|
+
P = ParamSpec("P")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def argcount(func: Callable) -> int:
|
|
23
|
+
try:
|
|
24
|
+
return func.__code__.co_argcount
|
|
25
|
+
except AttributeError:
|
|
26
|
+
return len(getfullargspec(func).args)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Args(Generic[T, P]):
|
|
30
|
+
"""Takes some positional arguments and keyword arguments,
|
|
31
|
+
and put them into an instance, which can be used repeatedly
|
|
32
|
+
every next time.
|
|
33
|
+
|
|
34
|
+
Fields::
|
|
35
|
+
self.pargs: the collected positional arguments
|
|
36
|
+
self.kargs: the collected keyword arguments
|
|
37
|
+
"""
|
|
38
|
+
__slots__ = ("pargs", "kargs")
|
|
39
|
+
|
|
40
|
+
def __init__(self, /, *pargs, **kargs):
|
|
41
|
+
self.pargs: P.args = pargs
|
|
42
|
+
self.kargs: P.kwargs = kargs
|
|
43
|
+
|
|
44
|
+
def __call__(self, /, func: Callable[..., T]) -> T:
|
|
45
|
+
"""Pass in the collected positional arguments and keyword
|
|
46
|
+
arguments when calling the callable `func`."""
|
|
47
|
+
return func(*self.pargs, **self.kargs)
|
|
48
|
+
|
|
49
|
+
def __copy__(self, /):
|
|
50
|
+
return type(self)(*self.pargs, **self.kargs)
|
|
51
|
+
|
|
52
|
+
def __eq__(self, other):
|
|
53
|
+
if isinstance(other, Args):
|
|
54
|
+
return self.pargs == other.pargs and self.kargs == other.kargs
|
|
55
|
+
return False
|
|
56
|
+
|
|
57
|
+
def __iter__(self, /):
|
|
58
|
+
return iter((self.pargs, self.kargs))
|
|
59
|
+
|
|
60
|
+
def __repr__(self):
|
|
61
|
+
return "%s(%s)" % (
|
|
62
|
+
type(self).__qualname__,
|
|
63
|
+
", ".join((
|
|
64
|
+
*map(repr, self.pargs),
|
|
65
|
+
*("%s=%r" % e for e in self.kargs.items()),
|
|
66
|
+
)),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
@classmethod
|
|
70
|
+
def call(cls, /, func: Callable[..., T], args: Any = ()) -> T:
|
|
71
|
+
"""Call the callable `func` and pass in the arguments `args`.
|
|
72
|
+
|
|
73
|
+
The actual behavior as below:
|
|
74
|
+
if isinstance(args, Args):
|
|
75
|
+
return args(func)
|
|
76
|
+
elif type(args) is tuple:
|
|
77
|
+
return func(*args)
|
|
78
|
+
elif type(args) is dict:
|
|
79
|
+
return func(**args)
|
|
80
|
+
return func(args)
|
|
81
|
+
"""
|
|
82
|
+
if isinstance(args, Args):
|
|
83
|
+
return args(func)
|
|
84
|
+
type_ = type(args)
|
|
85
|
+
if type_ is tuple:
|
|
86
|
+
return func(*args)
|
|
87
|
+
elif type_ is dict:
|
|
88
|
+
return func(**args)
|
|
89
|
+
return func(args)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class UpdativeArgs(Args):
|
|
93
|
+
"""Takes some positional arguments and keyword arguments,
|
|
94
|
+
and put them into an instance, which can be used repeatedly
|
|
95
|
+
every next time.
|
|
96
|
+
This derived class provides some methods to update the
|
|
97
|
+
collected arguments.
|
|
98
|
+
|
|
99
|
+
Fields::
|
|
100
|
+
self.pargs: the collected positional arguments
|
|
101
|
+
self.kargs: the collected keyword arguments
|
|
102
|
+
"""
|
|
103
|
+
__slots__ = ("pargs", "kargs")
|
|
104
|
+
|
|
105
|
+
def extend(self, /, *pargs, **kargs):
|
|
106
|
+
"""Extend the collected arguments.
|
|
107
|
+
|
|
108
|
+
Examples::
|
|
109
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
110
|
+
>>> args2 = args.extend(7, 8, x=9, r=0)
|
|
111
|
+
>>> args2
|
|
112
|
+
UpdativeArgs(1, 2, 3, 7, 8, x=4, y=5, z=6, r=0)
|
|
113
|
+
>>> args is args2
|
|
114
|
+
True
|
|
115
|
+
"""
|
|
116
|
+
if pargs:
|
|
117
|
+
self.pargs += pargs
|
|
118
|
+
if kargs:
|
|
119
|
+
kargs0 = self.kargs
|
|
120
|
+
kargs0.update(
|
|
121
|
+
(k, kargs[k])
|
|
122
|
+
for k in kargs.keys() - kargs0.keys()
|
|
123
|
+
)
|
|
124
|
+
return self
|
|
125
|
+
|
|
126
|
+
def copy_extend(self, /, *pargs, **kargs):
|
|
127
|
+
"""Extend the collected arguments in a copied instance.
|
|
128
|
+
|
|
129
|
+
Examples::
|
|
130
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
131
|
+
>>> args2 = args.copy_extend(7, 8, x=9, r=0)
|
|
132
|
+
>>> args2
|
|
133
|
+
UpdativeArgs(1, 2, 3, 7, 8, x=4, y=5, z=6, r=0)
|
|
134
|
+
>>> args is args2
|
|
135
|
+
False
|
|
136
|
+
"""
|
|
137
|
+
return copy(self).extend(*pargs, **kargs)
|
|
138
|
+
|
|
139
|
+
def prepend(self, /, *pargs, **kargs):
|
|
140
|
+
"""Prepend the collected arguments.
|
|
141
|
+
|
|
142
|
+
Examples::
|
|
143
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
144
|
+
>>> args2 = args.prepend(7, 8, x=9, r=0)
|
|
145
|
+
>>> args2
|
|
146
|
+
UpdativeArgs(7, 8, 1, 2, 3, x=9, y=5, z=6, r=0)
|
|
147
|
+
>>> args is args2
|
|
148
|
+
True
|
|
149
|
+
"""
|
|
150
|
+
if pargs:
|
|
151
|
+
self.pargs = pargs + self.pargs
|
|
152
|
+
if kargs:
|
|
153
|
+
self.kargs.update(kargs)
|
|
154
|
+
return self
|
|
155
|
+
|
|
156
|
+
def copy_prepend(self, /, *pargs, **kargs):
|
|
157
|
+
"""Prepend the collected arguments in a copied instance.
|
|
158
|
+
|
|
159
|
+
Examples::
|
|
160
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
161
|
+
>>> args2 = args.copy_prepend(7, 8, x=9, r=0)
|
|
162
|
+
>>> args2
|
|
163
|
+
UpdativeArgs(7, 8, 1, 2, 3, x=9, y=5, z=6, r=0)
|
|
164
|
+
>>> args is args2
|
|
165
|
+
False
|
|
166
|
+
"""
|
|
167
|
+
return copy(self).prepend(*pargs, **kargs)
|
|
168
|
+
|
|
169
|
+
def update(self, /, *pargs, **kargs):
|
|
170
|
+
"""Update the collected arguments.
|
|
171
|
+
|
|
172
|
+
Examples::
|
|
173
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
174
|
+
>>> args2 = args.update(7, 8, x=9, r=0)
|
|
175
|
+
>>> args2
|
|
176
|
+
UpdativeArgs(7, 8, 3, x=9, y=5, z=6, r=0)
|
|
177
|
+
>>> args is args2
|
|
178
|
+
True
|
|
179
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
180
|
+
>>> args.update(7, 8, 10, 11, x=9, r=0)
|
|
181
|
+
UpdativeArgs(7, 8, 10, 11, x=9, y=5, z=6, r=0)
|
|
182
|
+
"""
|
|
183
|
+
if pargs:
|
|
184
|
+
n = len(pargs) - len(self.pargs)
|
|
185
|
+
if n >= 0:
|
|
186
|
+
self.pargs = pargs
|
|
187
|
+
else:
|
|
188
|
+
self.pargs = pargs + self.pargs[n:]
|
|
189
|
+
if kargs:
|
|
190
|
+
self.kargs.update(kargs)
|
|
191
|
+
return self
|
|
192
|
+
|
|
193
|
+
def copy_update(self, /, *pargs, **kargs):
|
|
194
|
+
"""Update the collected arguments in a copied instance.
|
|
195
|
+
|
|
196
|
+
Examples::
|
|
197
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
198
|
+
>>> args2 = args.copy_update(7, 8, x=9, r=0)
|
|
199
|
+
>>> args2
|
|
200
|
+
UpdativeArgs(7, 8, 3, x=9, y=5, z=6, r=0)
|
|
201
|
+
>>> args is args2
|
|
202
|
+
False
|
|
203
|
+
|
|
204
|
+
Idempotence
|
|
205
|
+
>>> args3 = args2.copy_update(7, 8, x=9, r=0)
|
|
206
|
+
>>> args2 == args3
|
|
207
|
+
True
|
|
208
|
+
|
|
209
|
+
Idempotence
|
|
210
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
211
|
+
>>> args2 = args.copy_update(7, 8, 10, 11, x=9, r=0)
|
|
212
|
+
>>> args3 = args2.copy_update(7, 8, 10, 11, x=9, r=0)
|
|
213
|
+
>>> args2 == args3
|
|
214
|
+
True
|
|
215
|
+
"""
|
|
216
|
+
return copy(self).update(*pargs, **kargs)
|
|
217
|
+
|
|
218
|
+
def update_extend(self, /, *pargs, **kargs):
|
|
219
|
+
"""Update and entend the collected arguments.
|
|
220
|
+
|
|
221
|
+
Examples::
|
|
222
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
223
|
+
>>> args2 = args.update_extend(7, 8, 10, 11, x=9, r=0)
|
|
224
|
+
>>> args2
|
|
225
|
+
UpdativeArgs(1, 2, 3, 11, x=4, y=5, z=6, r=0)
|
|
226
|
+
>>> args is args2
|
|
227
|
+
True
|
|
228
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
229
|
+
>>> args.update_extend(7, 8, x=9, r=0)
|
|
230
|
+
UpdativeArgs(1, 2, 3, x=4, y=5, z=6, r=0)
|
|
231
|
+
"""
|
|
232
|
+
if pargs:
|
|
233
|
+
n = len(self.pargs) - len(pargs)
|
|
234
|
+
if n < 0:
|
|
235
|
+
self.pargs += pargs[n:]
|
|
236
|
+
if kargs:
|
|
237
|
+
kargs0 = self.kargs
|
|
238
|
+
kargs0.update(
|
|
239
|
+
(k, kargs[k])
|
|
240
|
+
for k in kargs.keys() - kargs0.keys()
|
|
241
|
+
)
|
|
242
|
+
return self
|
|
243
|
+
|
|
244
|
+
def copy_update_extend(self, /, *pargs, **kargs):
|
|
245
|
+
"""Update and extend the collected arguments in
|
|
246
|
+
a copied instance.
|
|
247
|
+
|
|
248
|
+
Examples::
|
|
249
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
250
|
+
>>> args2 = args.copy_update_extend(7, 8, 10, 11, x=9, r=0)
|
|
251
|
+
>>> args2
|
|
252
|
+
UpdativeArgs(1, 2, 3, 11, x=4, y=5, z=6, r=0)
|
|
253
|
+
>>> args is args2
|
|
254
|
+
False
|
|
255
|
+
|
|
256
|
+
Idempotence
|
|
257
|
+
>>> args3 = args2.copy_update_extend(7, 8, 10, 11, x=9, r=0)
|
|
258
|
+
>>> args2 == args3
|
|
259
|
+
True
|
|
260
|
+
|
|
261
|
+
Idempotence
|
|
262
|
+
>>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6)
|
|
263
|
+
>>> args2 = args.copy_update_extend(7, 8, x=9, r=0)
|
|
264
|
+
>>> args2
|
|
265
|
+
UpdativeArgs(1, 2, 3, x=4, y=5, z=6, r=0)
|
|
266
|
+
>>> args3 = args2.copy_update_extend(7, 8, x=9, r=0)
|
|
267
|
+
>>> args2 == args3
|
|
268
|
+
True
|
|
269
|
+
"""
|
|
270
|
+
return copy(self).update_extend(*pargs, **kargs)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
if __name__ == "__main__":
|
|
274
|
+
import doctest
|
|
275
|
+
doctest.testmod()
|
|
276
|
+
|
argtools/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ChenyangGao <https://github.com/ChenyangGao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: python-argtools
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python argument tools.
|
|
5
|
+
Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-argtools
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: argument,tools
|
|
8
|
+
Author: ChenyangGao
|
|
9
|
+
Author-email: wosiwujm@gmail.com
|
|
10
|
+
Requires-Python: >=3.10,<4.0
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
|
+
Classifier: Topic :: Software Development
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-argtools
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# Python argument tools.
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
You can install from [pypi](https://pypi.org/project/python-argtools/)
|
|
32
|
+
|
|
33
|
+
```console
|
|
34
|
+
pip install -U argtools
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import argtools
|
|
41
|
+
```
|
|
42
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
2
|
+
argtools/__init__.py,sha256=t6sGhWh2ph0jYQausPJr-j8ALV59ketr2qOA2Gj0w8k,8519
|
|
3
|
+
argtools/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
python_argtools-0.0.1.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
5
|
+
python_argtools-0.0.1.dist-info/METADATA,sha256=_VNtcqlH_kfeQ-0VipJlg2xPsVbUZOdk4z0lc8UssmQ,1326
|
|
6
|
+
python_argtools-0.0.1.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
|
7
|
+
python_argtools-0.0.1.dist-info/RECORD,,
|