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

Use __str__ to replaced print_hierarchy #1314

Merged
merged 7 commits into from
May 7, 2024
Merged

Conversation

Licini
Copy link
Contributor

@Licini Licini commented Mar 28, 2024

According to @chenkasirer there are some discussion about remove all print in core library, instead use __str__ for this type of functionalities.
BlockResearchGroup/compas_model#31 (comment)

I made this change for Tree, let me know what you guys think.

What type of change is this?

  • Bug fix in a backwards-compatible manner.
  • New feature in a backwards-compatible manner.
  • Breaking change: bug fix or new feature that involve incompatible API changes.
  • Other (e.g. doc update, configuration, etc)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I added a line to the CHANGELOG.md file in the Unreleased section under the most fitting heading (e.g. Added, Changed, Removed).
  • I ran all tests on my computer and it's all green (i.e. invoke test).
  • I ran lint on my computer and there are no errors (i.e. invoke lint).
  • I added new functions/classes and made them available on a second-level import, e.g. compas.datastructures.Mesh.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate)

@jf---
Copy link
Contributor

jf--- commented Apr 8, 2024

this looks great, but curious what's @tomvanmele @Licini take on str vs repr: could repr just invoke str?

@tomvanmele
Copy link
Member

the point of __repr__ is to allow for (re)construction of an object using eval of its string "repr"esentation. in the case of making something print-friendly we should use __str__ in my opinion. if i remember correctly, we only implement __repr__ if reconstruction is indeed possible...

@jf---
Copy link
Contributor

jf--- commented Apr 8, 2024

right, repr(oduction) its a neat feature, so repr can echo str when reconstruction isn't viable

Copy link
Member

@gonzalocasas gonzalocasas left a comment

Choose a reason for hiding this comment

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

lgtm! only one small q in comments


if max_depth is not None and depth > max_depth:
return

connector = "└── " if last else "├── "
print("{}{}{}".format(prefix, connector, node))
hierarchy["string"] += "{}{}{}\n".format(prefix, connector, node)
Copy link
Member

Choose a reason for hiding this comment

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

why do you pass around a dictionary with a single, hard-coded key?

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 was to make the string a mutable object (like a pointer). In python 3 i can simply use nonlocal and just say hierarchy_string, but that option is not available for 2.7. Maybe now looks better with the list?

@chenkasirer
Copy link
Member

@Licini sorry to ping, I'd just like to merge BlockResearchGroup/compas_model#31 soon

@tomvanmele
Copy link
Member

@Licini would indeed be good to finish this...

@Licini
Copy link
Contributor Author

Licini commented Apr 29, 2024

@chenkasirer @gonzalocasas Sorry for late update on this folks. But now I can declare this is done 😂. If you guys give me a go I will merge now

Comment on lines 452 to 453
str
The spatial hierarchy of the tree.
Copy link
Member

Choose a reason for hiding this comment

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

fwiw, i don't agree with this. why would anyone expect Tree.hierarchy to return a string?

Copy link
Member

Choose a reason for hiding this comment

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

if the only purpose of this method is to be a helper for __str__, i think it should be named differently and made private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about something more expressive like get_hierarchy_string ? Tho I'm more on the side for still keeping it public because I call this function a lot with different max_depth for very deep trees, I can imagine people might also want to do something like this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tomvanmele any comments on this? would like to resolve it

@Licini Licini merged commit 835f233 into compas-dev:main May 7, 2024
14 checks passed
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.

5 participants