python-argtools 0.0.2__py3-none-any.whl → 0.0.4__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.
argtools/__init__.py CHANGED
@@ -6,23 +6,45 @@ some arguments at one time and then use them repeatedly later.
6
6
  """
7
7
 
8
8
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
9
- __version__ = (0, 0, 2)
10
- __all__ = ["argcount", "Args", "UpdativeArgs", "Call"]
9
+ __version__ = (0, 0, 4)
10
+ __all__ = ["argcount", "has_keyword_arg", "Args", "UpdativeArgs", "Call"]
11
11
 
12
12
  from collections.abc import Callable
13
13
  from copy import copy
14
14
  from functools import partial, update_wrapper
15
- from inspect import getfullargspec
16
- from typing import cast, Any
15
+ from inspect import getfullargspec, signature, Parameter
16
+ from types import MethodType, MethodWrapperType
17
+ from typing import Any
17
18
 
18
19
 
19
20
  def argcount(func: Callable, /) -> int:
20
21
  if isinstance(func, partial):
21
22
  return max(0, argcount(func.func) - len(func.args))
23
+ is_method = isinstance(func, (MethodType, MethodWrapperType))
22
24
  try:
23
- return func.__code__.co_argcount
25
+ return func.__code__.co_argcount - is_method
24
26
  except AttributeError:
25
- return len(getfullargspec(func).args)
27
+ return len(getfullargspec(func).args) - is_method
28
+
29
+
30
+ def has_keyword_arg(func: Callable, arg_name: str, /) -> bool:
31
+ try:
32
+ sig = signature(func)
33
+ except ValueError:
34
+ try:
35
+ arg_spec = getfullargspec(func)
36
+ return bool(arg_spec.varkw or arg_name in arg_spec.kwonlyargs)
37
+ except TypeError:
38
+ return False
39
+ else:
40
+ for name, param in sig.parameters.items():
41
+ match param.kind:
42
+ case Parameter.POSITIONAL_OR_KEYWORD | Parameter.KEYWORD_ONLY:
43
+ if name == arg_name:
44
+ return True
45
+ case Parameter.VAR_KEYWORD:
46
+ return True
47
+ return False
26
48
 
27
49
 
28
50
  class Args:
@@ -53,6 +75,25 @@ class Args:
53
75
  return self.args == other.args and self.kwargs == other.kwargs
54
76
  return False
55
77
 
78
+ def __getitem__(self, idx: int | slice | str | tuple[int | slice | str, ...], /):
79
+ cls = type(self)
80
+ pget = self.args.__getitem__
81
+ if isinstance(idx, (int, slice)):
82
+ return cls(pget(idx))
83
+ kget = self.kwargs.__getitem__
84
+ if isinstance(idx, str):
85
+ return cls(idx=kget(idx))
86
+ else:
87
+ pargs: list = []
88
+ kargs: dict = {}
89
+ add_parg = pargs.append
90
+ for idx_ in idx:
91
+ if isinstance(idx_, (int, slice)):
92
+ add_parg(pget(idx_))
93
+ else:
94
+ kargs[idx_] = kget(idx_)
95
+ return cls(*pargs, **kargs)
96
+
56
97
  def __iter__(self, /):
57
98
  return iter((self.args, self.kwargs))
58
99
 
@@ -281,19 +322,19 @@ class Call[**Params, R](partial):
281
322
  **kwds: Params.kwargs,
282
323
  ):
283
324
  if hasattr(func, "func"):
284
- args = cast(Params.args, getattr(func, "args", ()) + args)
325
+ args = getattr(func, "args", ()) + args # type: ignore
285
326
  kwargs: None | dict = None
286
327
  try:
287
328
  kwargs = getattr(func, "kwargs")
288
329
  except AttributeError:
289
330
  kwargs = getattr(func, "keywords", None)
290
331
  if kwargs:
291
- kwds = cast(Params.kwargs, {**kwargs, **kwds})
332
+ kwds = {**kwargs, **kwds} # type: ignore
292
333
  func = func.func
293
334
  return update_wrapper(super().__new__(cls, func, *args, **kwds), func)
294
335
 
295
336
  @property
296
- def kwargs(self, /) -> Params.kwargs:
337
+ def kwargs(self, /) -> dict:
297
338
  return self.keywords
298
339
 
299
340
  def __call__(self, /) -> R:
@@ -1,9 +1,9 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: python-argtools
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: Python argument tools.
5
- Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-argtools
6
5
  License: MIT
6
+ License-File: LICENSE
7
7
  Keywords: argument,tools
8
8
  Author: ChenyangGao
9
9
  Author-email: wosiwujm@gmail.com
@@ -16,11 +16,13 @@ Classifier: Programming Language :: Python
16
16
  Classifier: Programming Language :: Python :: 3
17
17
  Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
19
20
  Classifier: Programming Language :: Python :: 3 :: Only
20
21
  Classifier: Topic :: Software Development
21
22
  Classifier: Topic :: Software Development :: Libraries
22
23
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
- Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-argtools
24
+ Project-URL: Homepage, https://github.com/ChenyangGao/python-modules/tree/main/python-argtools
25
+ Project-URL: Repository, https://github.com/ChenyangGao/python-modules/tree/main/python-argtools
24
26
  Description-Content-Type: text/markdown
25
27
 
26
28
  # Python argument tools.
@@ -30,7 +32,7 @@ Description-Content-Type: text/markdown
30
32
  You can install from [pypi](https://pypi.org/project/python-argtools/)
31
33
 
32
34
  ```console
33
- pip install -U argtools
35
+ pip install -U python-argtools
34
36
  ```
35
37
 
36
38
  ## Usage
@@ -0,0 +1,6 @@
1
+ argtools/__init__.py,sha256=r9bht478QNx7gxclRvgikMSepq6qcJrGlCEN_sjVJ8I,11018
2
+ argtools/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ python_argtools-0.0.4.dist-info/METADATA,sha256=ZIW1ob5cWn_WeQ9zd2X4jcbjjGXv9HLvfemo2p00kjU,1337
4
+ python_argtools-0.0.4.dist-info/WHEEL,sha256=EGEvSphFYqXKs23-kQBeyNoJP1nrT8ZJKQoi5p5DYL8,88
5
+ python_argtools-0.0.4.dist-info/licenses/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
6
+ python_argtools-0.0.4.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 1.9.1
2
+ Generator: poetry-core 2.4.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,21 +0,0 @@
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.
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- argtools/__init__.py,sha256=VnzRDtsySC_7s0mh1jypHXclNG-bh_42xdICRmnDLaw,9498
3
- argtools/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_argtools-0.0.2.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_argtools-0.0.2.dist-info/METADATA,sha256=m36i3M9ThbvirkRV19D2rDu45N4Te9EmrXVPfbYRTQI,1275
6
- python_argtools-0.0.2.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
7
- python_argtools-0.0.2.dist-info/RECORD,,