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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
"""This module provides several classes, which are used to collect some arguments at one time and then use them repeatedly later."""
__author__ = "ChenyangGao <https://chenyanggao.github.io/>" __version__ = (0, 1) __all__ = ["Args", "UpdativeArgs"]
from copy import copy from typing import Any, Callable, Generic, TypeVar
T = TypeVar("T") P = TypeVar("P", bound=tuple) K = TypeVar("K", bound=dict)
class Args(Generic[T, P]): """Takes some positional arguments and keyword arguments, and put them into an instance, which can be used repeatedly every next time.
Fields:: self.pargs: the collected positional arguments self.kargs: the collected keyword arguments """ __slots__ = ("pargs", "kargs")
def __init__(self, /, *pargs, **kargs): self.pargs: P = pargs self.kargs: K = kargs
def __call__(self, /, func: Callable[..., T]) -> T: """Pass in the collected positional arguments and keyword arguments when calling the callable `func`.""" return func(*self.pargs, **self.kargs)
def __copy__(self, /): return type(self)(*self.pargs, **self.kargs)
def __eq__(self, other): if isinstance(other, Args): return self.pargs == other.pargs and self.kargs == other.kargs return False
def __iter__(self, /): return iter((self.pargs, self.kargs))
def __repr__(self): return "%s(%s)" % ( type(self).__qualname__, ", ".join(( *map(repr, self.pargs), *("%s=%r" % e for e in self.kargs.items()), )), )
@classmethod def call(cls, /, func: Callable[..., T], args: Any = ()) -> T: """Call the callable `func` and pass in the arguments `args`.
The actual behavior as below: if isinstance(args, Args): return args(func) elif type(args) is tuple: return func(*args) elif type(args) is dict: return func(**args) return func(args) """ if isinstance(args, Args): return args(func) type_ = type(args) if type_ is tuple: return func(*args) elif type_ is dict: return func(**args) return func(args)
class UpdativeArgs(Args): """Takes some positional arguments and keyword arguments, and put them into an instance, which can be used repeatedly every next time. This derived class provides some methods to update the collected arguments.
Fields:: self.pargs: the collected positional arguments self.kargs: the collected keyword arguments """ __slots__ = ("pargs", "kargs")
def extend(self, /, *pargs, **kargs): """Extend the collected arguments.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.extend(7, 8, x=9, r=0) >>> args2 UpdativeArgs(1, 2, 3, 7, 8, x=4, y=5, z=6, r=0) >>> args is args2 True """ if pargs: self.pargs += pargs if kargs: kargs0 = self.kargs kargs0.update( (k, kargs[k]) for k in kargs.keys() - kargs0.keys() ) return self
def copy_extend(self, /, *pargs, **kargs): """Extend the collected arguments in a copied instance.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.copy_extend(7, 8, x=9, r=0) >>> args2 UpdativeArgs(1, 2, 3, 7, 8, x=4, y=5, z=6, r=0) >>> args is args2 False """ return copy(self).extend(*pargs, **kargs)
def prepend(self, /, *pargs, **kargs): """Prepend the collected arguments.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.prepend(7, 8, x=9, r=0) >>> args2 UpdativeArgs(7, 8, 1, 2, 3, x=9, y=5, z=6, r=0) >>> args is args2 True """ if pargs: self.pargs = pargs + self.pargs if kargs: self.kargs.update(kargs) return self
def copy_prepend(self, /, *pargs, **kargs): """Prepend the collected arguments in a copied instance.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.copy_prepend(7, 8, x=9, r=0) >>> args2 UpdativeArgs(7, 8, 1, 2, 3, x=9, y=5, z=6, r=0) >>> args is args2 False """ return copy(self).prepend(*pargs, **kargs)
def update(self, /, *pargs, **kargs): """Update the collected arguments.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.update(7, 8, x=9, r=0) >>> args2 UpdativeArgs(7, 8, 3, x=9, y=5, z=6, r=0) >>> args is args2 True >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args.update(7, 8, 10, 11, x=9, r=0) UpdativeArgs(7, 8, 10, 11, x=9, y=5, z=6, r=0) """ if pargs: n = len(pargs) - len(self.pargs) if n >= 0: self.pargs = pargs else: self.pargs = pargs + self.pargs[n:] if kargs: self.kargs.update(kargs) return self
def copy_update(self, /, *pargs, **kargs): """Update the collected arguments in a copied instance.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.copy_update(7, 8, x=9, r=0) >>> args2 UpdativeArgs(7, 8, 3, x=9, y=5, z=6, r=0) >>> args is args2 False
Idempotence >>> args3 = args2.copy_update(7, 8, x=9, r=0) >>> args2 == args3 True
Idempotence >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.copy_update(7, 8, 10, 11, x=9, r=0) >>> args3 = args2.copy_update(7, 8, 10, 11, x=9, r=0) >>> args2 == args3 True """ return copy(self).update(*pargs, **kargs)
def update_extend(self, /, *pargs, **kargs): """Update and entend the collected arguments.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.update_extend(7, 8, 10, 11, x=9, r=0) >>> args2 UpdativeArgs(1, 2, 3, 11, x=4, y=5, z=6, r=0) >>> args is args2 True >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args.update_extend(7, 8, x=9, r=0) UpdativeArgs(1, 2, 3, x=4, y=5, z=6, r=0) """ if pargs: n = len(self.pargs) - len(pargs) if n < 0: self.pargs += pargs[n:] if kargs: kargs0 = self.kargs kargs0.update( (k, kargs[k]) for k in kargs.keys() - kargs0.keys() ) return self
def copy_update_extend(self, /, *pargs, **kargs): """Update and extend the collected arguments in a copied instance.
Examples:: >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.copy_update_extend(7, 8, 10, 11, x=9, r=0) >>> args2 UpdativeArgs(1, 2, 3, 11, x=4, y=5, z=6, r=0) >>> args is args2 False
Idempotence >>> args3 = args2.copy_update_extend(7, 8, 10, 11, x=9, r=0) >>> args2 == args3 True
Idempotence >>> args = UpdativeArgs(1, 2, 3, x=4, y=5, z=6) >>> args2 = args.copy_update_extend(7, 8, x=9, r=0) >>> args2 UpdativeArgs(1, 2, 3, x=4, y=5, z=6, r=0) >>> args3 = args2.copy_update_extend(7, 8, x=9, r=0) >>> args2 == args3 True """ return copy(self).update_extend(*pargs, **kargs)
if __name__ == "__main__": import doctest doctest.testmod()
|