11""" manage PyTables query interface via Expressions """
22
33import ast
4- import warnings
54from functools import partial
6- from datetime import datetime , timedelta
75import numpy as np
86import pandas as pd
97
@@ -452,6 +450,26 @@ def _rewrite_membership_op(self, node, left, right):
452450 return self .visit (node .op ), node .op , left , right
453451
454452
453+ def _validate_where (w ):
454+ """
455+ Validate that the where statement is of the right type.
456+
457+ The type may either be String, Expr, or list-like of Exprs.
458+
459+ Parameters
460+ ----------
461+ w : String term expression, Expr, or list-like of Exprs.
462+
463+ Raises
464+ ------
465+ TypeError : An invalid data type was passed in for w (e.g. dict).
466+ """
467+
468+ if not (isinstance (w , (Expr , string_types )) or is_list_like (w )):
469+ raise TypeError ("where must be passed as a string, Expr, "
470+ "or list-like of Exprs" )
471+
472+
455473class Expr (expr .Expr ):
456474
457475 """ hold a pytables like expression, comprised of possibly multiple 'terms'
@@ -481,11 +499,9 @@ class Expr(expr.Expr):
481499 "major_axis>=20130101"
482500 """
483501
484- def __init__ (self , where , op = None , value = None , queryables = None ,
485- encoding = None , scope_level = 0 ):
502+ def __init__ (self , where , queryables = None , encoding = None , scope_level = 0 ):
486503
487- # try to be back compat
488- where = self .parse_back_compat (where , op , value )
504+ _validate_where (where )
489505
490506 self .encoding = encoding
491507 self .condition = None
@@ -505,7 +521,7 @@ def __init__(self, where, op=None, value=None, queryables=None,
505521 if isinstance (w , Expr ):
506522 local_dict = w .env .scope
507523 else :
508- w = self . parse_back_compat (w )
524+ _validate_where (w )
509525 where [idx ] = w
510526 where = ' & ' .join (["(%s)" % w for w in where ]) # noqa
511527
@@ -519,59 +535,6 @@ def __init__(self, where, op=None, value=None, queryables=None,
519535 encoding = encoding )
520536 self .terms = self .parse ()
521537
522- def parse_back_compat (self , w , op = None , value = None ):
523- """ allow backward compatibility for passed arguments """
524-
525- if isinstance (w , dict ):
526- w , op , value = w .get ('field' ), w .get ('op' ), w .get ('value' )
527- if not isinstance (w , string_types ):
528- raise TypeError (
529- "where must be passed as a string if op/value are passed" )
530- warnings .warn ("passing a dict to Expr is deprecated, "
531- "pass the where as a single string" ,
532- FutureWarning , stacklevel = 10 )
533- if isinstance (w , tuple ):
534- if len (w ) == 2 :
535- w , value = w
536- op = '=='
537- elif len (w ) == 3 :
538- w , op , value = w
539- warnings .warn ("passing a tuple into Expr is deprecated, "
540- "pass the where as a single string" ,
541- FutureWarning , stacklevel = 10 )
542-
543- if op is not None :
544- if not isinstance (w , string_types ):
545- raise TypeError (
546- "where must be passed as a string if op/value are passed" )
547-
548- if isinstance (op , Expr ):
549- raise TypeError ("invalid op passed, must be a string" )
550- w = "{0}{1}" .format (w , op )
551- if value is not None :
552- if isinstance (value , Expr ):
553- raise TypeError ("invalid value passed, must be a string" )
554-
555- # stringify with quotes these values
556- def convert (v ):
557- if isinstance (v , (datetime , np .datetime64 ,
558- timedelta , np .timedelta64 )):
559- return "'{0}'" .format (v )
560- return v
561-
562- if isinstance (value , (list , tuple )):
563- value = [convert (v ) for v in value ]
564- else :
565- value = convert (value )
566-
567- w = "{0}{1}" .format (w , value )
568-
569- warnings .warn ("passing multiple values to Expr is deprecated, "
570- "pass the where as a single string" ,
571- FutureWarning , stacklevel = 10 )
572-
573- return w
574-
575538 def __unicode__ (self ):
576539 if self .terms is not None :
577540 return pprint_thing (self .terms )
0 commit comments