如何使用互斥参数创建Python函数?

时间:2018-02-14 18:52:38

标签: python arguments mutual-exclusion

我有一个Python类需要接受两个互斥参数中的一个。如果参数不是独占的,(即:如果给出了两者或两者都没有),则应该引发错误。

class OrgLocation:
    __init__(self, location_num=None, location_path=None):
        """location_num & location_path are mutually exclusive"""

在大多数情况下,最好的选择是制作两个单独的类。但是,我正在使用an external API,其中要求这两个属性是互斥的。

请求:

<OrgLocation LocationPathName="ROOT/BU/DIV/SL/DEPT/JOB" LocationNum="1234"/>

响应:

<Error Message="Use either LocationNum or LocationPathName but not both." ErrorCode="1186">

类似的问题似乎表明argparse可以用于命令行界面中的互斥参数,但我不确定如何将其应用于类构造函数

如何创建具有互斥参数的Python函数?

6 个答案:

答案 0 :(得分:3)

您可能希望在 init 方法中创建测试,但更好的问题可能是......为什么?

if location_num is not None and location_path is not None:
    raise TheseParametersAreMutuallyExclusiveError()

为什么要创建一个有多重目的的课程?为什么不创建单独的类?

答案 1 :(得分:1)

超越the answer by @Ivonet,Python中常见的方法是接受一个参数,并将其删除:

class Location:
    __init__(self, location):
        """location_num & location_path are mutually exclusive"""
        try:
            x = self.locationArray[location] #location is a num?
        except TypeError:
            x = self.locationDict[location] #location is a string?

可能有另一个例外。如果你想使用argparse,这可能只对两个参数有点过分,但可以很好地扩展:

import argparse

class Bla:
    parser = argparse.ArgumentParser(prog='Class Bla init')
    path_group = parser.add_mutually_exclusive_group(required=True)
    path_group.add_argument('--num',nargs=1,type=int)
    path_group.add_argument('--path',nargs=1,type=str)

    def __init__(self,**kwargs):
        args=self.parser.parse_args(sum(
            zip(map(
            lambda x: '--'+x,kwargs.keys()),
            map(str,kwargs.values())),()))

#Bla(x='abc')
#Bla(num='abc')
Bla(path='abc')
Bla(path='abc',num=3)

顶部顶部的结果:

usage: Class Bla init [-h] (--num NUM | --path PATH)
bla.py: error: one of the arguments --num --path is required

usage: Class Bla init [-h] (--num NUM | --path PATH)
bla.py: error: argument --num: invalid int value: 'abc'

<__main__.Bla object at 0x7fd070652160>

usage: Class Bla init [-h] (--num NUM | --path PATH)
bla.py: error: argument --num: not allowed with argument --path

这也很酷,因为Bla(help='anything')实际上会打印用法(并退出)。这是为了回答有关argparse的具体问题,但要明确的是,@ Ivonet的答案实际上会用于您的确切示例。

答案 2 :(得分:1)

虽然有点老套,但您可以按如下方式使用 XOR 运算符:

class OrgLocation:
    def __init__(self, location_num=None, location_path=None):
        """location_num & location_path are mutually exclusive"""
        assert (location_num is None) ^ bool(location_path is None), "location_num and location_path are mutually exclussive"
        print("OK")

答案 3 :(得分:0)

您想要做的事情很简单:

class Location:
    __init__(self, location_num=None, location_path=None):
    """location_num & location_path are mutually exclusive"""

    if location_num is not None and location_path is not None:
        raise ValueError("should have location_num or location_path, but not both")
    elif location_num:
        #create location from int
    elif location_str:
        #create location from str

但它不被认为是正确的python。您应该创建备用构造函数作为classmethods,而不是:

class Location:
    def __init__(self, parsed_location):
        #create location
    @classmethod
    def from_int(cls, location_int):
        return cls(parse_int(location_int))
    @classmethod
    def from_str(cls, location_str):
        return cls(parse_str(location_str))

有关更深入的示例,请参阅What is a clean, pythonic way to have multiple constructors in Python?

答案 4 :(得分:0)

我认为装饰器是实现此目的的好方法。 我确信我的实现可以改进,但是可以用,并且我认为使用起来很容易理解:

class MutuallyExclusiveArgsError(Exception):
    def __init__(self, groups):
        err = f"These groups or arguments are mutually exclusive: {','.join(str(tuple(g)) for g in groups)}"
        super().__init__(err)

def exclusive_args(*args):
    import attr
    import functools
    from typing import Callable,Set,Union,Iterable

    @attr.s
    class _inner:
        _arg_groups_conv = lambda val: {arg: group for group in {frozenset([s]) if isinstance(s, str) else s for s in val} for arg in group}

        func : Callable = attr.ib()
        arg_groups : Set[Union[str,Iterable]] = attr.ib(converter=_arg_groups_conv, kw_only=True)

        def __attrs_post_init_(self):
           functools.update_wrapper(self, self.func)

        def __call__(self, *args, **kwargs):
            groups = {self.arg_groups[kw] for kw in kwargs}
            if len(groups) > 1:
                raise MutuallyExclusiveArgsError(groups)
            self.func(*args, **kwargs)
    return functools.partial(_inner, arg_groups=args)

用法如下:

@exclusive_args("one", "two")
def ex(*, one=None, two=None):
    print(one or two)

ex(one=1, two=2)
---------------------------------------------------------------------------
MutuallyExclusiveArgsError                Traceback (most recent call last)
<ipython-input-38-0f1d142483d2> in <module>
----> 1 ex(one=1, two=2)
<ipython-input-36-c2ff5f47260f> in __call__(self, *args, **kwargs)
     21             groups = {self.arg_groups[kw] for kw in kwargs}
     22             if len(groups) > 1:
---> 23                 raise MutuallyExclusiveArgsError(groups)
     24             self.func(*args, **kwargs)
     25     return functools.partial(_inner, arg_groups=args)
MutuallyExclusiveArgsError: These groups or arguments are mutually exclusive: ('two',),('one',)
ex(one=1)
1
ex(two=2)
2

或类似这样:

@exclusive_args("one", ("two","three"))
def ex(*, one=None, two=None, three=None):
    print(one, two, three)

ex(one=1)
1 None None

ex(two=1)
None 1 None

ex(three=1)
None None 1

ex(two=1, three=2)
None 1 2

ex(one=1, two=2)
---------------------------------------------------------------------------
MutuallyExclusiveArgsError                Traceback (most recent call last)
<ipython-input-46-0f1d142483d2> in <module>
----> 1 ex(one=1, two=2)
<ipython-input-36-c2ff5f47260f> in __call__(self, *args, **kwargs)
     21             groups = {self.arg_groups[kw] for kw in kwargs}
     22             if len(groups) > 1:
---> 23                 raise MutuallyExclusiveArgsError(groups)
     24             self.func(*args, **kwargs)
     25     return functools.partial(_inner, arg_groups=args)
MutuallyExclusiveArgsError: These groups or arguments are mutually exclusive: ('one',),('two', 'three')

ex(one=1,three=3)
---------------------------------------------------------------------------
MutuallyExclusiveArgsError                Traceback (most recent call last)
<ipython-input-47-0dcb487cba71> in <module>
----> 1 ex(one=1,three=3)
<ipython-input-36-c2ff5f47260f> in __call__(self, *args, **kwargs)
     21             groups = {self.arg_groups[kw] for kw in kwargs}
     22             if len(groups) > 1:
---> 23                 raise MutuallyExclusiveArgsError(groups)
     24             self.func(*args, **kwargs)
     25     return functools.partial(_inner, arg_groups=args)
MutuallyExclusiveArgsError: These groups or arguments are mutually exclusive: ('one',),('two', 'three')

答案 5 :(得分:0)

我知道这是一个老问题,但我还没有看到有人使用我采用的简单方法。这个例子是一个普通的旧方法,但它在类的 __init__ 方法中同样有效:

def circle_area(radius=None, diameter=None, circumference=None):

# check for mutually-exclusive parameters
number_of_options_specified = len([opt for opt in [circumference, diameter, radius] if opt is not None])
if number_of_options_specified != 1:
    raise ValueError(f"Exactly one of radius ({radius}) / diameter ({diameter}) / circumference ({circumference}) must be specified")

# calculate
pi = 3.14
if radius is not None:
    area = pi * radius**2
if diameter is not None:
    area = pi * (diameter/2.0)**2
if circumference is not None:
    area = (circumference**2)/(4.0*pi)
return area