8000 use pathlib and decode in Kohel database by fchapoton · Pull Request #40060 · sagemath/sage · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

use pathlib and decode in Kohel database #40060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/sage/databases/db_modular_polynomials.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
# https://www.gnu.org/licenses/
# ****************************************************************************
import bz2
import os
from sage.cpython.string import bytes_to_str
from pathlib import Path


def _dbz_to_string(name):
def _dbz_to_string(name) -> str:
r"""
TESTS::

Expand All @@ -54,17 +53,16 @@
'0\n1\n'
"""
from sage.env import SAGE_SHARE
dblocation = os.path.join(SAGE_SHARE, 'kohel')
filename = os.path.join(dblocation, name)
filename = Path(SAGE_SHARE) / 'kohel' / name
try:
with open(filename, 'rb') as f:
data = bz2.decompress(f.read())
except OSError:
raise ValueError('file not found in the Kohel database')
return bytes_to_str(data)
raise FileNotFoundError('file not found in the Kohel database')
return data.decode()

Check warning on line 62 in src/sage/databases/db_modular_polynomials.py

View check run for this annotation

Codecov / codecov/patch

src/sage/databases/db_modular_polynomials.py#L62

Added line #L62 was not covered by tests


def _dbz_to_integer_list(name):
def _dbz_to_integer_list(name) -> list[list]:
r"""
TESTS::

Expand All @@ -90,7 +88,7 @@
for row in data.split("\n")[:-1]]


def _dbz_to_integers(name):
def _dbz_to_integers(name) -> list:
r"""
TESTS::

Expand All @@ -103,19 +101,20 @@


class ModularPolynomialDatabase:
def _dbpath(self, level):
def _dbpath(self, level) -> Path:
r"""
TESTS::

sage: C = ClassicalModularPolynomialDatabase()
sage: C._dbpath(3)
'PolMod/Cls/pol.003.dbz'
PosixPath('PolMod/Cls/pol.003.dbz')
sage: C._dbpath(8)
'PolMod/Cls/pol.008.dbz'
PosixPath('PolMod/Cls/pol.008.dbz')
"""
return "PolMod/%s/pol.%03d.dbz" % (self.model, level)
path = Path("PolMod")
return path / self.model / ("pol.%03d.dbz" % level)

def __repr__(self):
def __repr__(self) -> str:
r"""
EXAMPLES::

Expand Down Expand Up @@ -161,7 +160,7 @@
sage: DBMP[50]
Traceback (most recent call last):
...
ValueError: file not found in the Kohel database
FileNotFoundError: file not found in the Kohel database
"""
from sage.rings.integer import Integer
from sage.rings.integer_ring import IntegerRing
Expand Down Expand Up @@ -198,16 +197,16 @@


class ModularCorrespondenceDatabase(ModularPolynomialDatabase):
def _dbpath(self, level):
def _dbpath(self, level) -> Path:
r"""
TESTS::

sage: DB = DedekindEtaModularCorrespondenceDatabase()
sage: DB._dbpath((2,4))
'PolMod/EtaCrr/crr.02.004.dbz'
PosixPath('PolMod/EtaCrr/crr.02.004.dbz')
"""
(Nlevel, crrlevel) = level
return "PolMod/%s/crr.%02d.%03d.dbz" % (self.model, Nlevel, crrlevel)
path = Path("PolMod")
return path / self.model / ("crr.%02d.%03d.dbz" % level)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the use of level is tricky.



class ClassicalModularPolynomialDatabase(ModularPolynomialDatabase):
Expand Down
7 changes: 4 additions & 3 deletions src/sage/schemes/elliptic_curves/mod_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ def classical_modular_polynomial(l, j=None):

try:
Phi = ZZ['X,Y'](_db[l])
except ValueError:
except (FileNotFoundError, ValueError):
try:
pari_Phi = pari.polmodular(l)
except PariError:
raise NotImplementedError('modular polynomial is not in database and computing it on the fly is not yet implemented')
d = {(i, j): c for i,f in enumerate(pari_Phi) for j, c in enumerate(f)}
d = {(i, j): c for i, f in enumerate(pari_Phi)
for j, c in enumerate(f)}
Phi = ZZ['X,Y'](d)

if l <= _cache_bound:
Expand All @@ -140,7 +141,7 @@ def classical_modular_polynomial(l, j=None):
return _cache[l](j, Y)
try:
Phi = _db[l]
except ValueError:
except (ValueError, FileNotFoundError):
pass
else:
if l <= _cache_bound:
Expand Down
Loading
0