mirror of
https://github.com/InternLM/InternBootcamp.git
synced 2026-04-23 16:55:02 +00:00
init-commit
This commit is contained in:
commit
18a552597a
3461 changed files with 1150579 additions and 0 deletions
22
internbootcamp/bootcamp_utils/catch_print.py
Executable file
22
internbootcamp/bootcamp_utils/catch_print.py
Executable file
|
|
@ -0,0 +1,22 @@
|
|||
from contextlib import redirect_stdout
|
||||
import io
|
||||
|
||||
|
||||
def catch_print(func, **kwargs):
|
||||
output_capture = io.StringIO()
|
||||
with redirect_stdout(output_capture):
|
||||
result = func(**kwargs)
|
||||
output = output_capture.getvalue()
|
||||
output_capture.close()
|
||||
return output,result
|
||||
|
||||
if __name__ == '__main__':
|
||||
def greet(name):
|
||||
print(f"Hello, {name}!")
|
||||
return 42
|
||||
|
||||
# 使用 catch_print 函数
|
||||
output, result = catch_print(greet, name="Alice")
|
||||
|
||||
print("Captured Output:", output) # 输出: Captured Output: Hello, Alice!
|
||||
print("Function Result:", result) # 输出: Function Result: 42
|
||||
Loading…
Add table
Add a link
Reference in a new issue