File: file.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 (530 lines) | stat: -rwxr-xr-x 10,775 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
.. _file:

.. note::

    Python provides native support for manipulating files. Use that whenever possible
    to ensure your code is understandable by the greatest number of people.


File Access (objected-oriented via NEURON)
------------------------------------------


.. class:: File


    Syntax:
        ``fobj = h.File()``

        ``fobj = h.File("filename")``


    Description:
        This class allows you to open several files at once, whereas the top level 
        functions, :func:`ropen` and :func:`wopen` , 
        allow you to deal only with one file at a time. 
         
        The functionality of :func:`xopen` is not implemented in this class so use 

        .. code-block::
            python

            h.xopen(fobj.getname())

        to execute a sequence of interpreter commands in a file. 
         

    Example:

        .. code-block::
            python
 
            from neuron import h
            f1 = h.File()		//state that f1, f2, and f3 are pointers to the File class 
            f2 = h.File() 
            f3 = h.File() 
            f1.ropen("file1")	//open file1 for reading 
            f2.wopen("file2")	//open file2 for writing 
            f3.aopen("file3")	//open file3 for appending to the end of the file 

        opens file1, file2, and file3 for reading, writing, and appending (respectively). 
         

    .. warning::
        The mswindows/dos version must deal with the difference between 
        binary and text mode files. The difference is transparent to the 
        user except for one limitation. If you mix text and binary data 
        and you write text first to the file, then you need to do a 
        File.seek(0) to explicitly switch to binary mode just after opening the file 
        and prior to the first File.printf. 
        An error message will occur if you 
        read/write text to a file in text mode and then try to read/write a binary 
        vector.  This issue does not arise with the unix version. 

    .. seealso::
        :ref:`printf_doc`, :func:`ropen`, :func:`xopen`, :func:`wopen`


----



.. method:: File.ropen


    Syntax:
        ``.ropen()``

        ``.ropen("name")``


    Description:
        Open the file for reading. If the argument is 
        not present it opens (for reading) the last specified file. 

         

----



.. method:: File.wopen


    Syntax:
        ``.wopen()``

        ``.wopen("name")``


    Description:
        Open the file for writing.  If the argument is 
        not present it opens the last specified file. 

         

----



.. method:: File.aopen


    Syntax:
        ``.aopen()``

        ``.aopen("name")``


    Description:
        Open the file for appending to the end of the file. If the argument is 
        not present it opens the last specified file. 

         

----



.. method:: File.xopen


    Syntax:
        ``.xopen()``

        ``.xopen("name")``


    Description:
        Open the file and execute it. (**not implemented**) 
         
        Note: if instead of a "*name*", the number 0,1,or 2 is specified then 
        the stdin, stdout, or stderr is opened. (**not implemented**) 

         

----



.. method:: File.close


    Syntax:
        ``.close()``


    Description:
        Flush and close the file. This occurs automatically 
        whenever opening another file or destroying the object. 

         

----



.. method:: File.mktemp


    Syntax:
        ``success = f.mktemp()``


    Description:
        Sets the name to a temporary filename in the /tmp directory (or other 
        writable path for mswin and mac). Success returns 1. 

    Example of creating a temporary file:

        .. code-block::
            python

            f = h.File()
            if f.mktemp() != 1:
                raise Exception('Unable to create temporary file')
            # create a tempoary file, get its name
            temp_file_name = f.getname()

            # do stuff, possibly using regular Python File IO instead

            # dispose of the temporary file
            f.unlink()
         

----



.. method:: File.unlink


    Syntax:
        ``success = f.unlink()``


    Description:
        Remove the file specified by the current name. A return value of 
        1 means the file was removed (or at least it's link count was reduced by 
        one and the filename no longer exists). 

         

----



.. method:: File.printf


    Syntax:
        ``.printf("format", args, ...)``


    Description:
        As in standard C \ ``printf`` and the normal 
        NEURON :func:`printf` . 

         

----



.. method:: File.scanvar


    Syntax:
        ``.scanvar()``


    Description:
        Reads the next number as in the function ``fscan()`` and 
        returns its value. 
         
        Note: in order that .eof will return 
        true after the last number, the last digit of that number 
        should either be the last character in the file or 
        be followed by a newline which is the last character in the file. 

         

----



.. method:: File.scanstr


    Syntax:
        ``.scanstr(strptr)``


    Description:
        Read the next string (delimited by whitespace) into 
        \ ``strptr`` (must be a pointer to a NEURON string *not* a Python string).
        Returns the length of a string (if failure then returns 
        -1 and the string pointed to by ``strptr`` is unchanged). 

         

----



.. method:: File.gets


    Syntax:
        ``.gets(_ref_str)``


    Description:
        Read up to and including end of line. Returns length of	string. 
        If at the end of file, returns -1 and does not change the argument. 

        ``_ref_str`` is a reference to a NEURON string (e.g. one created via
        ``_ref_str = h.ref('')``); it is not a Python string.

         

----



.. method:: File.getname


    Syntax:
        ``name = fobj.getname()``

        ``name = fobj.getname(strptr)``


    Description:
        Return the name of the last specified file as a string. 
        For backward compatibility, if the arg is present (must a pointer to a NEURON string) also copy it to that. 

         

----



.. method:: File.dir


    Syntax:
        ``dirname = file.dir()``


    Description:
        Return the pathname of the last directory moved to in the chooser. 
        If the :meth:`File.chooser` has not been created, return the empty string. 

         

----



.. method:: File.eof


    Syntax:
        ``fobj.eof()``


    Description:
        Return true if at end of ropen'd file. 

         

----



.. method:: File.flush


    Syntax:
        ``fobj.flush()``


    Description:
        Flush pending output to the file. 

         

----



.. method:: File.isopen


    Syntax:
        ``fobj.isopen()``


    Description:
        Return true if a file is open. 

         

----



.. method:: File.chooser


    Syntax:
        ``.chooser()``

        ``.chooser("w,r,a,x,d or nothing")``

        ``.chooser("w,r,a,x,d or nothing", "Banner", "filter", "accept", "cancel", "path")``



    Description:
        File chooser interface for writing , reading, appending, or 
        just specifying a directory or filename without opening. The banner is 
        optional. The filter, eg. \ ``"*.dat"`` specifies the files shown 
        in the browser part of the chooser. 
        The "path" arg specifies the file or directory to use when the 
        browser first pops up. 
        The form with args sets the style of the chooser but 
        does not pop it up. With no args, the browser pops up and can 
        be called several times. Each time starting where it left 
        off previously. 
         
        The "d" style is used for selecting a directory (in 
        contrast to a file). 
        With the "d" style, three buttons are placed beneath the 
        browser area with :guilabel:`Open` centered beneath the :guilabel:`Show`, :guilabel:`Cancel` button pair. 
        The :guilabel:`Open` button must be pressed for the 
        dialog to return the name of the directory. The :guilabel:`Show` button merely 
        selects the highlighted browser entry and shows the relevant directory 
        contents. A returned directory 
        string always has a final "/". 
         
        The "*x*" style is unimplemented. Use 

        .. code-block::
            none

            f.chooser("", "Execute a hoc file", "*.hoc", "Execute") 
            if f.chooser():
                h.xopen(f.getname()) 

        Example:

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

            f = h.File()
            f.chooser('', 'Example file browser', '*', 'Type file name', 'Cancel')
            while f.chooser():
                print(f.getname())

        .. image:: ../../images/filechooser.png
            :align: center


----

The following comes courtesy of Zach Mainen, ``zach@helmholtz.sdsc.edu``:

----


.. method:: File.vwrite


    Syntax:
        ``.vwrite(_ref_x)``

        ``.vwrite(n, _ref_x)``


    Description:
        Write binary doubles to a file from an array or variable 
        using \ ``fwrite()``. The form with two arguments specifies the 
        number of elements to write and the address from which to 
        begin writing.  With one argument, *n* is assumed to be 1. 
        Must be careful that  *x*\ [] has at least *n* 
        elements after its passed address. 

        i.e. If ``x = h.Vector(10)`` and ``f`` is an instance of a :class:`File`
        opened for writing, then one might call ``f.vwrite(5, x._ref_x[0]`` to write
        the first five values to a file.)

         

----



.. method:: File.vread


    Syntax:
        ``.vread(_ref_x)``

        ``.vread(n, _ref_x)``


    Description:
        Read binary doubles from a file into a pre-existing :class:`Vector` 
        or variable using \ ``fread()``. 

    .. seealso::
        :func:`vwrite`
        

         
         

----



.. method:: File.seek


    Syntax:
        ``.seek()``

        ``.seek(offset)``

        ``.seek(offset,origin)``


    Description:
        Set the file position.  Any subsequent file access will access 
        data beginning at the new position.  Without arguments, goes to 
        the beginning of file.  Offset is in characters and is measured 
        from the beginning of the file unless origin is 1 (measures from 
        the current position) or 2 (from the end of the file).  Returns 
        0 if successful, non-zero on error.  Used with :meth:`tell`. 

         

----



.. method:: File.tell


    Syntax:
        ``.tell()``


    Description:
        Return the current file position or -1 on error.  Used with :meth:`seek`.