File: printf.rst

package info (click to toggle)
neuron 8.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 34,760 kB
  • sloc: cpp: 149,571; python: 58,465; ansic: 50,329; sh: 3,510; xml: 213; pascal: 51; makefile: 35; sed: 5
file content (174 lines) | stat: -rwxr-xr-x 4,798 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
.. _printf_doc:


Printf (Formatted Output)
-------------------------

.. function:: printf

    .. warning::

        For code written in Python, it is generally more practical to use Python string
        formatting and file IO.

    Name:
        printf, fprint, sprint --- formatted output 
         

    Syntax:
        ``h.printf(format, ...)``

        ``h.fprint(format, ...)``

        ``h.sprint(string, format, ...)``



    Description:
        ``h.printf`` places output on the standard output.  ``h.fprint`` places output 
        on the file opened with the ``h.wopen(filename)`` command (standard 
        output if no file is opened).  ``h.sprint`` places 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: 
         


        f 
            signed value of the form -dddd.ddddd 

        e 
            signed value of the form -d.dddddde-nn 

        g 
            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. 

        d 
            signed value truncated and printed as integer. 

        o 
            printed as unsigned octal integer. 

        x 
            printed as unsigned hexadecimal integer 

        c 
            number treated as ascii code and printed as single character 

        s 
            string is printed, arg must be a string. 

         
        Between ``%`` 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 ``width.precis``. 
         
        Special characters of note are: 
         


        ``\n`` 
            newline 

        ``\t`` 
            tab 

        ``\r`` 
            carriage return without the line feed 

         
        ``h.printf`` and ``h.fprint`` return the number of characters printed. 
         

    Example:

        .. code::

            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:

        .. code::

            import math
            print('\tpi=%-20.10g sin(pi)=%f' % (math.pi, math.sin(math.pi)))

        .. note::

            The parentheses around the ``print`` argument are supplied in this way to allow
            it to work with both Python 2 and Python 3.

            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, ``print`` is a function and the return is ``None``.


    .. seealso::
        :meth:`File.ropen`
        

    .. warning::
        Only a subset of the C standard library functions. 
         

----


Redirect Standard Out
---------------------

.. function:: hoc_stdout


    Syntax:
        :samp:`h.hoc_stdout("{filename}")`

        ``h.hoc_stdout()``


    Description:
        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. 

    Example:

        .. code::

            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() 

    .. note::

        Despite the misleading name, this redirects standard out from both Python and HOC.