Skip to content
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

Merged
merged 11 commits into from
Oct 5, 2023
Merged

Minor cleanups of some python antipatterns #1386

merged 11 commits into from
Oct 5, 2023

Conversation

HaydenCognite
Copy link
Contributor

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:

  • Tests added/updated.
  • Documentation updated. Documentation is generated from docstrings - these must be updated according to your change.
    If a new method has been added it should be referenced in cognite.rst in order to generate docs based on its docstring.
  • Changelog updated in CHANGELOG.md.
  • Version bumped. If triggering a new release is desired, bump the version number in _version.py and pyproject.toml per semantic versioning.

Copy link
Contributor

@haakonvt haakonvt left a 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.

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)]
Copy link
Contributor

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.

cognite/client/_api/datapoints.py Outdated Show resolved Hide resolved
cognite/client/_api/datapoints.py Show resolved Hide resolved
tests/tests_unit/test_api/test_sequences.py Outdated Show resolved Hide resolved
tests/tests_unit/test_api/test_sequences.py Outdated Show resolved Hide resolved
@@ -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))
Copy link
Contributor

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)):
Copy link
Contributor

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

Comment on lines 80 to 88
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)
]
Copy link
Contributor

@haakonvt haakonvt Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
]

Copy link
Contributor Author

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.

Copy link
Contributor Author

@HaydenCognite HaydenCognite Oct 4, 2023

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. 😇

Copy link
Contributor

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 😄

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor Author

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.

@HaydenCognite HaydenCognite marked this pull request as ready for review October 4, 2023 07:07
@HaydenCognite HaydenCognite requested review from a team as code owners October 4, 2023 07:07
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]
Copy link
Contributor

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.

Copy link
Contributor Author

@HaydenCognite HaydenCognite Oct 4, 2023

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". 🤷

Comment on lines +77 to +78
for exp in expressions_to_iterate:
expression, short_expression = self._build_expression(exp, variables, aggregate, granularity)
Copy link
Contributor

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
Copy link

codecov bot commented Oct 4, 2023

Codecov Report

Merging #1386 (1ef99b6) into master (44f11db) will decrease coverage by 0.18%.
The diff coverage is 100.00%.

@@            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     
Files Coverage Δ
cognite/client/_api/assets.py 95.16% <100.00%> (-0.07%) ⬇️
cognite/client/_api/datapoints.py 96.28% <100.00%> (-0.02%) ⬇️
cognite/client/_api/synthetic_time_series.py 96.05% <100.00%> (ø)

... and 7 files with indirect coverage changes

@haakonvt
Copy link
Contributor

haakonvt commented Oct 4, 2023

Btw, please update the PR title 😅

@HaydenCognite HaydenCognite changed the title Doge26 Minor cleanups of some python antipatterns Oct 4, 2023
@HaydenCognite
Copy link
Contributor Author

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 = [
Copy link
Contributor

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

Copy link
Contributor

@haakonvt haakonvt left a 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)

@haakonvt haakonvt enabled auto-merge (squash) October 5, 2023 18:31
@haakonvt haakonvt merged commit 4cd59a1 into master Oct 5, 2023
7 checks passed
@haakonvt haakonvt deleted the doge26 branch October 5, 2023 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants