Python模块-打包多个参数于一体

Python模块-打包多个参数于一体

背景

在调用 Python 函数 (See also realpython - Defining Your Own Python Function) 的时候,往往需要传入一些参数。函数所需的参数被称为形式参数 parameter,传入的参数被称为实际参数 argument (See also 5 Types of Arguments in Python Function Definitions)。

See also the FAQ question on the difference between arguments and parameters, the inspect.Parameter class, the Function definitions section, and PEP 362.

我常常需要把一些实际参数收集起来,可能后续还要进行一些更新,并在需要的时候反复使用。有一种办法是使用偏函数 functools.partial,但这需要绑定具体的函数。

1
2
3
4
5
6
7
8
9
>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
18
>>> basethree = partial(basetwo, base=3)
>>> basethree.__doc__ = 'Convert base 3 string to an int.'
>>> basethree('10010')
84

于是我实现了一个类 Args,它可以一次性收集一些位置参数(positional argument,See also stackoverflow - Understanding positional arguments in Python)和关键字参数(keyword argument,See also realpython - Python args and kwargs: Demystified),并在以后需要时,反复直接使用。

1
2
3
4
5
6
7
>>> from args import UpdativeArgs
>>> args = UpdativeArgs('10010', base=2)
>>> args(int)
18
>>> args.update(base=3)
>>> args(int)
84

Args 类更可以简化其它函数的定义,例如下面的创建数据库游标 (See PEP 249 – Python Database API Specification v2.0)的代码片段

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
from args import Args

from contextlib import closing, contextmanager

@contextmanager
def ctx_cursor(
connect,
con_args,
cur_args = (),
do_tsac: bool = False,
):
"""创建一个上下文管理器,返回数据库的游标对象
:param connect: 创建连接的函数,返回一个数据库连接对象
:param con_args: 创建连接对象时的参数
:param cur_args: 创建游标对象时的参数
:param do_tsac: 是否提交事务

:return: 上下文管理器,返回游标对象
"""
with closing(Args.call(connect, con_args)) as con:
with closing(Args.call(con.cursor, cur_args)) as cur:
if do_tsac:
try:
yield cur
con.commit()
except BaseException:
con.rollback()
raise
else:
yield cur

示例:创建和使用一个 sqlite 游标

1
2
3
4
5
>>> import sqlite3
>>> with ctx_cursor(
... sqlite3.connect, ":memory:"
... ) as cur:
... ...

示例:创建和使用一个 mysql 游标

1
2
3
4
5
6
7
8
9
10
11
12
>>> import pymysql
>>> from args import Args
>>> with ctx_cursor(
... pymysql.connect,
... Args(
... user="root",
... password="12345",
... database="test",
... ),
... Args(pymysql.cursors.DictCursor),
... ) as cur:
... ...

代码实现

TIPS 代码的最新版本在 GitHub Gist 中维护
https://gist.github.com/ChenyangGao/6cd16d7aa29adfc852c3f3d9a39c0b81

文件名称是 args.pyPython 实现代码如下:

args.py
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
#!/usr/bin/env python3
# coding: utf-8

"""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()

评论

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×