0

Given the following snippet, I expect captured_string to contain the disp statements from the scipy's fortran L-BFGS-B algorithm. This is not what happens. This leads to my question -- what does fortran's format statement print to when called by python via a .pyf file?

from io import StringIO
from contextlib import redirect_stdout

from scipy.optimize import minimize

def foo(x):
    return 1/(x+0.001)**2 + x

string = StringIO()
with redirect_stdout(string):
    result = minimize(foo, [100], method='L-BFGS-B', options={'disp': True})

captured_string = string.getvalue()
string.close()

print('this string appears before L-BFGS-B output')

("this string appears before L-BFGS-B output" appears after L-BFGS-B output)

9
  • Format statements do not print anything anywhere. Do you mean print or write statements? Commented Nov 22, 2017 at 7:00
  • See stackoverflow.com/questions/41350116/… with a reply by one of IPython's developer for the case of IPython. The issue of capturing the stdout of subprocesses is apparently not that easy. Commented Nov 22, 2017 at 8:22
  • Is this a duplicate? I don't want to close it single-handedly. At least no yet. Commented Nov 22, 2017 at 11:43
  • The other question is about the behaviour within IPython/Jupyter and that is more specific. The current question concerns "plain Python files" but the conclusions of Matt (other Q) apply also here I believe. Commented Nov 22, 2017 at 11:52
  • 1
    I do know Fortran. Format only sets the format string. It does not print anything anywhere. 'write` does the writing. Commented Nov 22, 2017 at 17:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.