1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
6
files inside PyTables</TITLE
9
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
11
TITLE="PyTables User's Guide"
12
HREF="index.html"><LINK
14
TITLE="FileNode - simulating a filesystem with PyTables"
15
HREF="c5708.html"><LINK
17
TITLE="Finding a FileNode
19
HREF="x5744.html"><LINK
21
TITLE="Complementary notes"
22
HREF="x5875.html"></HEAD
33
SUMMARY="Header navigation table"
45
> User's Guide: Hierarchical datasets in Python - Release 1.3.2</TH
61
>Chapter 6. FileNode - simulating a filesystem with <SPAN
100
CLASS="computeroutput"
102
> sub-package of <SAMP
103
CLASS="computeroutput"
106
The recommended way to import the module is:
110
> >>> from tables.nodes import FileNode
117
few symbols, so you can import <SAMP
118
CLASS="computeroutput"
121
interactive usage. In fact, you will most probably only use
123
CLASS="computeroutput"
127
CLASS="computeroutput"
130
CLASS="computeroutput"
136
CLASS="computeroutput"
138
> constant contains the value that
140
CLASS="computeroutput"
142
> system attribute of a node file is expected
144
CLASS="computeroutput"
146
>, as we have seen). Although
147
this is not expected to change, you should use <SPAN
149
>FileNode.NodeType</SPAN
152
CLASS="computeroutput"
158
CLASS="computeroutput"
161
CLASS="computeroutput"
164
equivalent to the Python <SAMP
165
CLASS="computeroutput"
169
CLASS="computeroutput"
171
>) for ordinary files. Their arguments
172
differ from that of <SAMP
173
CLASS="computeroutput"
176
only point where you will note the difference between
177
working with a node file and working with an ordinary file.
180
>For this little tutorial, we will assume that we have a
182
CLASS="computeroutput"
184
> database opened for writing. Also, if
185
you are somewhat lazy at typing sentences, the code that we
186
are going to explain is included in the
188
CLASS="computeroutput"
189
>examples/filenodes1.py</SAMP
193
>You can create a brand new file with these sentences:
197
> >>> import tables
198
>>> h5file = tables.openFile('fnode.h5', 'w')
205
NAME="subsection6.3.1"
206
>6.3.1. Creating a new file node</A
209
>Creation of a new file node is achieved with the
211
CLASS="computeroutput"
213
> call. You must tell it in which
215
CLASS="computeroutput"
217
> file you want to create it, where in
219
CLASS="computeroutput"
221
> hierarchy you want to create the
222
node and which will be its name. The
224
CLASS="computeroutput"
226
> file is the first argument to
228
CLASS="computeroutput"
230
>; it will be also called the
232
CLASS="computeroutput"
233
>'host PyTables file'</SAMP
235
arguments must be given as keyword arguments
237
CLASS="computeroutput"
240
CLASS="computeroutput"
243
As a result of the call, a brand new appendable and
244
readable file node object is returned.
247
>So let us create a new node file in the previously opened
249
CLASS="computeroutput"
252
CLASS="computeroutput"
256
CLASS="computeroutput"
258
> and placed right under the root
259
of the database hierarchy. This is that command:
263
> >>> fnode = FileNode.newNode(h5file, where='/', name='fnode_test')
266
>That is basically all you need to create a file node.
267
Simple, isn't it? From that point on, you can use
269
CLASS="computeroutput"
271
> as any opened Python file (i.e. you can
272
write data, read data, lines of text and so on).
276
CLASS="computeroutput"
278
> accepts some more keyword
279
arguments. You can give a title to your file with the
281
CLASS="computeroutput"
283
> argument. You can use
285
CLASS="computeroutput"
287
>' compression features with the
289
CLASS="computeroutput"
291
> argument. If you know beforehand the
292
size that your file will have, you can give its final file
293
size in bytes to the <SAMP
294
CLASS="computeroutput"
298
CLASS="computeroutput"
300
> library would be able to
301
optimize the data access.
305
CLASS="computeroutput"
308
CLASS="computeroutput"
311
node where it is told to. To prove it, we will try to get
313
CLASS="computeroutput"
315
> attribute from the newly created
320
> >>> print h5file.getNodeAttr('/fnode_test', 'NODE_TYPE')
329
NAME="subsection6.3.2"
330
>6.3.2. Using a file node</A
333
>As stated above, you can use the new node file as any
334
other opened file. Let us try to write some text in and
339
> >>> print >> fnode, "This is a test text line."
340
>>> print >> fnode, "And this is another one."
341
>>> print >> fnode
342
>>> fnode.write("Of course, file methods can also be used.")
344
>>> fnode.seek(0) # Go back to the beginning of file.
346
>>> for line in fnode:
348
'This is a test text line.\n'
349
'And this is another one.\n'
351
'Of course, file methods can also be used.'
354
>This was run on a Unix system, so newlines are expressed
356
CLASS="computeroutput"
358
>. In fact, you can override the line
359
separator for a file by setting its
361
CLASS="computeroutput"
363
> property to any string you
367
>While using a file node, you should take care of closing
376
CLASS="computeroutput"
378
> host file. Because of the way
380
CLASS="computeroutput"
382
> works, your data it will not be at a
383
risk, but every operation you execute after closing the
384
host file will fail with a <SAMP
385
CLASS="computeroutput"
388
close a file node, simply delete the corresponding reference
390
CLASS="computeroutput"
396
> >>> fnode.close()
397
>>> print fnode.closed
406
NAME="subsection6.3.3"
407
>6.3.3. Opening an existing file node</A
410
>If you have a file node that you created using
412
CLASS="computeroutput"
414
>, you can open it later by calling
416
CLASS="computeroutput"
418
>. Its arguments are similar to
420
CLASS="computeroutput"
423
CLASS="computeroutput"
426
first argument is the <SAMP
427
CLASS="computeroutput"
430
want to open (i.e. a node with a <SAMP
431
CLASS="computeroutput"
434
attribute having a <SAMP
435
CLASS="computeroutput"
438
second argument is a mode string indicating how to open
439
the file. Contrary to <SAMP
440
CLASS="computeroutput"
444
CLASS="computeroutput"
446
> can not be used to create a new
450
>File nodes can be opened in read-only mode
452
CLASS="computeroutput"
454
>) or in read-and-append mode
456
CLASS="computeroutput"
458
>). Reading from a file node is allowed
459
in both modes, but appending is only allowed in the second
460
one. Just like Python files do, writing data to an
461
appendable file places it after the file pointer if it is
462
on or beyond the end of the file, or otherwise after the
463
existing data. Let us see an example:
467
> >>> node = h5file.root.fnode_test
468
>>> fnode = FileNode.openNode(node, 'a+')
469
>>> print repr(fnode.readline())
470
'This is a test text line.\n'
471
>>> print fnode.tell()
473
>>> print >> fnode, "This is a new line."
474
>>> print repr(fnode.readline())
478
>Of course, the data append process places the pointer at
479
the end of the file, so the last <SAMP
480
CLASS="computeroutput"
484
CLASS="computeroutput"
486
>. Let us seek to the beginning
487
of the file to see the whole contents of our file.
491
> >>> fnode.seek(0)
492
>>> for line in fnode:
494
'This is a test text line.\n'
495
'And this is another one.\n'
497
'Of course, file methods can also be used.This is a new line.\n'
500
>As you can check, the last string we wrote was correctly
501
appended at the end of the file, instead of overwriting
502
the second line, where the file pointer was positioned by
503
the time of the appending.
511
NAME="subsection6.3.4"
512
>6.3.4. Adding metadata to a file node</A
515
>You can associate arbitrary metadata to any open node
516
file, regardless of its mode, as long as the host
518
CLASS="computeroutput"
520
> file is writable. Of course, you
522
CLASS="computeroutput"
526
CLASS="computeroutput"
528
> to do it directly on the proper
533
much more comfortable way to do it. <SAMP
534
CLASS="computeroutput"
537
objects have an <SAMP
538
CLASS="computeroutput"
540
> property which gives
541
you direct access to their corresponding
543
CLASS="computeroutput"
548
>For instance, let us see how to associate MIME type
549
metadata to our file node:
553
> >>> fnode.attrs.content_type = 'text/plain; charset=us-ascii'
556
>As simple as A-B-C. You can put nearly anything in an
557
attribute, which opens the way to authorship, keywords,
558
permissions and more. Moreover, there is not a fixed list
559
of attributes. However, you should avoid names in all
560
caps or starting with <SAMP
561
CLASS="computeroutput"
565
CLASS="computeroutput"
570
> may use them internally.
575
> >>> fnode.attrs.author = "Ivan Vilata i Balaguer"
576
>>> fnode.attrs.creation_date = '2004-10-20T13:25:25+0200'
577
>>> fnode.attrs.keywords_en = ["FileNode", "test", "metadata"]
578
>>> fnode.attrs.keywords_ca = ["FileNode", "prova", "metadades"]
579
>>> fnode.attrs.owner = 'ivan'
580
>>> fnode.attrs.acl = {'ivan': 'rw', '@users': 'r'}
583
>You can check that these attributes get stored by running
585
CLASS="computeroutput"
587
> command on the host
589
CLASS="computeroutput"
595
> $ ptdump -a fnode.h5:/fnode_test
596
/fnode_test (EArray(113,)) ''
597
/fnode_test.attrs (AttributeSet), 14 attributes:
600
FLAVOR := 'numarray',
602
NODE_TYPE_VERSION := 2,
605
acl := {'ivan': 'rw', '@users': 'r'},
606
author := 'Ivan Vilata i Balaguer',
607
content_type := 'text/plain; charset=us-ascii',
608
creation_date := '2004-10-20T13:25:25+0200',
609
keywords_ca := ['FileNode', 'prova', 'metadades'],
610
keywords_en := ['FileNode', 'test', 'metadata'],
618
assumptions about the meaning of your metadata, so its
619
handling is entirely left to your needs and imagination.
628
SUMMARY="Footer navigation table"
685
>Complementary notes</TD
b'\\ No newline at end of file'