Ross Bates (el 2007-09-06 a les 09:16:55 -0500) va dir::

> A Saturday 01 September 2007, Ross Bates escrigué:
> [...]
> I understand that the condensed where statement is not supported by
> the numexpr package,  the question I have which remains is more about
> Python best practices in this situation.
> 
> If I had a program which accepted 1 or more users parameters as values
> for the table.where clause, is dynamically building the condition to
> acheive an in-kernel select acceptable?
> 
> If it's possible, I like to avoid code that generates code so I'm just
> wondering if that's the only option.

Well, I think it is perfectly OK to construct your query dynamically
(but see below), and it is in fact as simple as::

  In [16]:values = ['a', 'b', 'c']
  In [17]:' | '.join('(src_id == %r)' % v for v in values)
  Out[17]:"(src_id == 'a') | (src_id == 'b') | (src_id == 'c')"

There are some Python concepts here you'd like to master first:
generator expressions, the % operator and its %r format and the
``str.join()`` method, but they are quite simple.

Just a couple of details: the previous way of building the expression
may not be desirable if you're comparing floating point of complex
numbers, since you may be losing precision when representing the value
literally (e.g. "src_id == 3.141592").  Also, if you're going to run
``where()`` repeatedly and you need speed, parsing a new expression each
time may become a bottleneck.  The solution to both problems is using
variables in the query expression, but that would make the solution a
little bit more involved.

Hope that helps,

::

        Ivan Vilata i Balaguer   >qo<   http://www.carabos.com/
               Cárabos Coop. V.  V  V   Enjoy Data
                                  ""

Attachment: signature.asc
Description: Digital signature

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users

Reply via email to