The error causes a TypeError in my function.
TypeError: 'NoneType' object is not subscriptable
I saw a similar one but it does not help that much: //stackoverflow.com/questions/68954674/python-usb-barcode-reading-typeerror
The function:
import time
from usb.core import find
from usb.util import get_string
import usb.backend.libusb1
import re
import usb.core
import usb.util
import os
import numpy as np
def info_usb_device[dev]:
xdev = find[idVendor=dev.idVendor, idProduct=dev.idProduct]
if xdev.bDeviceClass == 9: # don't list HUBs, see [USB class-codes][//www.usb.org/defined-class-codes]
return
if xdev._manufacturer is None:
xdev._manufacturer = get_string[xdev, xdev.iManufacturer, langid=1033]
if xdev._product is None:
xdev._product = get_string[xdev, xdev.iProduct, langid=1033]
device_info = '[%20s] %8d %9d %s - %s' % [xdev.serial_number, dev.idVendor, dev.idProduct,
str[xdev._manufacturer].strip[],
str[xdev._product].strip[]]
return xdev.serial_number, device_info
def add_usb_devices[device_dict]:
new_devices = []
for bus in usb.busses[]:
for dev in bus.devices:
if dev is None:
continue
serial_info = info_usb_device[dev]
if serial_info is not None:
[serial, info] = serial_info
if serial not in device_dict:
new_devices.append[serial]
device_dict[serial] = info
return new_devices
if __name__ == "__main__":
device_dict = {}
print['%22s %8s %9s %s' % ['serial', 'idVendor', 'idProduct', 'Manufacturer - Product']]
print['-'*22, '-'*8, '-'*9, '-'*30]
# first scan
add_usb_devices[device_dict]
for device_info in device_dict.values[]:
print[device_info]
# next scans
for i in range[5]: # run 5 more scans
new_serials = add_usb_devices[device_dict]
if len[new_serials] > 0:
print['** [scan %d] FOUND NEW USB DEVICES/SERIALS: %s' % [i, new_serials]]
for serial in new_serials:
print[device_dict[serial]]
time.sleep[3] # waiting 3 seconds before new scan
str_device_dict_serial = device_dict[serial]
listed__device_dict_serial = re.findall[r'\b\d+\b', str_device_dict_serial]
print['Scans completed.']
dev = usb.core.find[idVendor= listed__device_dict_serial[0], idProduct=listed__device_dict_serial[1]]
ep = dev[0].interfaces[][0].endpoints[][0]
i=dev[0].interfaces[][0].bInterfaceNumber
dev.reset[]
if dev.is_kernel_driver_active[i]:
print["hello joy"]
data_inside_content2222222 = np.loadtxt["/media/joy/Data/1111.txt", dtype='str' , delimiter=","]
print[data_inside_content2222222]
The output:
[base] joy@joy-System-Product-Name:~$ sudo python '/home/joy/fe_dir/10_4_detect_lastNew_plug_USB_and_raed.py'
serial idVendor idProduct Manufacturer - Product
---------------------- -------- --------- ------------------------------
[ None] 1133 49271 Logitech - USB Optical Mouse
[ 2004888] 1423 37728 - USB Reader
[5334354E373539325A315A4B] 5117 2112 Generic - External
** [scan 2] FOUND NEW USB DEVICES/SERIALS: ['01DZTW5EXY5TSUF8']
[ 01DZTW5EXY5TSUF8] 34148 4096 JetFlash - Mass Storage Device
Scans completed.
Traceback [most recent call last]:
File "/home/joy/fe_dir/10_4_detect_lastNew_plug_USB_and_raed.py", line 70, in
ep = dev[0].interfaces[][0].endpoints[][0]
TypeError: 'NoneType' object is not subscriptable
This is what happens when recursion goes wrong
I’m not talking about the stackoverflow you copy code from to do your job. I’m talking about what happens when I write bad recursion.
So what is a stack overflow?
According to wikipedia,
In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program.
Translation: There is a limited amount of memory [the call stack] allocated to a program. When that is exceeded [when it overflows], we call it a stack overflow.
Every time I’ve seen it, it’s been the result of infinite recursion. Recursion is when a method/function calls itself.
Sometimes recursion is intentional [ie: iterating over a graph]. Sometimes it’s unintentional [a callback triggers itself].
Now let’s cause intentionally cause stack overflows in a Python, Ruby and Javascript.
Python
In your favorite code editor, switch to python and run the following.
def do_recursion[]:
do_recursion[]do_recursion[]
This throws an error.
RecursionError: maximum recursion depth exceeded
Python throws a RecursionError
when the stack gets out of bounds.
Ruby
Switch your editor to ruby and run this.
def do_recursion
do_recursion
end
do_recursion
Which results in an error.
nodepad.rb:3:in `do_recursion': stack level too deep [SystemStackError]
Ruby throws a SystemStackError
when the stack level get’s too
deep.
Javascript
You can run this one in the JS console in chrome.
var do_recursion = function[] {
do_recursion[]
}
do_recursion[]
This throws an error.
Uncaught RangeError: Maximum call stack size exceeded
Javascript throws a RangeError when the stack is exceeded.
Conclusion
And that’s the real stack overflow.
In a nutshell, allocated memory is exceeded.
You won’t see it often unless you write a lot of recursive code. But it’s a good understanding to keep in your toolkit.