-
Notifications
You must be signed in to change notification settings - Fork 108
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
Make the tolerance class a singleton #1348
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,9 @@ class Tolerance(Data): | |
This value is called the "true value". | ||
By convention, the second value is considered the "true value" by the comparison functions of this class. | ||
|
||
The :class:`compas.tolerance.Tolerance` class is implemented using a "singleton" pattern and can therefore have only 1 (one) instance per context. | ||
Usage of :attr:`compas.tolerance.TOL` outside of :mod:`compas` internals is therefore deprecated. | ||
|
||
Examples | ||
-------- | ||
>>> tol = Tolerance() | ||
|
@@ -67,6 +70,9 @@ class Tolerance(Data): | |
|
||
""" | ||
|
||
_instance = None | ||
_is_inited = False | ||
|
||
ABSOLUTE = 1e-9 | ||
"""float: Determines when a number is small enough to be considered zero. | ||
""" | ||
|
@@ -108,8 +114,9 @@ class Tolerance(Data): | |
""" | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if not hasattr(cls, "_instance"): | ||
cls._instance = super(Tolerance, cls).__new__(cls) | ||
if not isinstance(cls._instance, cls): | ||
cls._instance = object.__new__(cls, *args, **kwargs) | ||
cls._inited = False | ||
return cls._instance | ||
|
||
@property | ||
|
@@ -151,22 +158,34 @@ def __init__( | |
name=None, | ||
): | ||
super(Tolerance, self).__init__(name=name) | ||
self._unit = None | ||
self._absolute = None | ||
self._relative = None | ||
self._angular = None | ||
self._approximation = None | ||
self._precision = None | ||
self._lineardeflection = None | ||
self._angulardeflection = None | ||
self.unit = unit | ||
self.absolute = absolute | ||
self.relative = relative | ||
self.angular = angular | ||
self.approximation = approximation | ||
self.precision = precision | ||
self.lineardeflection = lineardflection | ||
self.angulardeflection = angulardflection | ||
if not self._is_inited: | ||
self._unit = None | ||
self._absolute = None | ||
self._relative = None | ||
self._angular = None | ||
self._approximation = None | ||
self._precision = None | ||
self._lineardeflection = None | ||
self._angulardeflection = None | ||
|
||
self._is_inited = True | ||
|
||
if unit is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not important.... but i guess unit will never be None because it has a default "M", no? |
||
self.unit = unit | ||
if absolute is not None: | ||
self.absolute = absolute | ||
if relative is not None: | ||
self.relative = relative | ||
if angular is not None: | ||
self.angular = angular | ||
if approximation is not None: | ||
self.approximation = approximation | ||
if precision is not None: | ||
self.precision = precision | ||
if lineardflection is not None: | ||
self.lineardeflection = lineardflection | ||
if angulardflection is not None: | ||
self.angulardeflection = angulardflection | ||
|
||
# this can be autogenerated if we use slots | ||
# __repr__: return f"{__class__.__name__}({', '.join(f'{k}={v!r}' for k, v in self.__dict__.items())})}" | ||
|
@@ -193,6 +212,23 @@ def reset(self): | |
self._lineardeflection = None | ||
self._angulardeflection = None | ||
|
||
def update_from_dict(self, tolerance): | ||
"""Update the tolerance singleton from the key-value pairs found in a dict. | ||
|
||
Parameters | ||
---------- | ||
tolerance : dict | ||
A dictionary containing named tolerance values. | ||
|
||
Returns | ||
------- | ||
None | ||
|
||
""" | ||
for name in tolerance: | ||
if hasattr(self, name): | ||
setattr(self, name, tolerance[name]) | ||
|
||
@property | ||
def units(self): | ||
return self._unit | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmm... not sure I understand here, under what scenario is this condition triggered? Is it equivalent for
if cls._instance is None
, or it has some wider purposes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it intended for inherited classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean the only possible types for
_instance
areTolerance
andNoneType
, no?