查看原文
其他

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

carry麦 码农真经 2023-12-25

背景

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

See also the FAQ question on the difference between arguments and parameters[6], the inspect.Parameter[7] class, the Function definitions[8] section, and PEP 362[9].

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

>>> 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[11],See also stackoverflow - Understanding positional arguments in Python[12])和关键字参数(keyword argument[13] ,See also realpython - Python args and kwargs: Demystified[14])),并在以后需要时,反复直接使用。

>>> 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[15])的代码片段

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[16] 游标

>>> import sqlite3
>>> with ctx_cursor(
...     sqlite3.connect, ":memory:"
... as cur: 
...     ...

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

>>> 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[18] 中维护 https://gist.github.com/ChenyangGao/6cd16d7aa29adfc852c3f3d9a39c0b81

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

#!/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()

往期推荐

欢迎关注我的公众号“码中人”,原创技术文章第一时间推送。

引用链接

[1] Python: https://www.python.org
[2] realpython - Defining Your Own Python Function: https://realpython.com/defining-your-own-python-function/
[3] parameter: https://docs.python.org/3/glossary.html#term-parameter
[4] argument: https://docs.python.org/3/glossary.html#term-argument
[5] 5 Types of Arguments in Python Function Definitions: https://levelup.gitconnected.com/5-types-of-arguments-in-python-function-definition-e0e2a2cafd29
[6] the difference between arguments and parameters: https://docs.python.org/3/faq/programming.html#faq-argument-vs-parameter
[7] inspect.Parameter: https://docs.python.org/3/library/inspect.html#inspect.Parameter
[8] Function definitions: https://docs.python.org/3/reference/compound_stmts.html#function
[9] PEP 362: https://www.python.org/dev/peps/pep-0362
[10] functools.partial: https://docs.python.org/3/library/functools.html#functools.partial
[11] positional argument: https://www.codingem.com/what-is-a-positional-argument-in-python/#:~:text=Arguments%20in%20Python-,A%20positional%20argument%20in%20Python%20is%20an%20argument,matters%20in%20a%20function%20call.&text=Let's%20define%20a%20function%20that,my%20name%20is%20%7Bname%7D.
[12] stackoverflow - Understanding positional arguments in Python: https://stackoverflow.com/questions/54709025/understanding-positional-arguments-in-python
[13] keyword argument: https://docs.python.org/3/tutorial/controlflow.html#keyword-arguments
[14] realpython - Python args and kwargs: Demystified: https://realpython.com/python-kwargs-and-args/
[15] PEP 249 – Python Database API Specification v2.0: https://peps.python.org/pep-0249/
[16] sqlite: https://www.sqlite.org/index.html
[17] mysql: https://www.mysql.com
[18] GitHub Gist: https://gist.github.com/ChenyangGao


继续滑动看下一个

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存