From ffa8abcd5159554ab1bcd82466498548289f6012 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Mon, 12 Jun 2023 16:57:32 -0400 Subject: [PATCH] apply C4 rule (#1953) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- pyproject.toml | 1 + reacnetgenerator/_detect.py | 20 +++++++++----------- reacnetgenerator/_draw.py | 4 ++-- reacnetgenerator/_matrix.py | 2 +- reacnetgenerator/_path.py | 12 +++++------- reacnetgenerator/_reachtml.py | 6 +++--- reacnetgenerator/utils.py | 2 +- 7 files changed, 22 insertions(+), 25 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 401944342..4029d0972 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -143,6 +143,7 @@ select = [ "D", # pydocstyle "I", # isort "UP", # pyupgrade + "C4", # flake8-comprehensions ] ignore = [ "E501", # line too long diff --git a/reacnetgenerator/_detect.py b/reacnetgenerator/_detect.py index dceedc594..b152ef794 100644 --- a/reacnetgenerator/_detect.py +++ b/reacnetgenerator/_detect.py @@ -185,18 +185,16 @@ def _connectmolecule(self, bond, level): # int() because sometimes, the elements in bondlist are type numpy.int64 sometimes are int # which cause the different bytes results from same bond-network. mols, bondlists = dps(bond, level) - return list( - [ - b"".join( - ( - listtobytes(mol), - listtobytes([(int(i[0]), int(i[1])) for i in bondlist]), - listtobytes([int(i[2]) for i in bondlist]), - ) + return [ + b"".join( + ( + listtobytes(mol), + listtobytes([(int(i[0]), int(i[1])) for i in bondlist]), + listtobytes([int(i[2]) for i in bondlist]), ) - for mol, bondlist in zip(mols, bondlists) - ] - ) + ) + for mol, bondlist in zip(mols, bondlists) + ] def _writemoleculetempfile(self, d): with WriteBuffer(tempfile.NamedTemporaryFile("wb", delete=False)) as f: diff --git a/reacnetgenerator/_draw.py b/reacnetgenerator/_draw.py index ef918843e..0098b6919 100644 --- a/reacnetgenerator/_draw.py +++ b/reacnetgenerator/_draw.py @@ -169,9 +169,9 @@ def _handlespecies(self, name): species = [spec for spec in species if spec not in self.speciesfilter] if self.showid: - showname = dict([(v, u) for u, v in enumerate(species, start=1)]) + showname = {v: u for u, v in enumerate(species, start=1)} else: - showname = dict([(u, u) for u in species]) + showname = {u: u for u in species} if species: logger.info("Species are:") for specname, n in showname.items(): diff --git a/reacnetgenerator/_matrix.py b/reacnetgenerator/_matrix.py index f34476141..bdba914d8 100644 --- a/reacnetgenerator/_matrix.py +++ b/reacnetgenerator/_matrix.py @@ -156,5 +156,5 @@ def _printspecies(self): d[t][name] += 1 for t in range(len(self.timestep)): fw.append(f"Timestep {self.timestep[t]}:") - fw.extend(map(lambda item: f" {item[0]} {item[1]}", d[t].items())) + fw.extend(f" {item[0]} {item[1]}" for item in d[t].items()) fw.append("\n") diff --git a/reacnetgenerator/_path.py b/reacnetgenerator/_path.py index 46daad7af..2540cfc42 100644 --- a/reacnetgenerator/_path.py +++ b/reacnetgenerator/_path.py @@ -98,12 +98,10 @@ def collect(self): self.allmoleculeroute = self._printatomroute(atomeach) if self.split > 1: splittime = np.array_split(np.arange(self.step), self.split) - self.splitmoleculeroute = list( - [ - self._printatomroute(atomeach[:, st], timeaxis=i) - for i, st in enumerate(splittime) - ] - ) + self.splitmoleculeroute = [ + self._printatomroute(atomeach[:, st], timeaxis=i) + for i, st in enumerate(splittime) + ] self.returnkeys() ReactionsFinder(self.rng).findreactions(atomeach.T, conflict.T) @@ -341,7 +339,7 @@ def __init__(self, cmp, atoms, bonds): self.graph = self._makemoleculegraph() counter = Counter(self._atomnames) self.name = "".join( - map(lambda atomname: f"{atomname}{counter[atomname]}", cmp.atomname) + f"{atomname}{counter[atomname]}" for atomname in cmp.atomname ) self._smiles = None self._convertSMILES = cmp.convertSMILES diff --git a/reacnetgenerator/_reachtml.py b/reacnetgenerator/_reachtml.py index 578672705..a17ae2837 100644 --- a/reacnetgenerator/_reachtml.py +++ b/reacnetgenerator/_reachtml.py @@ -101,8 +101,8 @@ def _re(self, smi): def _handlereaction(self, line): sx = line.split() left, right = sx[1].split("->") - left = list([self._re(spec) for spec in left.split("+")]) - right = list([self._re(spec) for spec in right.split("+")]) + left = [self._re(spec) for spec in left.split("+")] + right = [self._re(spec) for spec in right.split("+")] num = int(sx[0]) return left, right, num @@ -145,7 +145,7 @@ def _readspecies(self, reaction, timeaxis=None): if timeaxis is None: self._svgspecs.add(spec) # return list of dict - return list([{"s": spec, "i": i} for i, spec in enumerate(specs, 1)]) + return [{"s": spec, "i": i} for i, spec in enumerate(specs, 1)] def _readdata(self): self._reaction = [self._readreaction()] diff --git a/reacnetgenerator/utils.py b/reacnetgenerator/utils.py index 057430e8b..6a3d97b44 100644 --- a/reacnetgenerator/utils.py +++ b/reacnetgenerator/utils.py @@ -289,7 +289,7 @@ def listtostirng( if isinstance(l, str): return l if isinstance(l, (list, tuple, np.ndarray)): - return sep[0].join(map(lambda x: listtostirng(x, sep[1:]), l)) + return sep[0].join(listtostirng(x, sep[1:]) for x in l) return str(l)