0

I'm trying to write a class which will accept any kwargs.

class testcode():
    def check_values(**kwargs):
        if "Test1" in kwargs.values():
           return function1
        elif "Test2" in kwargs.values():
           return function2...
        ...
        else:
           return function20



test=testcode(Input1="Test1",Something="blablabla",Otherthing="blabalbalbalb2")

As the number of kwarg is not fixed, I may write lots of if/elif, can I use a dict to replace the if/elif/else?

update: @deceze, the reason why I ignore some args is that I want to simulate some AWS API. for example, AWS S3 is using

response = client.list_multipart_uploads(
    Bucket='string',
    Delimiter='string',
    EncodingType='url',
    KeyMarker='string',
    MaxUploads=123,
    Prefix='string',
    UploadIdMarker='string'
)

I want to write a class to mock the function. And the when I test, I can return the value based on UploadIdMarker.

class testcode():
    def list_multipart_uploads(**kwargs):
        if "Test1" in kwargs.values():
           return s3_string1
        elif "Test2" in kwargs.values():
           return s3_string2
        ...
        else:
           return s3_string100

test=testcode(Bucket='string',
    Delimiter='string',
    EncodingType='url',
    KeyMarker='string',
    MaxUploads=123,
    Prefix='string',
    UploadIdMarker='Test1'")
Free P
  • 41
  • 4
  • yes, you can ... – RomanPerekhrest Aug 27 '19 at 12:28
  • Possible duplicate of [How to convert all arguments to dictionary in python](https://stackoverflow.com/questions/44412617/how-to-convert-all-arguments-to-dictionary-in-python) – shuberman Aug 27 '19 at 12:29
  • 4
    That doesn't seem terribly sensible either way…?! You're just looking at any *value* and ignore the keywords themselves? So `testcode(foo='bar', bar='bar')` and `testcode(baz='blarg', quux='bar')` would return the same value? How does that make any practical sense? – deceze Aug 27 '19 at 12:29
  • Why you passing arguments to class, you need to call "check_values".Check on it – Narendra Lucky Aug 27 '19 at 12:31
  • There's the Y in the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), what's the X? – Paritosh Singh Aug 27 '19 at 12:35
  • Wouldn't it make the most sense to use a ["Python switch"](https://stackoverflow.com/a/60211/476) as `return {'Test1': ..., ...}[kwargs['UploadIdMarker']]`…? – deceze Aug 27 '19 at 12:46
  • Why are you accepting arbitrary keyword arguments if you don't care about the keyword and don't (depending on your version of Python) care about which argument you see first? – chepner Aug 27 '19 at 12:55
  • kwargs is just a dict. If you want this to depend on UploadIdMarker then test with 'UploadIdMarker' in kwargs, and get the value with kwargs['UploadIdMarker'] – Kenny Ostrom Aug 27 '19 at 13:05

0 Answers0