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

fix for issue 3053. Added check to determine if variable meta is a dict #3054

Merged
merged 5 commits into from
Nov 2, 2022
Merged

Conversation

ntenney
Copy link
Contributor

@ntenney ntenney commented Oct 20, 2022

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #3053

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

This code has been run against databases containing jsonb type and strings for metadata. In addition, I used this code against a local list that simulated the results coming from the database, and also tested it against a situation that should never arise in the wild (strings mixed with objects in the metadata result column). This was tested using the 8.2 deploy of campus.

@ntenney ntenney requested a review from craig8 October 20, 2022 03:13
@craig8 craig8 changed the base branch from main to develop October 20, 2022 17:08
@@ -350,7 +350,7 @@ def get_topic_meta_map(self):
'SELECT topic_id, metadata '
'FROM {}').format(Identifier(self.meta_table))
rows = self.select(query)
meta_map = {tid: jsonapi.loads(meta) if meta else None for tid, meta in rows}
meta_map = {tid: meta if meta and isinstance(meta, dict) else jsonapi.loads(meta) if meta else None for tid, meta in rows}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just for my understanding, is this nested ternary expression one-liner equivalent to the following for-loop?

meta_map = {}
for tid, meta in rows:
   if meta:
       if meta and isinstance(meta, dict):
           meta_map[tid] = meta
       else:
           meta_map[tid] = jsonapi.loads(meta) 
   else:
       meta_map[tid] = None

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

Copy link

Choose a reason for hiding this comment

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

Just curious. Is obfuscation best practice for this project?

Copy link
Contributor

Choose a reason for hiding this comment

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

@awoz No it's not

I think nested if looping this way is harder to read as well. The original meta_map was the right level of complexity, but I did have a hard time following the new code.

@ntenney can you replace the code with something more user friendly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@awoz No it's not

I think nested if looping this way is harder to read as well. The original meta_map was the right level of complexity, but I did have a hard time following the new code.

@ntenney can you replace the code with something more user friendly.

@ntenney If it helps, feel free to copy and/or modify the for loop in my initial comment above ^^

Copy link
Contributor Author

@ntenney ntenney Oct 28, 2022

Choose a reason for hiding this comment

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

I'm not a huge fan of splitting something that takes 1 line into 9. Would this be more palatable?

meta_map = {tid: meta if meta and isinstance(meta,dict) else (json.loads(meta) if meta else None) for tid, meta in rows}

or

meta_map = {tid: meta if (meta and isinstance(meta,dict)) else (json.loads(meta) if meta else None) for tid, meta in rows}

Copy link
Contributor

Choose a reason for hiding this comment

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

@ntenney splitting the long line or adding the parenthesis doesn't make it any clearer for me. I do get what the code is doing but I agree that the broken down for loop was much easier to read. I might have voted for the compact format if there was just a single if inside but the second if inside for loop inside the {} really pushes it over the edge for me

Copy link

Choose a reason for hiding this comment

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

It is my general sense that all source code should be written "for other" developers who may not be familiar with your code. For them to be successful in their roles, source code hints in the form of comments or clean format are essential. This principle allows for an efficient coding, debug and review cycle, with a timely delivery of solutions. I'm pretty sure that understanding and debugging / single stepping a one-liner is not very palatable.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is my general sense that all source code should be written "for other" developers who may not be familiar with your code. For them to be successful in their roles, source code hints in the form of comments or clean format are essential. This principle allows for an efficient coding, debug and review cycle, with a timely delivery of solutions. I'm pretty sure that understanding and debugging / single stepping a one-liner is not very palatable.

@awoz Agree. That's why I made the initial comment because I had a hard time understanding and visualizing the control flow of the dictionary comprehension, especially since the comprehension requires one to read it from right to left as opposed to the conventional (i.e. Western) way of reading from left to right.

I drafted up the equivalent for loop to both confirm that the control flow of the dictionary comprehension was the intended flow; and to elicit feedback from others on best practices. Thank you for your feedback.

@craig8
Copy link
Contributor

craig8 commented Nov 2, 2022

@ntenney Thank you!

@craig8 craig8 merged commit 7880c9a into VOLTTRON:develop Nov 2, 2022
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.

6 participants