-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google Analytics runner - iterate over keys the Python 3 way #4538
Conversation
@@ -164,7 +164,7 @@ def run_query(self, query, user): | |||
params = json_loads(query) | |||
except: | |||
params = parse_qs(urlparse(query).query, keep_blank_values=True) | |||
for key in params.keys(): | |||
for key in [*params]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is the issue:
>>> d = {'ab': 'yesterday', 'cd': 2}
>>> for k in d.keys():
... print(k)
...
ab
cd
(this shows that the keys are iterated properly)
But, check the following:
>>> ",".join("yesterday")
'y,e,s,t,e,r,d,a,y'
So the issue is actually line 168 which applies ",".join
on any value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The real problem is line 170, which messes around with the dict_keys
iterator, something that didn't happen in Python 2. Populating a new list of keys with [*params]
solves this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I understood the real problem and the solution. To help people like myself I've added a few comments as suggested commits.
@@ -164,7 +164,7 @@ def run_query(self, query, user): | |||
params = json_loads(query) | |||
except: | |||
params = parse_qs(urlparse(query).query, keep_blank_values=True) | |||
for key in params.keys(): | |||
for key in [*params]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for key in [*params]: | |
# Because we change keys list on line 170, we need to create a copy of the keys | |
for key in [*params]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option that would make fiddling with iterators (and the need to explain it) redundant is:
query_string = parse_qs(urlparse(query).query, keep_blank_values=True)
params = {k.replace('-', '_'): ",".join(v) for k,v in query_string.items() }
We'll still have to explain ",".join
, but maybe we don't have to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this 👍
@@ -164,7 +164,7 @@ def run_query(self, query, user): | |||
params = json_loads(query) | |||
except: | |||
params = parse_qs(urlparse(query).query, keep_blank_values=True) | |||
for key in params.keys(): | |||
for key in [*params]: | |||
params[key] = ",".join(params[key]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
params[key] = ",".join(params[key]) | |
# parse_qs returns every value as a list | |
params[key] = ",".join(params[key]) |
What type of PR is this? (check all applicable)
Description
Following #4181, Google Analytics queries were broken since dict
.keys()
returnsdict_keys
, which would in turn yield wrong results when iterating over param values, resulting in errors such asRelated Tickets & Documents
#4181