[Understanding / application of with syntax] Flexible switching of Python standard output destination

I will introduce how to use the output destination (file output, console output) of Python standard output (print and sys.stdout) while switching appropriately with with syntax.

Motivation

You can change the output destination of the standard output according to the processing by doing the following,

import sys
#Temporarily change to file output
sys.stdout = open('out.log', 'a+')

...
sys.stdout.write('fugahoge')
...

#Return to console output
sys.stdout = sys.__stdout__

When the process is finished, I was wondering if there was something because it would be troublesome to restore it, and remembering that using the with syntax made close () after file open () unnecessary, this article I came to write.

What is written in this article

--How to change the standard output --Description of with syntax --How to change the output destination of standard output with with syntax

with syntax

The with syntax is used in various situations such as reading and writing files such as the following and gradient_tape of tensorflow.

with open('', 'r') as f:
  f.read()

Reference: with syntax (Python)

What's happening in the with syntax

What happens when you use the with syntax is that if you specify (or generate) an instance after with, the process will run as follows.

  1. The instance's ``` .__ enter__ () method is called
  2. Processing in the with syntax
  3. The instance's ``` .__ exit__ () method is called

Code example

If you execute the following code,

class Logger():
    def setIO(self, *args, **kwargs):
        #Generate TestIO instance
        return TestIO(*args, **kwargs)

class TestIO():
    def __enter__(self):
        print('enter')

    def __exit__(self, *args):
        print('exit')

logger = Logger()
with logger.setIO():
    print('---- in with syntax ----')

The output is as follows.

console


enter
---- in with syntax ----
exit

Now you can see that the processing is done in the order of `` .__ enter__ () method-> processing in the syntax-> .__ exit__ () `method.

reference: -What is the with syntax-Ice all year round -Implement a class that can be used with [Python] with syntax

Flexible switching of standard output destination with with syntax

Finally, I will introduce a code example of "Flexibly switching the output destination of standard output with with syntax" which is the subject of this article.

import sys

class SetIO():
    """I in with syntax/Class for switching O"""
    def __init__(self, filename: str):
        self.filename = filename

    def __enter__(self):
        sys.stdout = _STDLogger(out_file=self.filename)

    def __exit__(self, *args):
        sys.stdout = sys.__stdout__

class _STDLogger():
    """Custom I/O"""
    def __init__(self, out_file='out.log'):
        self.log = open(out_file, "a+")

    def write(self, message):
        self.log.write(message)

    def flush(self):
        # this flush method is needed for python 3 compatibility.
        pass

print('before with block')

with SetIO('out.log'):
    #Switch to file output
    print('---- in with syntax ----')

print('after with block')

When I run the above code, The following is output to the console.

console


before with block
after with block

And the following is output to the file (out.log).

out.log


---- in with syntax ----

I was able to switch the standard output destination with syntax by the above method.

Summary

You can change the output destination by using the namespace of the built-in logger module, but I think it is inconvenient to change the output destination in detail. (It's possible that you're just not familiar with logger) Therefore, I think there are situations where the method introduced in this article can be used. I hope it will be helpful for you!

Refs -What is the with syntax-Ice all year round -Implement a class that can be used with [Python] with syntax -with syntax (Python)

Recommended Posts

[Understanding / application of with syntax] Flexible switching of Python standard output destination
UnicodeEncodeError struggle with standard output of python3
with syntax (Python)
Receives and outputs standard output of Python 2 and Python 3> C implementations
Get standard output in real time with Python subprocess
Output log in JSON format with Python standard logging
Basics of python: Output
Application of Python 3 vars
[For beginners] Summary of standard input in Python (with explanation)
1. Statistics learned with Python 2. Probability distribution [Thorough understanding of scipy.stats]
Test standard output with Pytest
Web application with Python + Flask ② ③
Full understanding of Python debugging
Web application with Python + Flask ④
Convert to a string while outputting standard output with Python subprocess
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
Read the standard output of a subprocess line by line in Python
[Blender] List of shortcut keys of Python Console that comes standard with Blender
The story of making a standard driver for db with python.
Memorandum of understanding when Python is run on EC2 with Apache
I tried to output the rpm list of SSH login destination to an Excel sheet with Python + openpyxl.
Python: Application of supervised learning (regression)
Output to csv file with Python
Input / output with Python (Python learning memo ⑤)
Monitor Python application performance with Dynatrace ♪
Make standard output non-blocking in Python
Getting Started with Python Basics of Python
Comply with Python coding standard PEP8
[Note] Hello world output with python
Unit test log output with python
Life game with Python! (Conway's Game of Life)
[Python] Understanding the potential_field_planning of Python Robotics
10 functions of "language with battery" python
Implementation of Dijkstra's algorithm with python
RPC completed with standard Python3 modules
Coexistence of Python2 and 3 with CircleCI (1.0)
Application development with Docker + Python + Flask
Python application: Data cleansing # 2: Data cleansing with DataFrame
Application of graphs with plotly sliders
Basic study of OpenCV with Python
CSV output of pulse data with Raspberry Pi (confirm analog input with python)
Immediate display, forced display, flash of print output result with python (mainly jupyter)
I tried to streamline the standard role of new employees with Python
Explanation of creating an application for displaying images and drawing with Python