From 3bb7bc5212ad2485092b952c678b80eca254ae21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 5 Nov 2024 09:57:00 +0100 Subject: [PATCH] using pathlib in 3 files --- src/sage/rings/polynomial/pbori/nf.py | 19 ++++--- src/sage/sat/solvers/dimacs.py | 50 +++++++++---------- .../schemes/elliptic_curves/ec_database.py | 7 ++- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/sage/rings/polynomial/pbori/nf.py b/src/sage/rings/polynomial/pbori/nf.py index 004abef5243..09d985f9885 100644 --- a/src/sage/rings/polynomial/pbori/nf.py +++ b/src/sage/rings/polynomial/pbori/nf.py @@ -1,3 +1,6 @@ +from pathlib import Path +from warnings import warn + from sage.rings.polynomial.pbori.pbori import mod_mon_set from .pbori import (BooleSet, GroebnerStrategy, ReductionStrategy, parallel_reduce, easy_linear_factors) @@ -6,8 +9,6 @@ from .easy_polynomials import (easy_linear_polynomials as easy_linear_polynomials_func) from .statistics import used_vars_set -from warnings import warn -import os class GeneratorLimitExceeded(Exception): @@ -69,9 +70,8 @@ def build_and_print_matrices(v, strat): assert j < cols im.putpixel((j, i), 0) - file_name = strat.matrix_prefix + str(mat_counter) + ".png" - if os.path.exists(file_name): - os.remove(file_name) + file_name = Path(strat.matrix_prefix + str(mat_counter) + ".png") + file_name.unlink(missing_ok=True) im.save(file_name) del im @@ -84,8 +84,8 @@ def multiply_polynomials(l, ring): TESTS:: sage: from sage.rings.polynomial.pbori import * - sage: r=Ring(1000) - sage: x=r.variable + sage: r = Ring(1000) + sage: x = r.variable sage: from sage.rings.polynomial.pbori.nf import multiply_polynomials sage: multiply_polynomials([x(3), x(2)+x(5)*x(6), x(0), x(0)+1], r) 0 @@ -149,9 +149,8 @@ def build_and_print_matrices_deg_colored(v, strat): assert j < cols hsl = str(270 - (270 * i2deg[j]) / max_deg) im.putpixel((j, i), ImageColor.getrgb("hsl(" + hsl + ",100%,50%)")) - file_name = strat.matrix_prefix + str(mat_counter) + ".png" - if os.path.exists(file_name): - os.remove(file_name) + file_name = Path(strat.matrix_prefix + str(mat_counter) + ".png") + file_name.unlink(missing_ok=True) im.save(file_name) del im diff --git a/src/sage/sat/solvers/dimacs.py b/src/sage/sat/solvers/dimacs.py index 61d5460d432..09ec052b914 100644 --- a/src/sage/sat/solvers/dimacs.py +++ b/src/sage/sat/solvers/dimacs.py @@ -28,14 +28,14 @@ # https://www.gnu.org/licenses/ ############################################################################## -import os +from pathlib import Path import sys import subprocess import shlex +from time import sleep from sage.sat.solvers.satsolver import SatSolver from sage.misc.temporary_file import tmp_filename -from time import sleep class DIMACS(SatSolver): @@ -44,7 +44,7 @@ class DIMACS(SatSolver): .. NOTE:: - Usually, users won't have to use this class directly but some + Usually, users will not have to use this class directly but some class which inherits from this class. .. automethod:: __init__ @@ -136,10 +136,9 @@ def __del__(self): """ if not self._tail.closed: self._tail.close() - if os.path.exists(self._tail.name): - os.unlink(self._tail.name) - if self._headname_file_created_during_init and os.path.exists(self._headname): - os.unlink(self._headname) + Path(self._tail.name).unlink(missing_ok=True) + if self._headname_file_created_during_init: + Path(self._headname).unlink(missing_ok=True) def var(self, decision=None): """ @@ -209,7 +208,7 @@ def add_clause(self, lits): self.var() l.append(str(lit)) l.append("0\n") - self._tail.write(" ".join(l) ) + self._tail.write(" ".join(l)) self._lit += 1 def write(self, filename=None): @@ -246,19 +245,19 @@ def write(self, filename=None): headname = self._headname if filename is None else filename head = open(headname, "w") head.truncate(0) - head.write("p cnf %d %d\n" % (self._var,self._lit)) + head.write("p cnf %d %d\n" % (self._var, self._lit)) head.close() tail = self._tail tail.close() - head = open(headname,"a") + head = open(headname, "a") tail = open(self._tail.name) head.write(tail.read()) tail.close() head.close() - self._tail = open(self._tail.name,"a") + self._tail = open(self._tail.name, "a") return headname def clauses(self, filename=None): @@ -313,7 +312,7 @@ def clauses(self, filename=None): if lit == 0: break clause.append(lit) - clauses.append( ( tuple(clause), False, None ) ) + clauses.append((tuple(clause), False, None)) tail.close() self._tail = open(self._tail.name, "a") return clauses @@ -362,20 +361,19 @@ def render_dimacs(clauses, filename, nlits): 1 2 -3 0 """ - fh = open(filename, "w") - fh.write("p cnf %d %d\n" % (nlits,len(clauses))) - for clause in clauses: - if len(clause) == 3 and clause[1] in (True, False) and clause[2] in (True,False,None): - lits, is_xor, rhs = clause - else: - lits, is_xor, rhs = clause, False, None - - if is_xor: - closing = lits[-1] if rhs else -lits[-1] - fh.write("x" + " ".join(map(str, lits[:-1])) + " %d 0\n" % closing) - else: - fh.write(" ".join(map(str, lits)) + " 0\n") - fh.close() + with open(filename, "w") as fh: + fh.write("p cnf %d %d\n" % (nlits, len(clauses))) + for clause in clauses: + if len(clause) == 3 and clause[1] in (True, False) and clause[2] in (True, False, None): + lits, is_xor, rhs = clause + else: + lits, is_xor, rhs = clause, False, None + + if is_xor: + closing = lits[-1] if rhs else -lits[-1] + fh.write("x" + " ".join(map(str, lits[:-1])) + " %d 0\n" % closing) + else: + fh.write(" ".join(map(str, lits)) + " 0\n") def _run(self): r""" diff --git a/src/sage/schemes/elliptic_curves/ec_database.py b/src/sage/schemes/elliptic_curves/ec_database.py index 7d418feb437..263e4ba930c 100755 --- a/src/sage/schemes/elliptic_curves/ec_database.py +++ b/src/sage/schemes/elliptic_curves/ec_database.py @@ -67,7 +67,7 @@ which enable easy looping through the Cremona elliptic curve database. """ -import os +from pathlib import Path from ast import literal_eval from .constructor import EllipticCurve @@ -132,10 +132,9 @@ def rank(self, rank, tors=0, n=10, labels=False): """ from sage.features.databases import DatabaseEllcurves db = DatabaseEllcurves() - data = os.path.join(os.path.dirname(db.absolute_filename()), - f'rank{rank}') + data = Path(db.absolute_filename()).parent / f'rank{rank}' try: - f = open(data) + f = data.open() except OSError: return [] v = []