Several useful python tools

Keywords: pip xml

1. PySnooper - debugging Kit

Installation:

pip install pysnooper

Using PySnooper to add a decorator to the function to be debugged, you can get the log of the running function, including the executed code line, execution time and the time when the local variable changes
Example

import pysnooper

@pysnooper.snoop()
def  bug_test(test_num):
    test_num +=1
    print(test_num)

2. loguru - a simple and elegant logging tool

Installation:

pip install loguru

Usage method
···
from loguru import logger
logger.debug('this is a debug message')
···
Log output to the console directly. If you want to input to the specified file:

logger.add('runtime.log')
logger.debug('this is a debug')

Other parameters

def add(
    self,
    sink,
    *,
    level=_defaults.LOGURU_LEVEL,
    format=_defaults.LOGURU_FORMAT,
    filter=_defaults.LOGURU_FILTER,
    colorize=_defaults.LOGURU_COLORIZE,
    serialize=_defaults.LOGURU_SERIALIZE,
    backtrace=_defaults.LOGURU_BACKTRACE,
    diagnose=_defaults.LOGURU_DIAGNOSE,
    enqueue=_defaults.LOGURU_ENQUEUE,
    catch=_defaults.LOGURU_CATCH,
    **kwargs
):

The concepts and formats of format, filter and level are basically the same as the logging module, for example:

logger.add('runtime.log', format="{time} {level} {message}", filter="my_module", level="INFO")

Delete the contents of the rewrite log file

from loguru import logger

trace = logger.add('runtime.log')
logger.debug('this is a debug message')
logger.remove(trace)
logger.debug('this is another debug message')

rotation parameter
Indicates the update of the file. For example, if we want to output a log file every day, or the file is too large to automatically separate the log files, we can directly use the rotation parameter of the add method for configuration.

logger.add('runtime_{time}.log', rotation="500 MB")

It can store a file every 500MB, and a new log file will be created if each log file is too large. We added a time placeholder when configuring the log name, so that the time can be automatically replaced during generation, and a log file with a file name containing time can be generated.
In addition, we can use the rotation parameter to create a log file on a regular basis, for example:

logger.add('runtime_{time}.log', rotation='00:00')

Specific description

- an |int| which corresponds to the maximum file size in bytes before that the current
  logged file is closed and a new one started over.
- a |timedelta| which indicates the frequency of each new rotation.
- a |time| which specifies the hour when the daily rotation should occur.
- a |str| for human-friendly parametrization of one of the previously enumerated types.
  Examples: ``"100 MB"``, ``"0.5 GB"``, ``"1 month 2 weeks"``, ``"4 days"``, ``"10h"``,
  ``"monthly"``, ``"18:00"``, ``"sunday"``, ``"w0"``, ``"monday at 12:00"``, ...
- a |function|_ which will be called before logging. It should accept two
  arguments: the logged message and the file object, and it should return ``True`` if
  the rotation should happen now, ``False`` otherwise.

retention parameter
Configure maximum log retention time

logger.add('runtime.log', retention='10 days')

Only the log s of the last 10 days are reserved
compression parameter
loguru can also configure the compression format of the file, for example, save in the zip file format, as follows:

logger.add('runtime.log', compression='zip')

Traceback records
In many cases, if we encounter running errors, and if we don't configure Traceback's output when printing out the log accidentally, we may not be able to trace the error.

But after using loguru, we can directly record Traceback with the decorator provided by loguru. Such a configuration can

@logger.catch
def my_function(x, y, z):
    # An error? It's caught anyway!
    return 1 / (x + y + z)

After running, it can be found that Traceback information appears in the log, and the variable value at that time is output to us. It can't be praised any more! The results are as follows:

> File "run.py", line 15, in <module>
   my_function(0, 0, 0)
   └ <function my_function at 0x1171dd510>

 File "/private/var/py/logurutest/demo5.py", line 13, in my_function
   return 1 / (x + y + z)
               │   │   └ 0
               │   └ 0
               └ 0

ZeroDivisionError: division by zero

3. pytest tool

Installation:

pip install pytest

Rules for writing pytest test samples:

  • Test files start with test UU (or end with UU test)
  • Test class starts with test and cannot have init method
  • Test function starts with test
  • Assertions can be made by using the basic assert

Generate test report

# Generate report in Html format
py.test --resultlog=path

# Generate reports in XML format
py.test --junitxml=path
19 original articles published, 7 praised, 30000 visitors+
Private letter follow

Posted by The-Last-Escape on Mon, 24 Feb 2020 21:40:26 -0800