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.
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.
--How to change the standard output --Description of with syntax --How to change the output destination of standard output with 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 happens when you use the with syntax is that if you specify (or generate) an instance after with, the process will run as follows.
method is called
method is calledIf 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
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.
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