Created
March 5, 2016 03:17
-
-
Save flaccid/d8faf594b7b494df047d to your computer and use it in GitHub Desktop.
python metaprogramming heh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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 |
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