print_to_loggingยถ
Print to Logging Transformer Module.
This module provides functionality to convert print statements to logging calls in Python files using LibCST. It automatically transforms print() calls to logging.info() calls for better production-ready code.
Example
>>> from haive.tools.toolkits.dev.python.cst_toolkit.transformers.print_to_logging import replace_print_with_logging
>>> replace_print_with_logging("/path/to/file.py")
- Before:
print(โHello, world!โ)
- After:
logging.info(โHello, world!โ)
Classesยถ
Replaces print statements with logging.info calls. |
Functionsยถ
|
Replaces all print() calls with logging.info() calls in a Python file. |
Module Contentsยถ
- class print_to_logging.PrintToLoggingTransformerยถ
Bases:
libcst.CSTTransformer
Replaces print statements with logging.info calls.
This transformer identifies print() function calls in Python code and replaces them with equivalent logging.info() calls for better production-ready code.
- Noneยถ
- leave_Expr(original_node: libcst.Expr, updated_node: libcst.Expr)ยถ
Replace print calls with logging.info() during the AST traversal.
- Parameters:
original_node (cst.Expr) โ The original expression node
updated_node (cst.Expr) โ The updated expression node
- Returns:
- The transformed expression with logging.info() if it was a print call,
otherwise the original node
- Return type:
cst.Expr
- print_to_logging.replace_print_with_logging(filepath: str)ยถ
Replaces all print() calls with logging.info() calls in a Python file.
This function reads a Python file, converts all print() function calls to logging.info() calls, and writes the updated code back to the file. It does not automatically add the import statement for the logging module.
- Parameters:
filepath (str) โ Path to the Python file to process
- Raises:
FileNotFoundError โ If the specified file does not exist
IOError โ If there are issues reading from or writing to the file