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

Moved compile at the end and modified its content. #2113

Merged
merged 4 commits into from Mar 15, 2023

Conversation

tamakoshi2001
Copy link
Contributor

Checklist
Thank you for contributing to QuTiP! Please make sure you have finished the following tasks before opening the PR.

Description
I moved 'compile' at the end in qutip.settings and shortened it to only show the non-default values.

Related issues or PRs
#2100

@coveralls
Copy link

coveralls commented Mar 13, 2023

Coverage Status

Coverage: 75.414% (-0.0001%) from 75.414% when pulling 639d7ae on tamakoshi2001:improve_qutip.settings into 8cc88af on qutip:master.

Copy link
Member

@Ericgig Ericgig left a comment

Choose a reason for hiding this comment

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

Thank you, the settings print looks good.

However, it makes it impossible to see the default values when trying to print the compilations options and core options directly.
We can add extra input to __repr__ if thye have default value, so we can have settings call compile's __repr__ with a flag to shorten it while leaving the default call printing everything.

@@ -34,7 +34,8 @@ def __setitem__(self, key, value):
def __repr__(self):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def __repr__(self):
def __repr__(self, full=True):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ericgig Thanks for your advice! I added 'full option'. When full=True, print(settings.compile.__repr__(full=True)) outputs everything. When full=False, it only outputs modified settings.

Copy link
Member

Choose a reason for hiding this comment

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

Yes so now, print(settings.compile) will print the full output as expected. We still want to have print(settings) print the shorten version.

Also if you can, when no option is printed in the short version, it could print something like:

    ...
    colorblind_safe: False
    compile: <CompilationOptions()>

instead of

    ...
    colorblind_safe: False
    compile: <CompilationOptions(
)>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi. I included your suggestions! You can try them;

>>> import qutip.settings as settings
>>> print(settings)
>>> print(settings.compile)
>>> settings.compile.__setitem__('accept_int',True)
>>> print(settings)

Copy link
Member

@Ericgig Ericgig left a comment

Choose a reason for hiding this comment

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

This is a good improvement, Thank you.

I am fine with merging the code as is, but left some comment on how to polish it some more.

out += [")>"]
return "\n".join(out)
if cnt:
Copy link
Member

Choose a reason for hiding this comment

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

cnt is equal to len(out) - 2 and could be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I removed cnt.

if not attr.startswith('_'):
if attr != "core" and attr != "compile":
lines.append(f" {attr}: {self.__getattribute__(attr)}")
if "compile" in self.__dir__():
Copy link
Member

Choose a reason for hiding this comment

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

The normal way to test if an attribute exist would be hasattr(self, "compile").
But compile is added in __init__ as None. So a better check would be if self.compile is not None. But it would only happen if for some strange reason, someone imported this file without importing qutip, so I would be fine with removing the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed if

lines.append(f" {attr}: {self.__getattribute__(attr)}")
if "compile" in self.__dir__():
attr = "compile"
content = self.__getattribute__('compile').__repr__(full=False)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
content = self.__getattribute__('compile').__repr__(full=False)
content = self.compile.__repr__(full=False)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you. This is pretty nice!

Comment on lines 284 to 285
if not attr.startswith('_'):
if attr != "core" and attr != "compile":
Copy link
Member

Choose a reason for hiding this comment

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

Stacking if is not great.
We could simplify it by creating variables:

is_public_attr = not attr.startswith('_')
is_special_case = attr == "core" or attr == "compile"
if is_public_attr and not is_special_case:

or shorten the expression:

if not attr.startswith('_') and attr not in ["core", "compile"]:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I chose the short expression. I removed all unnecessary variables from codes.

Copy link
Member

@Ericgig Ericgig left a comment

Choose a reason for hiding this comment

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

Look great, thank you!

@Ericgig
Copy link
Member

Ericgig commented Mar 15, 2023

Not sure why, by there is a conflict stopping me from merging...
Could you fix it.

@tamakoshi2001
Copy link
Contributor Author

@Ericgig I fixed the conflict now.

@tamakoshi2001
Copy link
Contributor Author

@Ericgig I found that def __repr__(self, full=True): is deleted somehow. I will fix this issue soon.

@tamakoshi2001
Copy link
Contributor Author

@Ericgig I fixed the conflict and a related problem. Could you see if everything is ok and merge?

@Ericgig Ericgig merged commit fd89a25 into qutip:master Mar 15, 2023
11 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.

None yet

3 participants