Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python为参数获取了多个值_Python_Python 3.x - Fatal编程技术网

Python为参数获取了多个值

Python为参数获取了多个值,python,python-3.x,Python,Python 3.x,我有一个对象,带有一个依赖项注入字段,用于实现方法provide()的对象: 我为它编写了一个自定义提供程序对象,如下所示: # INTERFACE: class Provider(ABC): @abstractmethod def provide(self, *args, **kwargs): pass @abstractmethod def add_product(self, name: str, product: ProviderProd

我有一个对象,带有一个依赖项注入字段,用于实现方法provide()的对象:

我为它编写了一个自定义提供程序对象,如下所示:

# INTERFACE:

class Provider(ABC):
    @abstractmethod
    def provide(self, *args, **kwargs):
        pass

    @abstractmethod
    def add_product(self, name: str, product: ProviderProduct, *args, **kwargs) -> None:
        pass

    @abstractmethod
    def remove_product(self, name: str, *args, **kwargs) -> None:
        pass

class ProviderProduct(ABC):
    @abstractmethod
    def configure(self, *args, **kwargs) -> ProviderProduct:
        pass


IMPLEMENTATION:

class TestProvider(Provider):
    REGISTERED = dict()

    def provide(self, product, from_dict, *args, **kwargs):
        if product in TestProvider.REGISTERED:
            return TestProvider.REGISTERED[product].configure(from_dict)
        raise KeyError("{} not in REGISTERED".format(product))

class AbstractTestProduct(ProviderProduct, ABC):
    INDEX = [0]
    COLUMNS = list()

    def configure(self, from_dict: Dict) -> ProviderProduct:
        df = pd.DataFrame(index=self.INDEX, columns=self.COLUMNS)
        df.update(from_dict)
        return df

    def add_product(self, name, product):
        if isinstance(product, AbstractTestProduct):
            TestProvider.REGISTERED[name] = product
        else:
            raise ValueError("Given {} class is not of AbstractTestProduct type".format(product.__class__.__name__))

    def remove_product(self, name):
        if name in TestProvider.REGISTERED.keys():
            del TestProvider.REGISTERED[name]
        else:
            raise KeyError("{} not found in registered list".format(name))
现在,当我为它编写一些单元测试时,出现了一些问题:

class TestProviderTestSuite(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        class TestProviderProduct1(AbstractTestProduct):
            COLUMNS = ['test_col_1_1', 'test_col_1_2', 'test_col_1_3']

        class TestProviderProduct2(AbstractTestProduct):
            COLUMNS = ['test_col_2_1', 'test_col_2_2', 'test_col_2_3', 'test_col_2_4']

        class TestProviderProduct3(AbstractTestProduct):
            COLUMNS = ['test_col_3_1', 'test_col_3_2', 'test_col_3_3', 'test_col_3_4', 'test_col_3_5']
        
        cls.data_container = DataContainer()
        cls.data_container.register_provider("dataframe", TestProvider())
        cls.data_container.change_provider('dataframe')
        cls.data_container.add_provider_product("a", TestProviderProduct1())
        cls.data_container.add_provider_product("b", TestProviderProduct2())
        cls.data_container.add_provider_product("c", TestProviderProduct3())

    def test_should_provide_empty_product_df_a(self):
        # Given
        # -
        
        # When
        product_df_a = self.data_container.provide(product="a")

        # Then
        self.assertEqual(3, len(product_df_a.columns))
        self.assertEqual(1, len(product_df_a.index))
        self.assertTrue(0 in product_df_a.index)
        self.assertTrue(product_df_a.isnull().all(axis='columns').values[0])
我在上面的测试中得到以下错误:

self = <utils.providers.test_provider.TestProviderTestSuite testMethod=test_should_provide_empty_product_df_a>

    def test_should_provide_empty_product_df_a(self):
        # Given
        # -
    
        # When
>       product_df_a = self.data_container.provide(product="a")

tests\test_provider.py:43: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <DataContainer.DataContainer object at 0x0000022FF859CB48>, args = (), kwargs = {'product': 'a'}

    def provide(self, *args, **kwargs):
        if self.provider:
>           return self.provider.provide(self, *args, **kwargs)
E           TypeError: provide() got multiple values for argument 'product'
self=
def测试_应提供_空_产品_df_a(自身):
#给定
# -
#什么时候
>product\u df\u a=自身数据容器提供(product=“a”)
tests\test\u provider.py:43:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self=,args=(),kwargs={'product':'a'}
def提供(自身、*args、**kwargs):
如果是自助服务提供商:
>返回self.provider.provide(self,*args,**kwargs)
E TypeError:Provider()为参数“product”获取了多个值
我不明白为什么解释器在接收到对DataContainer的方法的调用时,provide(product=“a”)会确定“product”关键字参数被传递了两次

有什么想法吗


对我来说,这是在Python 3.7上发生的。

在DataContainer中,函数
provide
接受参数
*args
**kwargs
,这意味着当您在测试中调用它时,
product=“a”
将被收集到
kwargs

这意味着当您使用
TestProvider
provide
功能时,您将不会将产品作为第一个参数,而是作为关键字参数(在
**kwargs
中)。第二个
产品
来自位置参数,该参数由作为参数传递的self填充(是否确实要将self作为第一个参数传递?)


他们有很多方法来解决这个问题,最简单的方法是使所有
提供的
的签名相同

谢谢,伙计-你的答案很清楚,并且允许我来解决它-将“self”作为TestProvider的参数传递给DataContainer中的.provide()就是问题所在,我在代码中犯了一个意外错误。卸下后,现在一切正常。
self = <utils.providers.test_provider.TestProviderTestSuite testMethod=test_should_provide_empty_product_df_a>

    def test_should_provide_empty_product_df_a(self):
        # Given
        # -
    
        # When
>       product_df_a = self.data_container.provide(product="a")

tests\test_provider.py:43: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <DataContainer.DataContainer object at 0x0000022FF859CB48>, args = (), kwargs = {'product': 'a'}

    def provide(self, *args, **kwargs):
        if self.provider:
>           return self.provider.provide(self, *args, **kwargs)
E           TypeError: provide() got multiple values for argument 'product'