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

[CQT-289] Avoid creating an Axis (0, 0, 0) #406

Merged
merged 8 commits into from
Jan 16, 2025

Conversation

GuyPuts
Copy link
Contributor

@GuyPuts GuyPuts commented Dec 11, 2024

Avoids that an axis of (0, 0, 0) can be created and allows the user to check for this instance before instantiating the class.

@rturrado rturrado changed the title Cqt 289 avoid creating an axis 0 0 0 [CQT-289] Avoid creating an Axis (0, 0, 0) Dec 11, 2024
Copy link
Contributor

@rturrado rturrado left a comment

Choose a reason for hiding this comment

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

OK, let's have a look at the Axis class globally, and try to refine the logic a bit while not duplicating code.

We have already this function _parse_and_validate_axislike that parses and normalizes:

  1. I would divide it into two functions: one for parsing and the other one for normalizing, e.g., parse and normalize (this latter already exists). Can they both be static methods?
  2. I would add the check for !(0, 0, 0) to that parse method.

Now, if we go back to the constructor, can we do?

axis_to_parse = axis[0] if axis is a list, otherwise axis
self._value = Axis.parse(axis_to_parse)
self._value = Axis.normalize(self._value)

We can write 3 parametrized tests for each function: constructor, Axis.parse, and Axis.normalize.

Let me know how you see it!

opensquirrel/ir.py Outdated Show resolved Hide resolved
@GuyPuts
Copy link
Contributor Author

GuyPuts commented Dec 13, 2024

OK, let's have a look at the Axis class globally, and try to refine the logic a bit while not duplicating code.

We have already this function _parse_and_validate_axislike that parses and normalizes:

  1. I would divide it into two functions: one for parsing and the other one for normalizing, e.g., parse and normalize (this latter already exists). Can they both be static methods?
  2. I would add the check for !(0, 0, 0) to that parse method.

Now, if we go back to the constructor, can we do?

axis_to_parse = axis[0] if axis is a list, otherwise axis
self._value = Axis.parse(axis_to_parse)
self._value = Axis.normalize(self._value)

We can write 3 parametrized tests for each function: constructor, Axis.parse, and Axis.normalize.

Let me know how you see it!

@rturrado if we split the functions, parse "should" return the axis object. Shall we return false if the Axis is invalid?

@rturrado
Copy link
Contributor

@rturrado if we split the functions, parse "should" return the axis object. Shall we return false if the Axis is invalid?

We should raise a ValueError.

@GuyPuts
Copy link
Contributor Author

GuyPuts commented Dec 18, 2024

@rturrado if we split the functions, parse "should" return the axis object. Shall we return false if the Axis is invalid?

We should raise a ValueError.

I agree that usually a ValueError would be best practice. However, the ticket states that we want to create an Axis without a try/catch block:
image

So I believe that in this case, maybe it is better to return False?

@rturrado
Copy link
Contributor

No, Guy, let's try to raise the exception. I wrote that ticket. As you can see, I was valuing options, wondering, not stating anything.

@GuyPuts
Copy link
Contributor Author

GuyPuts commented Dec 18, 2024

No, Guy, let's try to raise the exception. I wrote that ticket. As you can see, I was valuing options, wondering, not stating anything.

Ah alright! I'll raise the exception :)

Copy link
Contributor

@rturrado rturrado left a comment

Choose a reason for hiding this comment

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

Good work, Guy.

I've added a few comments.

opensquirrel/ir.py Outdated Show resolved Hide resolved
opensquirrel/ir.py Outdated Show resolved Hide resolved
opensquirrel/ir.py Outdated Show resolved Hide resolved
test/test_ir.py Outdated Show resolved Hide resolved
test/test_ir.py Show resolved Hide resolved
opensquirrel/ir.py Outdated Show resolved Hide resolved
@GuyPuts GuyPuts merged commit 4a98e09 into develop Jan 16, 2025
15 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.

2 participants