Search code examples
pythonintegerhex

My hex values are converted to int while passing as args


I am creating an usb connection, and this works.

device = usb.core.find(idVendor=0x47f, idProduct=0x2e6)

According to signature of the method, those arguments are passed to method as args

def find(find_all=False, backend = None, custom_match = None, **args):

I want to make class that uses this method, and can take same input, smth like:

    class MyNewClass(object):
    
        def __init__(self, find_all=False, backend=None, custom_match=None, **args):
            self.find_all = find_all
            self.backend = backend
            self.custom_match = custom_match
            self.args = args
    
        def __enter__(self):
            self.device = usb.core.find(find_all=self.find_all, backend=self.backend, custom_match=self.custom_match,
                                        args=self.args)
            return self.device

Unfortuantely, that approach tranalates input hex values to int, and this is smth that usb.core.find cannot take, therefore it returns None

    with MyNewClass(idVendor=0x47f, idProduct=0x2e6) as aaa:
        aaa = ''

enter image description here

I would like to avoid that conversion, and I am looking for ideas.


Solution

  • When calling usb.core.find, you're doing:

    self.device = usb.core.find(find_all=self.find_all, 
                                backend=self.backend, 
                                custom_match=self.custom_match,
                                args=self.args)
    

    This won't pass the keyword args properly. Change it to:

    self.device = usb.core.find(find_all=self.find_all, 
                                backend=self.backend, 
                                custom_match=self.custom_match,
                                **self.args)
    

    This will correctly pass the dictionary entries in self.args as keyword arguments.