Skip to content

Commit

Permalink
Special methods in File and Group documented.
Browse files Browse the repository at this point in the history
git-svn-id: http://www.pytables.org/svn/pytables/trunk@150 1b98710c-d8ec-0310-ae81-f5f2bcd8cb94
  • Loading branch information
Francesc Alted committed Jul 26, 2003
1 parent 8a19f56 commit 3ed1b08
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 50 deletions.
10 changes: 7 additions & 3 deletions THANKS
Expand Up @@ -17,8 +17,11 @@ suggesting me to implement new capabilities, report on bugs, and
supporting the PyTables project with contracts.

Todd Miller and Perry Greenfield for promptly helping me to understand
many of the intricacies of the numarray package. And also to Jin-chung
Hsu for discussions on recarray module (now numarray.records module).
many of the intricacies of the numarray package and Jin-chung Hsu for
discussions on recarray module (now numarray.records module). They
have been very receptive and promptly made most of the improvements in
numarray (specially in the records module) that were necessary for
PyTables.

Alan McIntyre for porting PyTables to Windows.

Expand All @@ -28,4 +31,5 @@ bypass the ~1000 levels of deepness that Python recursion limit
imposed.

The HDF5 team at NCSA for making such an excellent library for data
persistence.
persistence, and specially Pedro Vicente, for quickly including my
suggested patches to the HDF5_HL library.
Binary file modified doc/usersguide.pdf
Binary file not shown.
182 changes: 177 additions & 5 deletions doc/xml/usersguide.xml
Expand Up @@ -3416,6 +3416,67 @@ if __name__=="__main__":
</subsubsection>

</subsection>

<subsection>
<heading><visual markup="tt">File</visual> special
methods</heading>

<p>Following are described the methods that automatically
trigger actions when a <verb>File</verb> instance is
accessed in a special way (e.g.,
<verb>fileh("/detector")</verb> will cause a call to
<verb>group.__call__("/detector")</verb>).
</p>

<subsubsection>
<heading id="__callFileDescr">__call__(where="/",
classname="")</heading>

<p>Recursively iterate over the childs in the
<verb>File</verb> instance. It takes two parameters:</p>

<description>

<term>where</term> <item>If supplied, the iteration
starts from this group.</item>

<term>classname</term> <item><em>(String)</em> If
supplied, only instances of this class are
returned.</item>

</description>

<p>Example of use:</p>

<verbatim>
# Recursively print all the nodes hanging from '/detector'
print "Nodes hanging from group '/detector':"
for node in h5file("/detector"):
print node
</verbatim>

</subsubsection>

<subsubsection>
<heading id="__iterFileDescr">__iter__()</heading>

<p>Iterate over the childs on the <verb>File</verb>
instance. However, this does not accept parameters. This
iterator <em>is recursive</em>.</p>

<p>Example of use:</p>

<verbatim>
# Recursively list all the nodes in the object tree
print "All nodes in the object tree:"
for node in h5file:
print node
</verbatim>

</subsubsection>

</subsection>

</section>

<section id="GroupClassDescr">
Expand Down Expand Up @@ -3575,6 +3636,69 @@ if __name__=="__main__":
</description>

</subsection>

<subsection>
<heading><visual markup="tt">Group</visual> special
methods</heading>

<p>Following are described the methods that automatically
trigger actions when a <verb>Group</verb> instance is
accessed in a special way (e.g.,
<verb>group("Table")</verb> will cause a call to
<verb>group.__call__("Table")</verb>).
</p>

<subsubsection>
<heading id="__callGroupDescr">__call__(classname="",
recursive=0)</heading>

<p>Iterate over the childs in the <verb>Group</verb>
instance. It takes two parameters:</p>

<description>

<term>classname</term> <item><em>(String)</em> If
supplied, only instances of this class are
returned.</item>

<term>recursive</term> <item><em>(Integer)</em> If
false, only childs hanging immediately after the group
are returned. If true, a recursion over all the groups
hanging from it is performed. </item>

</description>

<p>Example of use:</p>

<verbatim>
# Recursively print all the arrays hanging from '/'
print "Arrays the object tree '/':"
for array in h5file.root(classname="Array", recursive=1):
print array
</verbatim>

</subsubsection>

<subsubsection>
<heading id="__iterGroupDescr">__iter__()</heading>

<p>Iterate over the childs on the group instance. However,
this does not accept parameters. This iterator is not
recursive.</p>

<p>Example of use:</p>

<verbatim>
# Non-recursively list all the nodes hanging from '/detector'
print "Nodes in '/detector' group:"
for node in h5file.root.detector:
print node
</verbatim>

</subsubsection>

</subsection>

</section>

<section id="LeafClassDescr">
Expand Down Expand Up @@ -3804,8 +3928,8 @@ if __name__=="__main__":
<p>Following are described the methods that automatically
trigger actions when a <verb>Table</verb> instance is
accessed in a special way (e.g.,
<verb>table[1:14:3]</verb> will call table.__getitem__(1,
14, 3)).
<verb>table["var2"]</verb> will cause a call to
<verb>table.__getitem__("var2")</verb>).
</p>

<subsubsection>
Expand All @@ -3815,13 +3939,43 @@ if __name__=="__main__":
<p>It returns the same iterator than
<verb>Table.iterrows(start, stop, step)</verb>. It is,
therefore, a shorter way to call it.</p>

<p>Example of use:</p>

<verbatim>
result = [ row['var2'] for row in table(step=4)
if row['var1'] &lt;= 20 ]
</verbatim>

<p>Which is equivalent to:</p>

<verbatim>
result = [ row['var2'] for row in table.iterrows(step=4)
if row['var1'] &lt;= 20 ]
</verbatim>

</subsubsection>
<subsubsection>
<heading id="__iterTableDescr">__iter__()</heading>

<p>It returns the same iterator than
<verb>Table.iterrows(0,0,1)</verb>. However, this does not
accept parameters.</p>

<p>Example of use:</p>

<verbatim>
result = [ row['var2'] for row in table
if row['var1'] &lt;= 20 ]
</verbatim>

<p>Which is equivalent to:</p>

<verbatim>
result = [ row['var2'] for row in table.iterrows()
if row['var1'] &lt;= 20 ]
</verbatim>

</subsubsection>

<subsubsection>
Expand All @@ -3843,10 +3997,28 @@ if __name__=="__main__":

<term><visual markup="tt">key</visual> is a
<verb>String</verb></term> <item>The <verb>key</verb>
is interpreted as a <em>column</em> of the table, and
it is read and returned as a NumArray or CharArray
object (whatever is appropriate).</item>
is interpreted as a <em>column</em> name of the table,
and, if it exists, it is read and returned as a
<verb>NumArray</verb> or <verb>CharArray</verb> object
(whatever is appropriate).</item>
</description>

<p>Example of use:</p>

<verbatim>
record = table[4]
recarray = table[4:1000:2]
narray = table["var2"]
</verbatim>

<p>Which is equivalent to:</p>

<verbatim>
record = table.read(start=4)[0]
recarray = table.read(start=4, stop=1000, step=2)
narray = table.read(field="var2")
</verbatim>

</subsubsection>
</subsection>
</section>
Expand Down
32 changes: 19 additions & 13 deletions tables/File.py
Expand Up @@ -4,7 +4,7 @@
# Author: Francesc Alted - falted@openlc.org
#
# $Source: /home/ivan/_/programari/pytables/svn/cvs/pytables/pytables/tables/File.py,v $
# $Id: File.py,v 1.46 2003/07/25 14:31:57 falted Exp $
# $Id: File.py,v 1.47 2003/07/26 18:42:53 falted Exp $
#
########################################################################

Expand All @@ -31,7 +31,7 @@
"""

__version__ = "$Revision: 1.46 $"
__version__ = "$Revision: 1.47 $"
format_version = "1.1" # File format version we write
compatible_formats = [] # Old format versions we can read

Expand Down Expand Up @@ -593,12 +593,12 @@ def setAttrNode(self, where, attrname, attrvalue, name=""):

def listNodes(self, where, classname = ""):

"""Returns a list with all the object nodes (Group or Leaf) hanging
from "where". The list is alphanumerically sorted by node name.
"where" can be a path string or Group instance. If a "classname"
parameter is supplied, the iterator will return only instances of
this class (or subclasses of it). The only supported classes in
"classname" are 'Group' and 'Leaf'."""
"""Returns a list with all the object nodes (Group or Leaf)
hanging from "where". The list is alphanumerically sorted by
node name. "where" can be a path string or Group instance. If
a "classname" parameter is supplied, only instances of this
class (or subclasses of it) are returned. The only supported
classes in "classname" are 'Group' and 'Leaf'."""

group = self.getNode(where, classname = 'Group')
if group <> -1:
Expand All @@ -607,12 +607,12 @@ def listNodes(self, where, classname = ""):
return []

def __iter__(self, where="/", classname=""):
"""Iterate over the nodes hanging from 'where'."""
"""Iterate over the nodes in the object tree."""

return self.iterTree(where, classname)
return self._iterTree(where, classname)

def iterTree(self, where="/", classname=""):
"""Iterate over all the nodes on tree"""
def _iterTree(self, where="/", classname=""):
"""Iterate over the nodes in the object tree."""

assert classname in [None, "", "Group", "Leaf", "Table", "Array"], \
"Incorrect specification of 'classname'"
Expand All @@ -626,7 +626,13 @@ def iterTree(self, where="/", classname=""):
yield leaf

def __call__(self, where="/", classname=""):
"""Iterate over the nodes hanging from 'where'."""
"""Iterate over the nodes in the object tree.
If "where" supplied, the iteration starts from this group.
If "classname" is supplied, only instances of this class are
returned.
"""

return self.__iter__(where, classname)

Expand Down
20 changes: 13 additions & 7 deletions tables/Group.py
Expand Up @@ -5,7 +5,7 @@
# Author: Francesc Alted - falted@openlc.org
#
# $Source: /home/ivan/_/programari/pytables/svn/cvs/pytables/pytables/tables/Group.py,v $
# $Id: Group.py,v 1.44 2003/07/17 18:57:28 falted Exp $
# $Id: Group.py,v 1.45 2003/07/26 18:42:54 falted Exp $
#
########################################################################

Expand Down Expand Up @@ -33,7 +33,7 @@
"""

__version__ = "$Revision: 1.44 $"
__version__ = "$Revision: 1.45 $"

MAX_DEPTH_IN_TREE = 2048
# Note: the next constant has to be syncronized with the
Expand Down Expand Up @@ -114,12 +114,12 @@ def __init__(self, title = "", new = 1):
self.__dict__["_v_childs"] = {}
return

def __iter__(self, classname="", recursive=0):
def __iter__(self, classname=None, recursive=0):
"""Iterate over the childs on self"""

return self._f_iterGroup(classname, recursive)

def _f_iterGroup(self, classname="", recursive=0):
def _f_iterGroup(self, classname=None, recursive=0):

assert classname in [None, "", "Group", "Leaf", "Table", "Array"], \
"Incorrect specification of 'classname'"
Expand All @@ -138,9 +138,15 @@ def _f_iterGroup(self, classname="", recursive=0):
for leaf in group._f_listNodes(classname):
yield leaf

def __call__(self, classname="", recursive=0):
"""Iterate over the childs on self"""

def __call__(self, classname=None, recursive=0):
"""Iterate over the childs on self
If "classname" is supplied, only instances of this class
are returned. If "recursive" is false, only childs
hanging immediately after the group are returned. If
true, a recursion over all the groups hanging from it is
performed. """

return self.__iter__(classname, recursive)

# This iterative version of _g_openFile is due to John Nielsen
Expand Down

0 comments on commit 3ed1b08

Please sign in to comment.