21
21
from pandas .io .formats .printing import pprint_thing , pprint_thing_encoded
22
22
23
23
24
- class Scope (_scope .Scope ):
24
+ class PyTablesScope (_scope .Scope ):
25
25
__slots__ = ("queryables" ,)
26
26
27
27
queryables : Dict [str , Any ]
@@ -38,13 +38,13 @@ def __init__(
38
38
39
39
40
40
class Term (ops .Term ):
41
- env : Scope
41
+ env : PyTablesScope
42
42
43
43
def __new__ (cls , name , env , side = None , encoding = None ):
44
44
klass = Constant if not isinstance (name , str ) else cls
45
45
return object .__new__ (klass )
46
46
47
- def __init__ (self , name , env : Scope , side = None , encoding = None ):
47
+ def __init__ (self , name , env : PyTablesScope , side = None , encoding = None ):
48
48
super ().__init__ (name , env , side = side , encoding = encoding )
49
49
50
50
def _resolve_name (self ):
@@ -68,7 +68,8 @@ def value(self):
68
68
69
69
70
70
class Constant (Term ):
71
- def __init__ (self , value , env , side = None , encoding = None ):
71
+ def __init__ (self , value , env : PyTablesScope , side = None , encoding = None ):
72
+ assert isinstance (env , PyTablesScope ), type (env )
72
73
super ().__init__ (value , env , side = side , encoding = encoding )
73
74
74
75
def _resolve_name (self ):
@@ -270,7 +271,7 @@ def evaluate(self):
270
271
raise ValueError ("query term is not valid [{slf}]" .format (slf = self ))
271
272
272
273
rhs = self .conform (self .rhs )
273
- values = [ TermValue ( v , v , self . kind ). value for v in rhs ]
274
+ values = list ( rhs )
274
275
275
276
if self .is_in_table :
276
277
@@ -386,7 +387,7 @@ def prune(self, klass):
386
387
return None
387
388
388
389
389
- class ExprVisitor (BaseExprVisitor ):
390
+ class PyTablesExprVisitor (BaseExprVisitor ):
390
391
const_type = Constant
391
392
term_type = Term
392
393
@@ -486,25 +487,29 @@ def _validate_where(w):
486
487
TypeError : An invalid data type was passed in for w (e.g. dict).
487
488
"""
488
489
489
- if not (isinstance (w , (Expr , str )) or is_list_like (w )):
490
- raise TypeError ("where must be passed as a string, Expr, or list-like of Exprs" )
490
+ if not (isinstance (w , (PyTablesExpr , str )) or is_list_like (w )):
491
+ raise TypeError (
492
+ "where must be passed as a string, PyTablesExpr, "
493
+ "or list-like of PyTablesExpr"
494
+ )
491
495
492
496
return w
493
497
494
498
495
- class Expr (expr .Expr ):
496
- """ hold a pytables like expression, comprised of possibly multiple 'terms'
499
+ class PyTablesExpr (expr .Expr ):
500
+ """
501
+ Hold a pytables-like expression, comprised of possibly multiple 'terms'.
497
502
498
503
Parameters
499
504
----------
500
- where : string term expression, Expr , or list-like of Exprs
505
+ where : string term expression, PyTablesExpr , or list-like of PyTablesExprs
501
506
queryables : a "kinds" map (dict of column name -> kind), or None if column
502
507
is non-indexable
503
508
encoding : an encoding that will encode the query terms
504
509
505
510
Returns
506
511
-------
507
- an Expr object
512
+ a PyTablesExpr object
508
513
509
514
Examples
510
515
--------
@@ -520,8 +525,8 @@ class Expr(expr.Expr):
520
525
"major_axis>=20130101"
521
526
"""
522
527
523
- _visitor : Optional [ExprVisitor ]
524
- env : Scope
528
+ _visitor : Optional [PyTablesExprVisitor ]
529
+ env : PyTablesScope
525
530
526
531
def __init__ (
527
532
self ,
@@ -542,14 +547,14 @@ def __init__(
542
547
# capture the environment if needed
543
548
local_dict = DeepChainMap ()
544
549
545
- if isinstance (where , Expr ):
550
+ if isinstance (where , PyTablesExpr ):
546
551
local_dict = where .env .scope
547
552
_where = where .expr
548
553
549
554
elif isinstance (where , (list , tuple )):
550
555
where = list (where )
551
556
for idx , w in enumerate (where ):
552
- if isinstance (w , Expr ):
557
+ if isinstance (w , PyTablesExpr ):
553
558
local_dict = w .env .scope
554
559
else :
555
560
w = _validate_where (w )
@@ -559,11 +564,11 @@ def __init__(
559
564
_where = where
560
565
561
566
self .expr = _where
562
- self .env = Scope (scope_level + 1 , local_dict = local_dict )
567
+ self .env = PyTablesScope (scope_level + 1 , local_dict = local_dict )
563
568
564
569
if queryables is not None and isinstance (self .expr , str ):
565
570
self .env .queryables .update (queryables )
566
- self ._visitor = ExprVisitor (
571
+ self ._visitor = PyTablesExprVisitor (
567
572
self .env ,
568
573
queryables = queryables ,
569
574
parser = "pytables" ,
@@ -601,30 +606,31 @@ def evaluate(self):
601
606
class TermValue :
602
607
""" hold a term value the we use to construct a condition/filter """
603
608
604
- def __init__ (self , value , converted , kind : Optional [str ]):
609
+ def __init__ (self , value , converted , kind : str ):
610
+ assert isinstance (kind , str ), kind
605
611
self .value = value
606
612
self .converted = converted
607
613
self .kind = kind
608
614
609
- def tostring (self , encoding ):
615
+ def tostring (self , encoding ) -> str :
610
616
""" quote the string if not encoded
611
617
else encode and return """
612
618
if self .kind == "string" :
613
619
if encoding is not None :
614
- return self .converted
620
+ return str ( self .converted )
615
621
return '"{converted}"' .format (converted = self .converted )
616
622
elif self .kind == "float" :
617
623
# python 2 str(float) is not always
618
624
# round-trippable so use repr()
619
625
return repr (self .converted )
620
- return self .converted
626
+ return str ( self .converted )
621
627
622
628
623
629
def maybe_expression (s ) -> bool :
624
630
""" loose checking if s is a pytables-acceptable expression """
625
631
if not isinstance (s , str ):
626
632
return False
627
- ops = ExprVisitor .binary_ops + ExprVisitor .unary_ops + ("=" ,)
633
+ ops = PyTablesExprVisitor .binary_ops + PyTablesExprVisitor .unary_ops + ("=" ,)
628
634
629
635
# make sure we have an op at least
630
636
return any (op in s for op in ops )
0 commit comments