@@ -3314,8 +3314,8 @@ def _unconvert_index_legacy(data, kind, legacy=False, encoding=None):
3314
3314
def _convert_string_array (data , encoding , itemsize = None ):
3315
3315
3316
3316
# encode if needed
3317
- if encoding is not None :
3318
- f = np .vectorize (lambda x : x .encode (encoding ))
3317
+ if encoding is not None and len ( data ) :
3318
+ f = np .vectorize (lambda x : x .encode (encoding ), otypes = [ np . object ] )
3319
3319
data = f (data )
3320
3320
3321
3321
# create the sized dtype
@@ -3333,7 +3333,7 @@ def _unconvert_string_array(data, nan_rep=None, encoding=None):
3333
3333
# guard against a None encoding in PY3 (because of a legacy
3334
3334
# where the passed encoding is actually None)
3335
3335
encoding = _ensure_encoding (encoding )
3336
- if encoding is not None :
3336
+ if encoding is not None and len ( data ) :
3337
3337
f = np .vectorize (lambda x : x .decode (encoding ),otypes = [np .object ])
3338
3338
data = f (data )
3339
3339
@@ -3378,7 +3378,6 @@ class Term(object):
3378
3378
value : a value or list of values (required)
3379
3379
queryables : a kinds map (dict of column name -> kind), or None i column is non-indexable
3380
3380
encoding : an encoding that will encode the query terms
3381
- i : my term id number
3382
3381
3383
3382
Returns
3384
3383
-------
@@ -3399,18 +3398,13 @@ class Term(object):
3399
3398
_search = re .compile ("^\s*(?P<field>\w+)\s*(?P<op>%s)\s*(?P<value>.+)\s*$" % '|' .join (_ops ))
3400
3399
_max_selectors = 31
3401
3400
3402
- def __init__ (self , field , op = None , value = None , queryables = None , i = None , encoding = None ):
3401
+ def __init__ (self , field , op = None , value = None , queryables = None , encoding = None ):
3403
3402
self .field = None
3404
3403
self .op = None
3405
3404
self .value = None
3406
3405
self .q = queryables or dict ()
3407
3406
self .filter = None
3408
-
3409
- if i is None :
3410
- i = 0
3411
- self .i = i
3412
3407
self .condition = None
3413
- self .condvars = dict ()
3414
3408
self .encoding = encoding
3415
3409
3416
3410
# unpack lists/tuples in field
@@ -3498,15 +3492,10 @@ def kind(self):
3498
3492
""" the kind of my field """
3499
3493
return self .q .get (self .field )
3500
3494
3501
- def generate (self , v , i = None ):
3502
- """ create and return the op string for this TermValue
3503
- add the variable to condvars """
3504
- if i is None :
3505
- i = 0
3506
-
3507
- cv = "_%s_%s_%s" % (self .field ,self .i ,i )
3508
- self .condvars [cv ] = v .converted
3509
- return "(%s %s %s)" % (self .field , self .op , cv )
3495
+ def generate (self , v ):
3496
+ """ create and return the op string for this TermValue """
3497
+ val = v .tostring (self .encoding )
3498
+ return "(%s %s %s)" % (self .field , self .op , val )
3510
3499
3511
3500
def eval (self ):
3512
3501
""" set the numexpr expression for this term """
@@ -3534,7 +3523,7 @@ def eval(self):
3534
3523
3535
3524
# too many values to create the expression?
3536
3525
if len (values ) <= self ._max_selectors :
3537
- vs = [ self .generate (v , i ) for i , v in enumerate ( values ) ]
3526
+ vs = [ self .generate (v ) for v in values ]
3538
3527
self .condition = "(%s)" % ' | ' .join (vs )
3539
3528
3540
3529
# use a filter after reading
@@ -3600,6 +3589,15 @@ def __init__(self, value, converted, kind):
3600
3589
self .converted = converted
3601
3590
self .kind = kind
3602
3591
3592
+ def tostring (self , encoding ):
3593
+ """ quote the string if not encoded
3594
+ else encode and return """
3595
+ if self .kind == u'string' :
3596
+ if encoding is not None :
3597
+ return self .converted
3598
+ return '"%s"' % self .converted
3599
+ return self .converted
3600
+
3603
3601
class Coordinates (object ):
3604
3602
""" holds a returned coordinates list, useful to select the same rows from different tables
3605
3603
@@ -3637,7 +3635,6 @@ def __init__(self, table, where=None, start=None, stop=None, **kwargs):
3637
3635
self .start = start
3638
3636
self .stop = stop
3639
3637
self .condition = None
3640
- self .condvars = dict ()
3641
3638
self .filter = None
3642
3639
self .terms = None
3643
3640
self .coordinates = None
@@ -3652,8 +3649,6 @@ def __init__(self, table, where=None, start=None, stop=None, **kwargs):
3652
3649
terms = [ t for t in self .terms if t .condition is not None ]
3653
3650
if len (terms ):
3654
3651
self .condition = "(%s)" % ' & ' .join ([ t .condition for t in terms ])
3655
- for t in terms :
3656
- self .condvars .update (t .condvars )
3657
3652
self .filter = []
3658
3653
for t in self .terms :
3659
3654
if t .filter is not None :
@@ -3676,14 +3671,14 @@ def generate(self, where):
3676
3671
where = [where ]
3677
3672
3678
3673
queryables = self .table .queryables ()
3679
- return [Term (c , queryables = queryables , i = i , encoding = self .table .encoding ) for i , c in enumerate ( where ) ]
3674
+ return [Term (c , queryables = queryables , encoding = self .table .encoding ) for c in where ]
3680
3675
3681
3676
def select (self ):
3682
3677
"""
3683
3678
generate the selection
3684
3679
"""
3685
3680
if self .condition is not None :
3686
- return self .table .table .readWhere (self .condition , condvars = self . condvars , start = self .start , stop = self .stop )
3681
+ return self .table .table .readWhere (self .condition , start = self .start , stop = self .stop )
3687
3682
elif self .coordinates is not None :
3688
3683
return self .table .table .readCoordinates (self .coordinates )
3689
3684
return self .table .table .read (start = self .start , stop = self .stop )
@@ -3695,7 +3690,7 @@ def select_coords(self):
3695
3690
if self .condition is None :
3696
3691
return np .arange (self .table .nrows )
3697
3692
3698
- return self .table .table .getWhereList (self .condition , condvars = self . condvars , start = self .start , stop = self .stop , sort = True )
3693
+ return self .table .table .getWhereList (self .condition , start = self .start , stop = self .stop , sort = True )
3699
3694
3700
3695
3701
3696
### utilities ###
0 commit comments