From df96110733c85da52615c8f1b05794c0ac95e2cc Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 29 Jan 2022 22:38:17 +0100 Subject: [PATCH] some suggestions from prospector/pylint --- python/damask/_colormap.py | 18 +++++++++--------- python/damask/_grid.py | 24 ++++++++++++------------ python/damask/_table.py | 22 +++++++++++----------- python/damask/tensor.py | 2 +- python/damask/util.py | 26 +++++++++++++------------- 5 files changed, 46 insertions(+), 46 deletions(-) diff --git a/python/damask/_colormap.py b/python/damask/_colormap.py index 9d9776821..005cf80d6 100644 --- a/python/damask/_colormap.py +++ b/python/damask/_colormap.py @@ -267,7 +267,7 @@ class Colormap(mpl.colors.ListedColormap): (bounds[0],bounds[1]) if abs(delta := r-l) * 1e8 <= (avg := 0.5*abs(r+l)): # delta is similar to numerical noise - l,r = l-0.5*avg*np.sign(delta),r+0.5*avg*np.sign(delta), # extend range to have actual data centered within + l,r = (l-0.5*avg*np.sign(delta),r+0.5*avg*np.sign(delta)) # extend range to have actual data centered within return Image.fromarray( (np.dstack(( @@ -301,7 +301,7 @@ class Colormap(mpl.colors.ListedColormap): >>> damask.Colormap.from_predefined('stress').reversed() """ - rev = super(Colormap,self).reversed(name) + rev = super().reversed(name) return Colormap(np.array(rev.colors),rev.name[:-4] if rev.name.endswith('_r_r') else rev.name) @@ -436,11 +436,11 @@ class Colormap(mpl.colors.ListedColormap): def adjust_hue(msh_sat, msh_unsat): """If saturation of one of the two colors is much less than the other, hue of the less.""" if msh_sat[0] >= msh_unsat[0]: - return msh_sat[2] - else: - hSpin = msh_sat[1]/np.sin(msh_sat[1])*np.sqrt(msh_unsat[0]**2.0-msh_sat[0]**2)/msh_sat[0] - if msh_sat[2] < - np.pi/3.0: hSpin *= -1.0 - return msh_sat[2] + hSpin + return msh_sat[2] + + hSpin = msh_sat[1]/np.sin(msh_sat[1])*np.sqrt(msh_unsat[0]**2.0-msh_sat[0]**2)/msh_sat[0] + if msh_sat[2] < - np.pi/3.0: hSpin *= -1.0 + return msh_sat[2] + hSpin lo = np.array(low) hi = np.array(high) @@ -453,9 +453,9 @@ class Colormap(mpl.colors.ListedColormap): else: lo = np.array([M_mid,0.0,0.0]) frac = 2.0*frac - 1.0 - if lo[1] < 0.05 and hi[1] > 0.05: + if lo[1] < 0.05 < hi[1]: lo[2] = adjust_hue(hi,lo) - elif lo[1] > 0.05 and hi[1] < 0.05: + elif hi[1] < 0.05 < lo[1]: hi[2] = adjust_hue(lo,hi) return (1.0 - frac) * lo + frac * hi diff --git a/python/damask/_grid.py b/python/damask/_grid.py index 639b4e91f..0b3a067e2 100644 --- a/python/damask/_grid.py +++ b/python/damask/_grid.py @@ -33,7 +33,7 @@ class Grid: material: np.ndarray, size: FloatSequence, origin: FloatSequence = np.zeros(3), - comments: Union[str, Sequence[str]] = []): + comments: Union[str, Sequence[str]] = None): """ New geometry definition for grid solvers. @@ -53,7 +53,7 @@ class Grid: self.material = material self.size = size # type: ignore self.origin = origin # type: ignore - self.comments = comments # type: ignore + self.comments = [] if comments is None else comments # type: ignore def __repr__(self) -> str: @@ -106,14 +106,14 @@ class Grid: material: np.ndarray): if len(material.shape) != 3: raise ValueError(f'invalid material shape {material.shape}') - elif material.dtype not in np.sctypes['float'] and material.dtype not in np.sctypes['int']: + if material.dtype not in np.sctypes['float'] and material.dtype not in np.sctypes['int']: raise TypeError(f'invalid material data type {material.dtype}') - else: - self._material = np.copy(material) - if self.material.dtype in np.sctypes['float'] and \ - np.all(self.material == self.material.astype(int).astype(float)): - self._material = self.material.astype(int) + self._material = np.copy(material) + + if self.material.dtype in np.sctypes['float'] and \ + np.all(self.material == self.material.astype(int).astype(float)): + self._material = self.material.astype(int) @property @@ -126,8 +126,8 @@ class Grid: size: FloatSequence): if len(size) != 3 or any(np.array(size) < 0): raise ValueError(f'invalid size {size}') - else: - self._size = np.array(size) + + self._size = np.array(size) @property def origin(self) -> np.ndarray: @@ -139,8 +139,8 @@ class Grid: origin: FloatSequence): if len(origin) != 3: raise ValueError(f'invalid origin {origin}') - else: - self._origin = np.array(origin) + + self._origin = np.array(origin) @property def comments(self) -> List[str]: diff --git a/python/damask/_table.py b/python/damask/_table.py index bd82dc134..1572c4f76 100644 --- a/python/damask/_table.py +++ b/python/damask/_table.py @@ -14,7 +14,7 @@ class Table: def __init__(self, data: np.ndarray, - shapes: dict, + shapes: dict, comments: Union[str, list] = None): """ New spreadsheet. @@ -526,10 +526,10 @@ class Table: """ if self.shapes != other.shapes or not self.data.columns.equals(other.data.columns): raise KeyError('Labels or shapes or order do not match') - else: - dup = self.copy() - dup.data = dup.data.append(other.data,ignore_index=True) - return dup + + dup = self.copy() + dup.data = dup.data.append(other.data,ignore_index=True) + return dup def join(self, @@ -552,12 +552,12 @@ class Table: """ if set(self.shapes) & set(other.shapes) or self.data.shape[0] != other.data.shape[0]: raise KeyError('Duplicated keys or row count mismatch') - else: - dup = self.copy() - dup.data = dup.data.join(other.data) - for key in other.shapes: - dup.shapes[key] = other.shapes[key] - return dup + + dup = self.copy() + dup.data = dup.data.join(other.data) + for key in other.shapes: + dup.shapes[key] = other.shapes[key] + return dup def save(self, diff --git a/python/damask/tensor.py b/python/damask/tensor.py index 0e2118002..63eb15b99 100644 --- a/python/damask/tensor.py +++ b/python/damask/tensor.py @@ -64,7 +64,7 @@ def eigenvectors(T_sym: _np.ndarray, associated eigenvalues. """ - (u,v) = _np.linalg.eigh(symmetric(T_sym)) + _,v = _np.linalg.eigh(symmetric(T_sym)) if RHS: v[_np.linalg.det(v) < 0.0,:,2] *= -1.0 return v diff --git a/python/damask/util.py b/python/damask/util.py index c042e4dd4..0cf6606fb 100644 --- a/python/damask/util.py +++ b/python/damask/util.py @@ -7,7 +7,7 @@ import subprocess import shlex import re import fractions -import collections.abc as abc +from collections import abc from functools import reduce from typing import Union, Tuple, Iterable, Callable, Dict, List, Any, Literal, Optional from pathlib import Path @@ -77,7 +77,7 @@ def srepr(msg, hasattr(msg, '__iter__'))): return glue.join(str(x) for x in msg) else: - return msg if isinstance(msg,str) else repr(msg) + return msg if isinstance(msg,str) else repr(msg) def emph(msg) -> str: @@ -230,15 +230,15 @@ def show_progress(iterable: Iterable, """ if isinstance(iterable,abc.Sequence): - if N_iter is None: - N = len(iterable) - else: - raise ValueError('N_iter given for sequence') + if N_iter is None: + N = len(iterable) + else: + raise ValueError('N_iter given for sequence') else: - if N_iter is None: - raise ValueError('N_iter not given') - else: - N = N_iter + if N_iter is None: + raise ValueError('N_iter not given') + + N = N_iter if N <= 1: for item in iterable: @@ -454,7 +454,7 @@ def shapeshifter(fro: Tuple[int, ...], """ - if not len(fro) and not len(to): return () + if len(fro) == 0 and len(to) == 0: return () beg = dict(left ='(^.*\\b)', right='(^.*?\\b)') @@ -462,8 +462,8 @@ def shapeshifter(fro: Tuple[int, ...], right='(.*?\\b)') end = dict(left ='(.*?$)', right='(.*$)') - fro = (1,) if not len(fro) else fro - to = (1,) if not len(to) else to + fro = (1,) if len(fro) == 0 else fro + to = (1,) if len(to) == 0 else to try: match = re.match(beg[mode] +f',{sep[mode]}'.join(map(lambda x: f'{x}'