-
Notifications
You must be signed in to change notification settings - Fork 27
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
Minor cleanups of some python antipatterns #1386
Conversation
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 know this is still in draft so maybe you have already addressed some of these comments; but I would just like to give some feedback after the workshop session 😄
Biggest remark: you can not remove the initial zero in range
when used with three arguments.
cognite/client/_api/assets.py
Outdated
chunk_size = 100 | ||
for i in range(0, len(ids), chunk_size): | ||
tasks.append({"parent_ids": ids[i : i + chunk_size], "limit": -1}) | ||
tasks = [{"parent_ids": ids[i : i + chunk_size], "limit": -1} for i in range(len(ids), chunk_size)] |
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.
Please instead use split_into_chunks
from utils.
@@ -277,12 +276,12 @@ def test_update_multiple_features(self, cognite_client, allow_crs_transformation | |||
feature_type_external_id=test_feature_type.external_id, | |||
feature=[ | |||
Feature(external_id=test_features[idx].external_id, temperature=6.237, pressure=12.21, volume=34.43) | |||
for idx in range(0, len(test_features)) | |||
for idx in range(len(test_features)) |
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.
Look at how can you remove range(len
], | ||
allow_crs_transformation=allow_crs_transformation, | ||
chunk_size=2, | ||
) | ||
for idx in range(0, len(test_features)): | ||
for idx in range(len(test_features)): |
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.
Same here, try to remove the range-len-construct
len_new_view = 1000 | ||
events = [ | ||
Event( | ||
external_id=f"test_evt_templates_1_{i}", | ||
type="test_templates_1", | ||
start_time=i * len_new_view, | ||
) | ||
for i in range(len_new_view + 1) | ||
] |
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.
len_new_view = 1000 | |
events = [ | |
Event( | |
external_id=f"test_evt_templates_1_{i}", | |
type="test_templates_1", | |
start_time=i * len_new_view, | |
) | |
for i in range(len_new_view + 1) | |
] | |
events = [ | |
Event(external_id=f"test_evt_templates_1_{i}", type="test_templates_1", start_time=i * 1000) | |
for i in range(1001) | |
] |
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.
This won't work because now len_new_view
is undefined.
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 promise I don't just give variable names to all random variables/arguments, only when they are repeated. 😇
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.
Just use a literal, that was my point 😄
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.
Yeah but with mine you only have one place to update it in case it needs to be changed.
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.
But they are not the same variable! First says do 1000 loops, the other says change start time to be seconds, not ms
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.
Ah whoops! Misread it then. Good thing I already made the change though.
tasks = [] | ||
for dps_object_list in dps_object_lists: | ||
tasks.append((dps_object_list,)) | ||
tasks = [(dps_object_list,) for dps_object_list in dps_object_lists] |
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.
Looks like list(zip(dps_object_lists))
would work 😜 Not that I think you should change it.
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 could go for either or, honestly. The reader knows that these are a kind of "type/variable/data massaging". 🤷
for exp in expressions_to_iterate: | ||
expression, short_expression = self._build_expression(exp, variables, aggregate, granularity) |
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.
Nice, this is exactly what I had in mind
Codecov Report
@@ Coverage Diff @@
## master #1386 +/- ##
==========================================
- Coverage 90.65% 90.48% -0.18%
==========================================
Files 117 117
Lines 14261 14255 -6
==========================================
- Hits 12929 12898 -31
- Misses 1332 1357 +25
|
Btw, please update the PR title 😅 |
Aww... 😢 😛 |
events = [] | ||
for i in range(0, 1001): | ||
events.append(Event(external_id="test_evt_templates_1_" + str(i), type="test_templates_1", start_time=i * 1000)) | ||
events = [ |
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.
One more idea, make this session scoped to just run once
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.
One last comment (unrelated to your changes, just made me aware of a bad fixture)
fixed string formatting Co-authored-by: Håkon V. Treider <[email protected]>
Description
Take on the range/len cleanup described here. Took the liberty of cleaning up a few other things like some f-strings, list comprehensions, etc.
Checklist:
If a new method has been added it should be referenced in cognite.rst in order to generate docs based on its docstring.