6
6
from io import StringIO
7
7
import itertools as it
8
8
import operator
9
+ import token
9
10
import tokenize
10
11
from typing import Type
11
12
15
16
from pandas .core import common as com
16
17
from pandas .core .computation .common import (
17
18
_BACKTICK_QUOTED_STRING ,
18
- _remove_spaces_column_name ,
19
+ _clean_special_characters_column_name ,
19
20
)
20
21
from pandas .core .computation .ops import (
21
22
_LOCAL_TAG ,
40
41
import pandas .io .formats .printing as printing
41
42
42
43
44
+ def tokenize_backtick_quoted_string (token_generator ):
45
+ """Creates a token from a backtick quoted string.
46
+ Moves the token_generator forwards till right after the next backtick."""
47
+ prev_toknum = token .OP # This will trigger the first token to have no space
48
+ new_tokval = ""
49
+ for toknum , tokval , _ , _ , _ in it .takewhile (
50
+ lambda tok : tok [1 ] != "`" , token_generator
51
+ ):
52
+ # This check will ensure that operators will not be surrounded by spaces
53
+ if toknum == token .OP or prev_toknum == token .OP :
54
+ new_tokval += tokval
55
+ else :
56
+ new_tokval += " " + tokval
57
+ prev_toknum = toknum
58
+
59
+ return _BACKTICK_QUOTED_STRING , new_tokval
60
+
61
+
43
62
def tokenize_string (source ):
44
63
"""
45
64
Tokenize a Python source code string.
@@ -57,14 +76,9 @@ def tokenize_string(source):
57
76
# string.
58
77
for toknum , tokval , _ , _ , _ in token_generator :
59
78
if tokval == "`" :
60
- tokval = " " .join (
61
- it .takewhile (
62
- lambda tokval : tokval != "`" ,
63
- map (operator .itemgetter (1 ), token_generator ),
64
- )
65
- )
66
- toknum = _BACKTICK_QUOTED_STRING
67
- yield toknum , tokval
79
+ yield tokenize_backtick_quoted_string (token_generator )
80
+ else :
81
+ yield toknum , tokval
68
82
69
83
70
84
def _rewrite_assign (tok ):
@@ -134,14 +148,14 @@ def _replace_locals(tok):
134
148
return toknum , tokval
135
149
136
150
137
- def _clean_spaces_backtick_quoted_names (tok ):
151
+ def _clean_backtick_quoted_names (tok ):
138
152
"""Clean up a column name if surrounded by backticks.
139
153
140
154
Backtick quoted string are indicated by a certain tokval value. If a string
141
155
is a backtick quoted token it will processed by
142
- :func:`_remove_spaces_column_name ` so that the parser can find this
156
+ :func:`_clean_special_characters_column_name ` so that the parser can find this
143
157
string when the query is executed.
144
- See also :meth:`NDFrame._get_space_character_free_column_resolver `.
158
+ See also :meth:`NDFrame._get_special_character_free_column_resolvers `.
145
159
146
160
Parameters
147
161
----------
@@ -155,7 +169,7 @@ def _clean_spaces_backtick_quoted_names(tok):
155
169
"""
156
170
toknum , tokval = tok
157
171
if toknum == _BACKTICK_QUOTED_STRING :
158
- return tokenize .NAME , _remove_spaces_column_name (tokval )
172
+ return tokenize .NAME , _clean_special_characters_column_name (tokval )
159
173
return toknum , tokval
160
174
161
175
@@ -176,7 +190,7 @@ def _preparse(
176
190
_replace_locals ,
177
191
_replace_booleans ,
178
192
_rewrite_assign ,
179
- _clean_spaces_backtick_quoted_names ,
193
+ _clean_backtick_quoted_names ,
180
194
),
181
195
):
182
196
"""Compose a collection of tokenization functions
@@ -789,7 +803,7 @@ def __init__(
789
803
preparser = partial (
790
804
_preparse ,
791
805
f = _compose (
792
- _replace_locals , _replace_booleans , _clean_spaces_backtick_quoted_names
806
+ _replace_locals , _replace_booleans , _clean_backtick_quoted_names
793
807
),
794
808
),
795
809
):
0 commit comments