Printf (Formatted Output)
For code written in Python, it is generally more practical to use Python string formatting and file IO.
- printf, fprint, sprint — formatted output
h.sprint(string, format, ...)
h.printfplaces output on the standard output.
h.fprintplaces output on the file opened with the
h.wopen(filename)command (standard output if no file is opened).
h.sprintplaces output in its string argument. These functions are subsets of their counterparts in the C standard library.
Each of these functions converts, formats, and prints its arguments after the format string under control of the format string.
The format string contains two types of objects: plain characters which are simply printed, and conversion specifications each of which causes conversion and printing of the next successive argument.
Each conversion specification is introduced by the character ‘
%‘ and ends with a conversion type specifier. The type specifiers supported are:
- signed value of the form -dddd.ddddd
- signed value of the form -d.dddddde-nn
- signed value in either ‘e’ or ‘f’ form based on given value and precision. Trailing zeros and the decimal point are printed only if necessary.
- signed value truncated and printed as integer.
- printed as unsigned octal integer.
- printed as unsigned hexadecimal integer
- number treated as ascii code and printed as single character
- string is printed, arg must be a string.
%and the conversion type, optional flags, width, precision and size specifiers can be placed. The most useful flag is ‘-’ which left justifies the result, otherwise the number is right justified in its field. Width and precision specifiers are of the form
Special characters of note are:
- carriage return without the line feed
h.fprintreturn the number of characters printed.
h.printf("\tpi=%-20.10g sin(pi)=%f\n", h.PI, h.sin(h.PI)) pi=3.141592654 sin(pi)=0.000000 42
Pure Python equivalent example:
import math print('\tpi=%-20.10g sin(pi)=%f' % (math.pi, math.sin(math.pi)))
The parentheses around the
This is not an identical replacement because it does not return the number of characters. In Python 2, this is a statement not a function and attempting to assign it to a variable is a syntax error. In Python 3,
Only a subset of the C standard library functions.
Redirect Standard Out
With a filename argument, switches the original standard out to filename. With no arguments. switches current standard out back to original filename.
Only one level of switching allowed. Switching back to original causes future output to append to the stdout. Switching to “filename” writes stdout from the beginning of the file.
from neuron import h def p(): print('one') # to original standard out h.hoc_stdout('temp.tmp') print('two') # to temp.tmp for sec in h.allsec(): h.psection(sec=sec) # to temp.tmp h.hoc_stdout() print('three') # to the original standard out p()
Despite the misleading name, this redirects standard out from both Python and HOC.