Skip to content

Instantly share code, notes, and snippets.

@flaccid
Created March 5, 2016 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaccid/d8faf594b7b494df047d to your computer and use it in GitHub Desktop.
Save flaccid/d8faf594b7b494df047d to your computer and use it in GitHub Desktop.
python metaprogramming heh
>>> dev.bLength
18
>>> foo = 'bLength'
>>> getattr(dev, foo)()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
@flaccid
Copy link
Author

flaccid commented Mar 5, 2016

import usb

VENDOR_ID       = 0x0FC5
PRODUCT_ID      = 0xB080
INTERFACE_ID    = 0

dev = usb.core.find(idProduct = PRODUCT_ID)

# was it found?
if dev is None:
    raise ValueError('Device not found')

dev_fields = ['bLength',
              'bDescriptorType',
              'bcdUSB',
              'bDeviceClass',
              'bDeviceSubClass',
              'bDeviceProtocol',
              'bMaxPacketSize0',
              'idVendor',
              'idProduct',
              'bcdDevice',
              'iManufacturer',
              'iProduct',
              'iSerialNumber',
              'bNumConfigurations',]

for field in dev_fields:
    print(getattr(dev, field)())

@flaccid
Copy link
Author

flaccid commented Mar 5, 2016

def scan():
    import usb
    import json

    VENDOR_ID       = 0x0FC5
    PRODUCT_ID      = 0xB080
    INTERFACE_ID    = 0

    dev = usb.core.find(idProduct = PRODUCT_ID)

    # was it found?
    if dev is None:
        raise ValueError('Device not found')

    dev_fields = ['bLength',
                  'bDescriptorType',
                  'bcdUSB',
                  'bDeviceClass',
                  'bDeviceSubClass',
                  'bDeviceProtocol',
                  'bMaxPacketSize0',
                  'idVendor',
                  'idProduct',
                  'bcdDevice',
                  'iManufacturer',
                  'iProduct',
                  'iSerialNumber',
                  'bNumConfigurations',]

    data = {}

    for field in dev_fields:
        value = str(getattr(dev, field))
        data[field] = value

    data['unique_id'] = "%s-%s-%s-%s" % (dev.idVendor,dev.idProduct,dev.bus,dev.address)
    json_data = json.dumps(data)

    return json_data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment