TAG

Python

  • 2020-03-10

Python: Output the log with logging module

It is general to export the log from the batch script which is executed periodically. The log is also useful to analyze the problem. You may insert the special log for debug to explore the reason of bug. The log is base facility to explore the reason of problem and recover from it. You can output the log from Python script with logging module. Simple Logging Most simple logging code is following. Execute this script, the following log will be output. How to output the log You can output the log by following functions. You need to select the function for appropriated logging level. info() warning() error() critical() exception() debug() The argument of these functions is a formatted string. The Logging Levels The importance of each logs are different. The logging level is an importance. For example, in production environment, the log is not needed which is output by debug() function. However, in development environment, it is an important for the developer. The output logging level can be specified with the code. Change the output logging level The minimum output logging level is changed with level argument of the basicConfig() method. In this sample code, the logging level of the output is […]

  • 2020-03-08

Python: Get the Command Line Argument with ArgumentParser

It is easy to get the command line argument of python shell script with ArgumentParser class. The ArgumentParser class has following functions: Get the positional required argument Get the optional argument Output the help If the script executed without required arguments, the ArgumentParser class abort the script. It is hard work to write above functions by yourself in your own script. Basic usage The basic code to use `ArgumentParser` class is following: Write your program description You can set the description of your program with the description parameter of the initializer. The description is displayed by --help option. Descriptions of programs and arguments can be omitted. However, we recommend that you do not omit it. Immediately after implementing the code, you know for yourself what the program is. However, after a while you may forget. In addition, if you take over and explain when another person is in charge or when someone uses it, you only need to hand it over. Parse and Get the Required Argument Use add_argument() method to parse and get the required argument. This code add the value argument. You can set the description of the argument to the help parameter. You can omit it. The passed value can be get with […]

>現役のプログラマーが書くプログラミング情報

現役のプログラマーが書くプログラミング情報

日々の開発の中での学びや分かったこと、調べたことなどを書いていくブログです。

CTR IMG