updated from Atlas

This commit is contained in:
Luciano Ramalho
2021-08-07 00:44:01 -03:00
parent cbd13885fc
commit 01e717b60a
96 changed files with 580 additions and 1021 deletions

View File

@@ -199,15 +199,17 @@ class Vector:
return self._components[index]
# tag::VECTOR_V3_GETATTR[]
shortcut_names = 'xyzt'
__match_args__ = ('x', 'y', 'z', 't') # <1>
def __getattr__(self, name):
cls = type(self) # <1>
if len(name) == 1: # <2>
pos = cls.shortcut_names.find(name) # <3>
if 0 <= pos < len(self._components): # <4>
return self._components[pos]
msg = f'{cls.__name__!r} object has no attribute {name!r}' # <5>
cls = type(self) # <2>
try:
pos = cls.__match_args__.index(name) # <3>
except ValueError: # <4>
pos = -1
if 0 <= pos < len(self._components): # <5>
return self._components[pos]
msg = f'{cls.__name__!r} object has no attribute {name!r}' # <6>
raise AttributeError(msg)
# end::VECTOR_V3_GETATTR[]
@@ -215,8 +217,8 @@ class Vector:
def __setattr__(self, name, value):
cls = type(self)
if len(name) == 1: # <1>
if name in cls.shortcut_names: # <2>
error = 'read-only attribute {attr_name!r}'
if name in cls.__match_args__: # <2>
error = 'readonly attribute {attr_name!r}'
elif name.islower(): # <3>
error = "can't set attributes 'a' to 'z' in {cls_name!r}"
else:

View File

@@ -199,14 +199,16 @@ class Vector:
index = operator.index(key)
return self._components[index]
shortcut_names = 'xyzt'
__match_args__ = ('x', 'y', 'z', 't')
def __getattr__(self, name):
cls = type(self)
if len(name) == 1:
pos = cls.shortcut_names.find(name)
if 0 <= pos < len(self._components):
return self._components[pos]
try:
pos = cls.__match_args__.index(name)
except ValueError:
pos = -1
if 0 <= pos < len(self._components):
return self._components[pos]
msg = f'{cls.__name__!r} object has no attribute {name!r}'
raise AttributeError(msg)

View File

@@ -242,14 +242,16 @@ class Vector:
index = operator.index(key)
return self._components[index]
shortcut_names = 'xyzt'
__match_args__ = ('x', 'y', 'z', 't')
def __getattr__(self, name):
cls = type(self)
if len(name) == 1:
pos = cls.shortcut_names.find(name)
if 0 <= pos < len(self._components):
return self._components[pos]
try:
pos = cls.__match_args__.index(name)
except ValueError:
pos = -1
if 0 <= pos < len(self._components):
return self._components[pos]
msg = f'{cls.__name__!r} object has no attribute {name!r}'
raise AttributeError(msg)