File: read.rst

package info (click to toggle)
neuron 8.2.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (187 lines) | stat: -rwxr-xr-x 5,505 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
.. _read:

Read from Terminal and Files
----------------------------

.. warning::
    These functions are provided for legacy code support. When writing new code, consider using Python input-output equivalents.

.. function:: xred


    Syntax:
        ``var = h.xred(promptstring, default, min, max)``


    Description:
        ``xred()`` reads a value from the standard input after printing *promptstring* 
        on the console.  The value read must be in the range *min* <= *val* <= *max* and 
        the user will be prompted to enter another number if the value is not in 
        that range.  If the user types the :kbd:`Return` key, the *default* value is used 
        (if it is in the valid range). 


----



.. function:: getstr


    Syntax:
        ``h.getstr(strvar)``

        ``h.getstr(strvar, 1)``


    Description:
        ``getstr()`` reads a string up to and including the next newline from the file 
        opened with the :meth:`~File.ropen` command (or the currently executing file or 
        the standard input) and places it 
        in its string variable argument. With a second arg equal to 1, getstr reads 
        a single word starting at the next non-whitespace character up to 
        but not including the following whitespace (similar to fscan).
    Example:
        .. code-block::
            python


            ###########################################
            ### create a file titled "file.dat" with: #
            ### hello                                 #
            ### world                                 #
            ###########################################
            
            from neuron import h, gui
            
            def r_open(ndat):
                h.ropen("file.dat")
                string = ""
                s = h.ref(string)
                x = []
                for i in range(ndat):
                    h.getstr(s, 1)
                    x.append(s[0])
                h.ropen()
                return x

            # ndat is number of data points
            ndat = 2
            x = r_open(ndat)
            print(x)


    .. seealso::
        :class:`StringFunctions`, :func:`sscanf`, :class:`File`


----



.. function:: sred


    Syntax:
        ``index = h.sred(prompt, defaultchar, charlist)``


    Description:
        ``sred()`` reads a character typed on the standard input after printing the 
        first argument followed by the default character. The user is required to 
        enter one of the characters in the character list (or return if the default 
        happens to be one of these characters). The function returns the index in 
        the character list of the character typed. The index of the first character 
        is 0. The character accepted becomes the next default when this statement 
        is executed again. This function was contributed by Stewart Jaslove. 

    Example:

        .. code-block::
            python
            
            from neuron import h, gui

            def response(answer):
                if (answer == 0):
                    print("No")
                else:
                    print("Yes")

            i = 0 
            while i == 0:
                i = h.sred("Shall we?", "y", "ny")
                response(i)
    



----



.. function:: fscan


    Syntax:
        ``var = h.scan()``


    Description:
        ``fscan()`` reads the next number from the file opened with the :meth:`~File.ropen` 
        command. If no file is opened the number is read from the currently 
        executing file. If no file is being executed the number is read from 
        the standard input. 
        A number is scanned as long as it begins with a digit, decimal point, or 
        sign.  There can be more than one number per line but they must be set 
        apart from each other by spaces or tabs.  Strings that can't be scanned 
        into numbers are skipped. 

    Example:
        Suppose in response to the command: ``print(fscan(), fscan())`` 
        the user types: ``this is a number 1.3e4 this is not45 this is 25`` 
        Then NEURON will print: ``13000 25`` 
         

        .. code-block::
            python

            ###########################################
            ### create a file titled "file.dat" with: #
            ### 42 13.7                               #
            ### 14 64.1                               #
            ### 12 9                                  #
            ###########################################

            from neuron import h, gui

            def r_open(ndat):
                h.ropen("file.dat")
                x = []
                y = []
                for i in range(ndat):
                    x.append(h.fscan())
                    y.append(h.fscan())
                    h.ropen()
                return x, y

            # ndat is number of data points
            ndat = 3
            x, y = r_open(ndat)
             


    Diagnostics:
        ``fscan()`` and ``getstr()`` returns to the HOC 
        interpreter with a run-time error on EOF. 
         
    .. note::
    These functions are provided for legacy code support. 
    In Python, it only supports file input not terminal input. 

    When writing new code, consider using Python input-output equivalents.

    .. seealso::
        :meth:`File.scanvar`, :ref:`read <keyword_read>`, :meth:`File.ropen`, :func:`File`, :func:`sscanf`, :class:`StringFunctions`, :func:`getstr`