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

Designing the main() function

The main() function, defined on line 39, is fairly straightforward in this scenario. This function handles initial variable assignments and setup before calling parse_setup(). In the following code block, we create a docstring, surrounded with three double quotes where we document the purpose of the function, along with the data returned by it, as shown on lines 40 through 43. Pretty sparse, right? We'll enhance our documentation as we proceed as things might change drastically this early in development:

039 def main():
040 """
041 Primary controller for script.
042 :return: None
043 """

After the docstring, we hardcode the path to the setupapi.dev.log file on line 45. This means that our script can only function correctly if a log file with this name is located in the same directory as our script:

045     file_path = 'setupapi.dev.log'

On lines 48 through 50, we print our script information, including name and version, to the console, which notifies the user that the script is running. In addition, we print out 22 equal signs to provide a visual distinction between the setup information and any other output from the script:

047     # Print version information when the script is run
048 print('='*22)
049 print('SetupAPI Parser, v', __date__)
050 print('='*22)

Finally, on line 51, we call our next function to parse the input file. This function expects a str object that represents the path to the setupapi.dev.log. Though it may seem to defeat the purpose of a main() function, we will place the majority of the functionality in a separate function. This allows us to reuse code that's dedicated to the primary functionality in other scripts and for the main() function to act more as a primary controller. An example of this will be shown in the final iteration of this code. See the following line of code:

051     parse_setupapi(file_path) 
主站蜘蛛池模板: 郴州市| 沾益县| 大同市| 蓬安县| 霍州市| 崇仁县| 宜阳县| 醴陵市| 石泉县| 雷州市| 易门县| 巢湖市| 嘉禾县| 泰州市| 泰顺县| 五大连池市| 夏津县| 庄河市| 门源| 甘孜县| 增城市| 沾化县| 潞城市| 临桂县| 康保县| 镇康县| 中宁县| 什邡市| 绍兴县| 屏东县| 三门峡市| 宁河县| 齐河县| 西乡县| 正镶白旗| 康马县| 金湖县| 昌宁县| 鄯善县| 额济纳旗| 朝阳县|