Skip to content

Simplify BOM code#197

Merged
17o2 merged 30 commits intowireviz:devfrom
kvid:simplify-BOM-code
Aug 22, 2021
Merged

Simplify BOM code#197
17o2 merged 30 commits intowireviz:devfrom
kvid:simplify-BOM-code

Conversation

@kvid
Copy link
Collaborator

@kvid kvid commented Nov 16, 2020

Currently, this PR does not change any output generated by build_examples.py. Consider implementing the TODO about using 'Description' in BOM heading. However, if this TODO is considered controversial or otherwise might delay merging the PR, then please ignore it for now.

@17o2
Copy link
Collaborator

17o2 commented Nov 16, 2020

Nice!
Does it make sense to rebase this onto your length_unit2 branch and include any changes necessary for it to work with the new length_unit attribute? (I can't thoroughly test right now, maybe my suggestion does not make sense, and your simplifications eliminate the need for this)

I will merge #196 tonight (squashing the commits, since the overall amount of lines changed is rather small, and adding everyone as a co-author). Then this PR could seamlessly follow afterwards :)

Let me know what you think.

@17o2 17o2 added this to the v0.3 milestone Nov 16, 2020
@kvid kvid marked this pull request as draft November 16, 2020 22:37
@kvid kvid marked this pull request as draft November 16, 2020 22:37
@kvid kvid force-pushed the simplify-BOM-code branch from 0ebf547 to e04058c Compare November 16, 2020 22:41
@kvid kvid force-pushed the simplify-BOM-code branch from 233c497 to 9cdc9df Compare December 30, 2020 11:11
@kvid kvid marked this pull request as ready for review December 30, 2020 11:29
@kvid kvid force-pushed the simplify-BOM-code branch from 9cdc9df to d15eeb1 Compare January 3, 2021 05:20
@kvid kvid force-pushed the simplify-BOM-code branch from 6a0c39c to 183a943 Compare February 20, 2021 20:34
@kvid kvid mentioned this pull request Mar 21, 2021
@17o2
Copy link
Collaborator

17o2 commented Mar 21, 2021

I haven't merged this PR since I have seen some more activity on it since you removed the draft status.

If, as per your comment, you think it is ready, I can have a look and integrate it soon :)

@kvid
Copy link
Collaborator Author

kvid commented Mar 21, 2021

I haven't merged this PR since I have seen some more activity on it since you removed the draft status.

That's true. I've added a few commits the last months, but I guess it might be time for a review so other PRs can benefit from these changes soon (if they are accepted).

If, as per your comment, you think it is ready, I can have a look and integrate it soon :)

The currently last commit about "Group common function arguments into a dict" might not look like a simplification, but my goal is to simplify later attribute extensions, like e.g. one of the commits in #219. The same goal is also my motivation behind the preceeding commit in this PR about optional_fields().

kvid added 14 commits April 5, 2021 18:56
- Use the actual BOM as first parameter instead of the whole harness.
- Use a whole AdditionalComponent as second parameter instead of each
  attribute separately.
Move the lambda declaration out of the function scope for common
access from two different functions.
Assign input designators once to a temporary variable for easy reusage.
- Use one common entry loop to consume iterator only once.
- Use same key function for sort() and groupby(),
  except replace None with empty string when sorting.
Seems to be the most popular search alternative:
 https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search

Raising StopIteration if not found is better than returning None
to detect such an internal error more easily.
Make a list from the group iterator for reusage in sum expressions
and to pick first group entry. The expected group sizes are very small,
so performance loss by creating a temporary list should be neglectable.

Alternativly, itertools.tee(group, 3) could be called to triplicate
the iterator, but it was not chosen for readability reasons.
kvid added 10 commits April 5, 2021 18:56
This type alias describes the possible types of keys and values in
the dict representing a BOM entry.
Build output string in component_table_entry() as the similar strings
in generate_bom(). Repeating a couple of minor if-expressions is small
cost to obtain a more compact and readable main expression.
This way, both BOM and harness.additional_bom_items uses the same
set of keys in their dict entries. This was originally suggested
in a wireviz#115 review, but had too many issues to be implemented then.
This reverts commit 96d393d.
However, raising an exception if failing the BOM index search is still
wanted, so a custom exception is raised instead of returning None.
@kvid kvid force-pushed the simplify-BOM-code branch from 4fd2276 to 514515f Compare April 5, 2021 17:01
@kvid kvid requested a review from 17o2 April 5, 2021 17:42
@kvid kvid force-pushed the simplify-BOM-code branch from 514515f to f1cb7e2 Compare April 6, 2021 18:40
@kvid kvid force-pushed the simplify-BOM-code branch from 40c19fd to 8de8a69 Compare May 3, 2021 02:49
Co-authored-by: kvid <kvid@users.noreply.github.com>
Copy link
Collaborator

@17o2 17o2 left a comment

Choose a reason for hiding this comment

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

I've tested the output of this PR and it looks good.
Admittedly, I cannot currently dive deep into every single line of the code, but overall it looks like a good simplification, and I don't want to keep this open much longer than it has to. Anyways, small tweaks and fixes are always possible later.

I will squash-merge to keep the dev branch simple. The detailed history will be preserved in the PR branch for anyone to check.

@17o2 17o2 merged commit 31d55ff into wireviz:dev Aug 22, 2021
17o2 added a commit that referenced this pull request Aug 22, 2021
17o2 added a commit that referenced this pull request Aug 23, 2021
lxander42 pushed a commit to lxander42/PipeViz that referenced this pull request Sep 20, 2025
* Skip assignment and return expression directly

* Simplify get_bom_index() parameters

- Use the actual BOM as first parameter instead of the whole harness.
- Use a whole AdditionalComponent as second parameter instead of each
  attribute separately.

* Use the same lambda in get_bom_index() as for deduplicating BOM

Move the lambda declaration out of the function scope for common
access from two different functions.

* Convert dataclass object to dict to use the same lambda

* Redefine the common lambda to an ordinary function

* Simplify BOM header row logic

* Simplify collecting designators for a joined BOM entry

Assign input designators once to a temporary variable for easy reusage.

* Simplify deduplication and sorting of collected designators

* Remove parentheses around return expressions

https://stackoverflow.com/questions/4978567/should-a-return-statement-have-parentheses

* Move out code from inner loop into helper functions

* Move BOM sorting above grouping to use groupby()

- Use one common entry loop to consume iterator only once.
- Use same key function for sort() and groupby(),
  except replace None with empty string when sorting.

* Make the BOM grouping function return string tuple for sorting

* Use a generator expressions and raise exception if failing

Seems to be the most popular search alternative:
 https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search

Raising StopIteration if not found is better than returning None
to detect such an internal error more easily.

* Replace accumulation loop with sum expressions

Make a list from the group iterator for reusage in sum expressions
and to pick first group entry. The expected group sizes are very small,
so performance loss by creating a temporary list should be neglectable.

Alternativly, itertools.tee(group, 3) could be called to triplicate
the iterator, but it was not chosen for readability reasons.

* Add function type hints and doc strings

* Add BOMEntry type alias

This type alias describes the possible types of keys and values in
the dict representing a BOM entry.

* Rename extra variable to part for consistency

* Build output string in one big expression

Build output string in component_table_entry() as the similar strings
in generate_bom(). Repeating a couple of minor if-expressions is small
cost to obtain a more compact and readable main expression.

* Move default qty value=1 to BOM deduplication

* Eliminate local variable

* Rename the 'item' key to 'description' in all BOMEntry dicts

This way, both BOM and harness.additional_bom_items uses the same
set of keys in their dict entries. This was originally suggested
in a wireviz#115 review, but had too many issues to be implemented then.

* Move repeated code into new optional_fields() function

* Group common function arguments into a dict

* Revert "Use a generator expressions and raise exception if failing"

This reverts commit 96d393d.
However, raising an exception if failing the BOM index search is still
wanted, so a custom exception is raised instead of returning None.

* Use new BOMKey type alias for get_bom_index() target argument

Replace the get_bom_index() part argument with the target key argument
to prepare for quering any BOM entry that matches the target key.

* Cache the BOM entry key in the entry itself

* Rename bom_types_group() to bom_entry_key()

* Define tuples of BOM columns as common constants

* Clarify a comment

* Change BOM heading from `Item` to `Description`

Co-authored-by: kvid <kvid@users.noreply.github.com>

Co-authored-by: Daniel Rojas <github@danielrojas.net>
lxander42 pushed a commit to lxander42/PipeViz that referenced this pull request Sep 20, 2025
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