Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit c741c38

Browse files
authored
refactor: simplify OrderedDict arguments in lexer (#598)
Python 3.6+ guarantees that kwargs order is preserved, thus we don't need to assure the order by passing them as a list of tuples.
1 parent df48cc5 commit c741c38

File tree

1 file changed

+37
-82
lines changed
  • google/cloud/bigquery/magics/line_arg_parser

1 file changed

+37
-82
lines changed

google/cloud/bigquery/magics/line_arg_parser/lexer.py

Lines changed: 37 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -49,90 +49,45 @@
4949
# the value of an option other than "--params", we do not really care about its
5050
# structure, and thus do not want to use any of the "Python tokens" for pattern matching.
5151
#
52-
# Since token definition order is important, an OrderedDict is needed with tightly
53-
# controlled member definitions (i.e. passed as a sequence, and *not* via kwargs).
52+
# Token definition order is important, thus an OrderedDict is used. In addition, PEP 468
53+
# guarantees us that the order of kwargs is preserved in Python 3.6+.
5454
token_types = OrderedDict(
55-
[
56-
(
57-
"state_parse_pos_args",
58-
OrderedDict(
59-
[
60-
(
61-
"GOTO_PARSE_NON_PARAMS_OPTIONS",
62-
r"(?P<GOTO_PARSE_NON_PARAMS_OPTIONS>(?=--))", # double dash - starting the options list
63-
),
64-
(
65-
"DEST_VAR",
66-
r"(?P<DEST_VAR>[^\d\W]\w*)", # essentially a Python ID
67-
),
68-
]
69-
),
70-
),
71-
(
72-
"state_parse_non_params_options",
73-
OrderedDict(
74-
[
75-
(
76-
"GOTO_PARSE_PARAMS_OPTION",
77-
r"(?P<GOTO_PARSE_PARAMS_OPTION>(?=--params(?:\s|=|--|$)))", # the --params option
78-
),
79-
("OPTION_SPEC", r"(?P<OPTION_SPEC>--\w+)"),
80-
("OPTION_EQ", r"(?P<OPTION_EQ>=)"),
81-
("OPT_VAL", r"(?P<OPT_VAL>\S+?(?=\s|--|$))"),
82-
]
83-
),
84-
),
85-
(
86-
"state_parse_params_option",
87-
OrderedDict(
88-
[
89-
(
90-
"PY_STRING",
91-
r"(?P<PY_STRING>(?:{})|(?:{}))".format(
92-
r"'(?:[^'\\]|\.)*'",
93-
r'"(?:[^"\\]|\.)*"', # single and double quoted strings
94-
),
95-
),
96-
("PARAMS_OPT_SPEC", r"(?P<PARAMS_OPT_SPEC>--params(?=\s|=|--|$))"),
97-
("PARAMS_OPT_EQ", r"(?P<PARAMS_OPT_EQ>=)"),
98-
(
99-
"GOTO_PARSE_NON_PARAMS_OPTIONS",
100-
r"(?P<GOTO_PARSE_NON_PARAMS_OPTIONS>(?=--\w+))", # found another option spec
101-
),
102-
("PY_BOOL", r"(?P<PY_BOOL>True|False)"),
103-
("DOLLAR_PY_ID", r"(?P<DOLLAR_PY_ID>\$[^\d\W]\w*)"),
104-
(
105-
"PY_NUMBER",
106-
r"(?P<PY_NUMBER>-?[1-9]\d*(?:\.\d+)?(:?[e|E][+-]?\d+)?)",
107-
),
108-
("SQUOTE", r"(?P<SQUOTE>')"),
109-
("DQUOTE", r'(?P<DQUOTE>")'),
110-
("COLON", r"(?P<COLON>:)"),
111-
("COMMA", r"(?P<COMMA>,)"),
112-
("LCURL", r"(?P<LCURL>\{)"),
113-
("RCURL", r"(?P<RCURL>})"),
114-
("LSQUARE", r"(?P<LSQUARE>\[)"),
115-
("RSQUARE", r"(?P<RSQUARE>])"),
116-
("LPAREN", r"(?P<LPAREN>\()"),
117-
("RPAREN", r"(?P<RPAREN>\))"),
118-
]
119-
),
120-
),
121-
(
122-
"common",
123-
OrderedDict(
124-
[
125-
("WS", r"(?P<WS>\s+)"),
126-
("EOL", r"(?P<EOL>$)"),
127-
(
128-
# anything not a whitespace or matched by something else
129-
"UNKNOWN",
130-
r"(?P<UNKNOWN>\S+)",
131-
),
132-
]
133-
),
55+
state_parse_pos_args=OrderedDict(
56+
GOTO_PARSE_NON_PARAMS_OPTIONS=r"(?P<GOTO_PARSE_NON_PARAMS_OPTIONS>(?=--))", # double dash - starting the options list
57+
DEST_VAR=r"(?P<DEST_VAR>[^\d\W]\w*)", # essentially a Python ID
58+
),
59+
state_parse_non_params_options=OrderedDict(
60+
GOTO_PARSE_PARAMS_OPTION=r"(?P<GOTO_PARSE_PARAMS_OPTION>(?=--params(?:\s|=|--|$)))", # the --params option
61+
OPTION_SPEC=r"(?P<OPTION_SPEC>--\w+)",
62+
OPTION_EQ=r"(?P<OPTION_EQ>=)",
63+
OPT_VAL=r"(?P<OPT_VAL>\S+?(?=\s|--|$))",
64+
),
65+
state_parse_params_option=OrderedDict(
66+
PY_STRING=r"(?P<PY_STRING>(?:{})|(?:{}))".format( # single and double quoted strings
67+
r"'(?:[^'\\]|\.)*'", r'"(?:[^"\\]|\.)*"'
13468
),
135-
]
69+
PARAMS_OPT_SPEC=r"(?P<PARAMS_OPT_SPEC>--params(?=\s|=|--|$))",
70+
PARAMS_OPT_EQ=r"(?P<PARAMS_OPT_EQ>=)",
71+
GOTO_PARSE_NON_PARAMS_OPTIONS=r"(?P<GOTO_PARSE_NON_PARAMS_OPTIONS>(?=--\w+))", # found another option spec
72+
PY_BOOL=r"(?P<PY_BOOL>True|False)",
73+
DOLLAR_PY_ID=r"(?P<DOLLAR_PY_ID>\$[^\d\W]\w*)",
74+
PY_NUMBER=r"(?P<PY_NUMBER>-?[1-9]\d*(?:\.\d+)?(:?[e|E][+-]?\d+)?)",
75+
SQUOTE=r"(?P<SQUOTE>')",
76+
DQUOTE=r'(?P<DQUOTE>")',
77+
COLON=r"(?P<COLON>:)",
78+
COMMA=r"(?P<COMMA>,)",
79+
LCURL=r"(?P<LCURL>\{)",
80+
RCURL=r"(?P<RCURL>})",
81+
LSQUARE=r"(?P<LSQUARE>\[)",
82+
RSQUARE=r"(?P<RSQUARE>])",
83+
LPAREN=r"(?P<LPAREN>\()",
84+
RPAREN=r"(?P<RPAREN>\))",
85+
),
86+
common=OrderedDict(
87+
WS=r"(?P<WS>\s+)",
88+
EOL=r"(?P<EOL>$)",
89+
UNKNOWN=r"(?P<UNKNOWN>\S+)", # anything not a whitespace or matched by something else
90+
),
13691
)
13792

13893

0 commit comments

Comments
 (0)