Skip to content

Commit

Permalink
using pathlib.Path in 3 files
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Nov 5, 2024
1 parent 851abeb commit 8b14e7d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 40 deletions.
19 changes: 9 additions & 10 deletions src/sage/rings/polynomial/pbori/nf.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
50 changes: 24 additions & 26 deletions src/sage/sat/solvers/dimacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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__
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -362,20 +361,19 @@ def render_dimacs(clauses, filename, nlits):
1 2 -3 0
<BLANKLINE>
"""
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"""
Expand Down
7 changes: 3 additions & 4 deletions src/sage/schemes/elliptic_curves/ec_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit 8b14e7d

Please sign in to comment.