david.bri...@ubs.com said the following on 10/25/2010 12:19 PM: > I currently have 160 or so tables with attributes on. I wish to find the > all the tables with a certain set of attributes. This code is running a > little slow: > > def getTables(tsdb, attributes): > answer = [] > for node in tsdb: > if isinstance(node, pytables.Leaf): > matches = True > for items in attributes.items(): > if not node.attrs.__contains__(items[0]): > matches = False > break > if node.attrs.__getattr__(items[0]) <> items[1]: > matches = False > break > if matches: answer.append(node) > return answer
A few suggestions: 1) Instead of for node in tsdb: if isinstance(node, pytables.Leaf): consider using for node in tsdb.walkNodes('/', classname='Leaf') or (maybe better) for node in tsdb.walkNodes('/', classname='Table') This way you get the nodes you need without having to introspect to see if they're of the proper class. 2) Within your nested loop you call attributes.items() each time. You only need to do this once, so I recommend binding the value of items() to a local variable before entering the outer loop: attrItems = attributes.items() for node in tsdb.walkNodes(...): ... 3) Next, the so-called 'double under' methods (__contains__(), __getattr__()) are generally intended for internal use only by their containing objects. A better way would be to use the 'in' operator; to wit (note also the binding of node.attr.items()): nodeAttrItems = node.attrs.items() if attrItems[0] not in nodeAttrItems: ... if nodeAttrItems[0] <> attrItems[1]: ... Also if you did this you can eliminate your innermost loop. -- David E. Sallis, Senior Principal Engineer, Software General Dynamics Information Technology NOAA Coastal Data Development Center Stennis Space Center, Mississippi 228.688.3805 david.sal...@gdit.com david.sal...@noaa.gov -------------------------------------------- "Better Living Through Software Engineering" -------------------------------------------- ------------------------------------------------------------------------------ Nokia and AT&T present the 2010 Calling All Innovators-North America contest Create new apps & games for the Nokia N8 for consumers in U.S. and Canada $10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store http://p.sf.net/sfu/nokia-dev2dev _______________________________________________ Pytables-users mailing list Pytables-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pytables-users