python-argtools 0.0.2__tar.gz → 0.0.4__tar.gz

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.
@@ -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
@@ -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,12 +1,12 @@
1
1
  [tool.poetry]
2
2
  name = "python-argtools"
3
- version = "0.0.2"
3
+ version = "0.0.4"
4
4
  description = "Python argument tools."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
7
7
  readme = "readme.md"
8
- homepage = "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-argtools"
9
- repository = "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-argtools"
8
+ homepage = "https://github.com/ChenyangGao/python-modules/tree/main/python-argtools"
9
+ repository = "https://github.com/ChenyangGao/python-modules/tree/main/python-argtools"
10
10
  keywords = ["argument", "tools"]
11
11
  classifiers = [
12
12
  "License :: OSI Approved :: MIT License",
@@ -5,7 +5,7 @@
5
5
  You can install from [pypi](https://pypi.org/project/python-argtools/)
6
6
 
7
7
  ```console
8
- pip install -U argtools
8
+ pip install -U python-argtools
9
9
  ```
10
10
 
11
11
  ## Usage
File without changes