it's useful for users to see standard docstrings in magic methods

This commit is contained in:
d.mentock 2022-07-08 18:06:41 +02:00
parent 655103d39e
commit df1f362ed3
9 changed files with 194 additions and 27 deletions

View File

@ -48,7 +48,12 @@ class Colormap(mpl.colors.ListedColormap):
def __eq__(self,
other: object) -> bool:
"""Test equality of colormaps."""
"""
Return self==other.
Test equality of colormaps.
"""
if not isinstance(other, Colormap):
return NotImplemented
return len(self.colors) == len(other.colors) \
@ -56,31 +61,61 @@ class Colormap(mpl.colors.ListedColormap):
def __add__(self,
other: 'Colormap') -> 'Colormap':
"""Concatenate."""
"""
Return self+other.
Concatenate.
"""
return Colormap(np.vstack((self.colors,other.colors)),
f'{self.name}+{other.name}')
def __iadd__(self,
other: 'Colormap') -> 'Colormap':
"""Concatenate (in-place)."""
"""
Return self+=other.
Concatenate (in-place).
"""
return self.__add__(other)
def __mul__(self,
factor: int) -> 'Colormap':
"""Repeat."""
"""
Return self*other.
Repeat.
"""
return Colormap(np.vstack([self.colors]*factor),f'{self.name}*{factor}')
def __imul__(self,
factor: int) -> 'Colormap':
"""Repeat (in-place)."""
"""
Return self*=other.
Repeat (in-place).
"""
return self.__mul__(factor)
def __invert__(self) -> 'Colormap':
"""Reverse."""
"""
Return ~self.
Return reverse of self
"""
return self.reversed()
def __repr__(self) -> str:
"""Show as matplotlib figure."""
"""
Return repr(self).
Show as matplotlib figure.
"""
fig = plt.figure(self.name,figsize=(5,.5))
ax1 = fig.add_axes([0, 0, 1, 1])
ax1.set_axis_off()

View File

@ -64,7 +64,12 @@ class Config(dict):
super().__init__(**kwargs)
def __repr__(self) -> str:
"""Show as in file."""
"""
Return repr(self).
Show as in file.
"""
output = StringIO()
self.save(output)
output.seek(0)
@ -72,7 +77,12 @@ class Config(dict):
def __copy__(self: MyType) -> MyType:
"""Create deep copy."""
"""
Return deepcopy(self).
Create deep copy.
"""
return copy.deepcopy(self)
copy = __copy__
@ -81,6 +91,8 @@ class Config(dict):
def __or__(self: MyType,
other) -> MyType:
"""
Return self|other.
Update configuration with contents of other.
Parameters
@ -105,7 +117,12 @@ class Config(dict):
def __ior__(self: MyType,
other) -> MyType:
"""Update configuration with contents of other."""
"""
Return self|=other.
Update configuration with contents of other.
"""
return self.__or__(other)

View File

@ -403,7 +403,12 @@ class Crystal():
def __repr__(self):
"""Give short human-readable summary."""
"""
Return repr(self).
Give short human-readable summary.
"""
family = f'Crystal family: {self.family}'
return family if self.lattice is None else \
util.srepr([family,
@ -415,6 +420,8 @@ class Crystal():
def __eq__(self,
other: object) -> bool:
"""
Return self==other.
Equal to other.
Parameters

View File

@ -62,7 +62,12 @@ class Grid:
self.comments = [] if comments_ is None else [str(c) for c in comments_]
def __repr__(self) -> str:
"""Give short human-readable summary."""
"""
Return repr(self).
Give short human-readable summary.
"""
mat_min = np.nanmin(self.material)
mat_max = np.nanmax(self.material)
mat_N = self.N_materials
@ -76,7 +81,12 @@ class Grid:
def __copy__(self) -> 'Grid':
"""Create deep copy."""
"""
Return deepcopy(self).
Create deep copy.
"""
return copy.deepcopy(self)
copy = __copy__
@ -85,6 +95,8 @@ class Grid:
def __eq__(self,
other: object) -> bool:
"""
Return self==other.
Test equality of other.
Parameters

View File

@ -120,14 +120,24 @@ class Orientation(Rotation,Crystal):
def __repr__(self) -> str:
"""Give short human-readable summary."""
"""
Return repr(self).
Give short human-readable summary.
"""
return util.srepr([Crystal.__repr__(self),
Rotation.__repr__(self)])
def __copy__(self: MyType,
rotation: Union[FloatSequence, Rotation] = None) -> MyType:
"""Create deep copy."""
"""
Return deepcopy(self).
Create deep copy.
"""
dup = copy.deepcopy(self)
if rotation is not None:
dup.quaternion = Rotation(rotation).quaternion
@ -140,6 +150,8 @@ class Orientation(Rotation,Crystal):
def __eq__(self,
other: object) -> bool:
"""
Return self==other.
Equal to other.
Parameters
@ -158,6 +170,8 @@ class Orientation(Rotation,Crystal):
def __ne__(self,
other: object) -> bool:
"""
Return self!=other.
Not equal to other.
Parameters

View File

@ -152,14 +152,24 @@ class Result:
def __copy__(self) -> "Result":
"""Create deep copy."""
"""
Return deepcopy(self).
Create deep copy.
"""
return copy.deepcopy(self)
copy = __copy__
def __repr__(self) -> str:
"""Give short human-readable summary."""
"""
Return repr(self).
Give short human-readable summary.
"""
with h5py.File(self.fname,'r') as f:
header = [f'Created by {f.attrs["creator"]}',
f' on {f.attrs["created"]}',

View File

@ -88,14 +88,24 @@ class Rotation:
def __repr__(self) -> str:
"""Give short human-readable summary."""
"""
Return repr(self).
Give short human-readable summary.
"""
return f'Quaternion{" " if self.quaternion.shape == (4,) else "s of shape "+str(self.quaternion.shape[:-1])+chr(10)}'\
+ str(self.quaternion)
def __copy__(self: MyType,
rotation: Union[FloatSequence, 'Rotation'] = None) -> MyType:
"""Create deep copy."""
"""
Return deepcopy(self).
Create deep copy.
"""
dup = copy.deepcopy(self)
if rotation is not None:
dup.quaternion = Rotation(rotation).quaternion
@ -106,7 +116,12 @@ class Rotation:
def __getitem__(self,
item: Union[Tuple[int], int, bool, np.bool_, np.ndarray]):
"""Return slice according to item."""
"""
Return x[y].
Return slice according to item.
"""
return self.copy() if self.shape == () else \
self.copy(self.quaternion[item+(slice(None),)] if isinstance(item,tuple) else self.quaternion[item])
@ -114,6 +129,8 @@ class Rotation:
def __eq__(self,
other: object) -> bool:
"""
Return self==other.
Equal to other.
Parameters
@ -130,6 +147,8 @@ class Rotation:
def __ne__(self,
other: object) -> bool:
"""
Return self!=other.
Not equal to other.
Parameters
@ -214,12 +233,22 @@ class Rotation:
def __len__(self) -> int:
"""Length of leading/leftmost dimension of array."""
"""
Return len(self).
Length of leading/leftmost dimension of array.
"""
return 0 if self.shape == () else self.shape[0]
def __invert__(self: MyType) -> MyType:
"""Inverse rotation (backward rotation)."""
"""
Return ~self.
Inverse rotation (backward rotation).
"""
dup = self.copy()
dup.quaternion[...,1:] *= -1
return dup
@ -228,6 +257,8 @@ class Rotation:
def __pow__(self: MyType,
exp: Union[float, int]) -> MyType:
"""
Return self**exp.
Perform the rotation 'exp' times.
Parameters
@ -243,6 +274,8 @@ class Rotation:
def __ipow__(self: MyType,
exp: Union[float, int]) -> MyType:
"""
Return self**=exp.
Perform the rotation 'exp' times (in-place).
Parameters
@ -257,6 +290,8 @@ class Rotation:
def __mul__(self: MyType,
other: MyType) -> MyType:
"""
Return self*other.
Compose with other.
Parameters
@ -284,6 +319,8 @@ class Rotation:
def __imul__(self: MyType,
other: MyType) -> MyType:
"""
Return self*=other.
Compose with other (in-place).
Parameters
@ -298,6 +335,8 @@ class Rotation:
def __truediv__(self: MyType,
other: MyType) -> MyType:
"""
Return self/other.
Compose with inverse of other.
Parameters
@ -319,6 +358,8 @@ class Rotation:
def __itruediv__(self: MyType,
other: MyType) -> MyType:
"""
Return self/=other.
Compose with inverse of other (in-place).
Parameters
@ -333,6 +374,8 @@ class Rotation:
def __matmul__(self,
other: np.ndarray) -> np.ndarray:
"""
Return self@other.
Rotate vector, second order tensor, or fourth order tensor.
Parameters

View File

@ -37,7 +37,12 @@ class Table:
def __repr__(self) -> str:
"""Give short human-readable summary."""
"""
Return repr(self).
Give short human-readable summary.
"""
self._relabel('shapes')
data_repr = self.data.__repr__()
self._relabel('uniform')
@ -46,7 +51,12 @@ class Table:
def __eq__(self,
other: object) -> bool:
"""Compare to other Table."""
"""
Return self==other.
Compare to other Table.
"""
return NotImplemented if not isinstance(other,Table) else \
self.shapes == other.shapes and self.data.equals(other.data)
@ -54,6 +64,8 @@ class Table:
def __getitem__(self,
item: Union[slice, Tuple[slice, ...]]) -> 'Table':
"""
Return x[y].
Slice the Table according to item.
Parameters
@ -102,12 +114,22 @@ class Table:
def __len__(self) -> int:
"""Number of rows."""
"""
Return len(self).
Number of rows.
"""
return len(self.data)
def __copy__(self) -> 'Table':
"""Create deep copy."""
"""
Return deepcopy(self).
Create deep copy.
"""
return copy.deepcopy(self)
copy = __copy__

View File

@ -39,7 +39,12 @@ class VTK:
def __repr__(self) -> str:
"""Give short human-readable summary."""
"""
Return repr(self).
Give short human-readable summary.
"""
info = [self.vtk_data.__vtkname__]
for data in ['Cell Data', 'Point Data']:
@ -54,6 +59,8 @@ class VTK:
def __eq__(self,
other: object) -> bool:
"""
Return self==other.
Equal to other.
Parameters