官术网_书友最值得收藏!

  • Learning Python for Forensics
  • Preston Miller Chapin Bryce
  • 540字
  • 2021-08-20 10:17:08

Interpreting the search_key() function

The search_key() function, originally called on line 62 of the main() function, is where we search for the user-supplied vendor and product IDs, and display the resolved results to the user. In addition, all of our error handling logic is contained within this function.

Let's practice accessing nested lists or dictionaries. We discussed this in the main() function; however, it pays to actually practice rather than take our word for it. Accessing nested structures requires us to use multiple indices rather than just one. For example, let's create a list and map that to key_1 in a dictionary. To access elements from the nested list, we will need to supply key_1 to access the list and then a numerical index to access elements of the list:

>>> inner_list = ['a', 'b', 'c', 'd']
>>> print(inner_list[0])
a
>>> outer_dict = {'key_1': inner_list}
>>> print(outer_dict['key_1'])
['a', 'b', 'c', 'd']
>>> print(outer_dict['key_1'][3])
d

Now, let's switch gears, back to the task at hand, and leverage our new skills to search our dictionary to find vendor and product IDs. The search_key() function is defined on line 65 and takes the user-supplied VID and PID along with our parsed out usb_dict dictionary. We then start by querying usb_dict for the vendor_key value, using the get() method of a dictionary to attempt to get the requested key's value or return None, as specified on line 66, if the key is not found.

Please note that the data returned by the get() call, if successful, is the entire value for that key, or in this case a list, where element zero is the vendor name and element one is the dictionary of product details. We can then check, on line 67, to see whether the key was found; if it was unavailable, we print this to the user and exit on lines 68 and 69, as shown here:

065 def search_key(vendor_key, product_key, usb_dict):
066 vendor = usb_dict.get(vendor_key, None)
067 if vendor is None:
068 print('Vendor ID not found')
069 exit()

We can then repeat this logic for looking up the product information, though we first have to navigate to the product information. On line 71, we access element one of the vendor list, containing the product details dictionary, and perform the same get() method call to look up any name resolution for the PID. In the same manner, we check to see if the lookup failed and provide any available details to the user; in case it fails, we can at least give the vendor information:

071     product = vendor[1].get(product_key, None)
072 if product is None:
073 print('Vendor: {}\nProduct Id not found.'.format(
074 vendor[0]))
075 exit(0)

If everything resolves successfully, we can print the output to the user and the script will complete! Notice how, on line 77, in the format statement, we have to call the first element of the vendor variable since the value of the VID key lookup was a list, whereas the value of the PID key lookup is just the product's name. This is where things can get a little confusing, though feel free to reference the earlier sample data structure and add as many intermediate print statements to help with comprehension:

077     print('Vendor: {}\nProduct: {}'.format(vendor[0], product))
主站蜘蛛池模板: 霞浦县| 沂源县| 宁安市| 通海县| 乌鲁木齐市| 东丽区| 前郭尔| 珠海市| 木兰县| 永仁县| 启东市| 安国市| 辉县市| 香港 | 平遥县| 云南省| 水城县| 阿荣旗| 仁寿县| 时尚| 呈贡县| 永年县| 交城县| 拉萨市| 北流市| 隆化县| 昌图县| 威信县| 米泉市| 涞水县| 湟源县| 曲水县| 沅江市| 宁强县| 康马县| 元阳县| 福贡县| 宜州市| 五河县| 辽宁省| 连南|