This commit is contained in:
Eloi Torrents 2025-09-21 14:45:15 +02:00
parent 1d9c1fe0dd
commit ee6e422a4d
10 changed files with 163 additions and 2 deletions

20
.gitignore vendored Normal file
View File

@ -0,0 +1,20 @@
__pycache__/
*.py[cod]
*.sage.pyc
*.sage.py
*.egg-info/
.sage/
.sage-cache/
.pytest_cache/
.mypy_cache/
.coverage
htmlcov/
dist/
build/
.venv/
.venv-*/
.env
.envrc
.DS_Store
.idea/
.vscode/

View File

@ -1 +1,4 @@
"THE BEER-WARE LICENSE" (Revision 42): <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return Poul-Henning Kamp "THE BEER-WARE LICENSE" (Revision 42):
Eloi Torrents <eloitor@duck.com> wrote this project. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return.
-- Eloi Torrents

View File

@ -1,2 +1,28 @@
# SageMath_package_template # SageMath Package Template
A lightweight template that shows how to structure a SageMath package and ship a couple of runnable demos.
## Quick start
- Install the package in editable mode with `sage -pip install -e .`
- Run the elliptic curve demo: `sage -python elliptic_curve_demo.py`
- Try the simple Sage script: `sage -python hello_world.sage`
## Just shortcuts
- `just prepare` create the Sage-aware virtualenv and install the package editable
- `just run` execute the elliptic curve demo inside that virtualenv
- `just test` run the unit tests with Sage's Python
## Running tests
Use Sage's Python so Sage-specific imports resolve correctly when you run the bundled unittest suite:
```bash
sage -python -m unittest discover -s tests -t .
```
## Project layout
- `demo_package/` minimal package source code
- `elliptic_curve_demo.py` Python entry point using SageMath
- `hello_world.sage` / `hello_world.sage.py` companion Sage script demo
- `tests/` example unittest-based tests
Feel free to copy this template, rename the package, and extend the demos for your own SageMath projects.

5
demo_package/__init__.py Normal file
View File

@ -0,0 +1,5 @@
"""Demo package exposing helper utilities."""
from .utils import greet
__all__ = ["greet"]

6
demo_package/utils.py Normal file
View File

@ -0,0 +1,6 @@
"""Utility functions for the demo package."""
def greet(name: str) -> str:
"""Return a friendly greeting for the provided name."""
return f"Hello, {name}!"

34
elliptic_curve_demo.py Normal file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env sage -python
from sage.all import EllipticCurve
from demo_package import greet
def describe_curve() -> None:
"""Construct an elliptic curve and print basic arithmetic information."""
E = EllipticCurve([0, 0, 0, -1, 1]) # y^2 = x^3 - x + 1
print(f"Elliptic curve E: {E}")
print(f"Discriminant: {E.discriminant()}")
print(f"j-invariant: {E.j_invariant()}")
rank = E.rank()
print(f"Rank: {rank}")
generators = E.gens()
if generators:
P = generators[0]
print(f"Generator: {P}")
for n in range(1, 6):
multiple = n * P
print(f"{n} * P = {multiple}")
else:
print("Curve has no generators over Q.")
def main() -> None:
print(greet("Sage enthusiast"))
describe_curve()
if __name__ == "__main__":
main()

18
justfile Normal file
View File

@ -0,0 +1,18 @@
SAGE_PY := "sage -python"
# Create the Sage-aware virtual environment and install this package editable.
prepare:
if [ ! -d .venv ]; then {{SAGE_PY}} -m venv --system-site-packages .venv; fi
.venv/bin/pip install --no-build-isolation -e .
# Upgrade pip inside the managed virtual environment.
update: prepare
.venv/bin/python -m pip install --upgrade pip
# Run the elliptic curve demo using the virtual environment.
run: prepare
.venv/bin/python elliptic_curve_demo.py
# Execute the unit test suite.
test: prepare
.venv/bin/python -m unittest discover -s tests -t .

37
pyproject.toml Normal file
View File

@ -0,0 +1,37 @@
[build-system]
requires = ["setuptools>=64", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "demo-sage-package"
version = "0.1.0"
description = "Demo package used alongside SageMath elliptic-curve computations"
readme = "README.md"
requires-python = ">=3.10"
authors = [
{ name = "Eloi Torrents", email = "eloitor@duck.com" }
]
keywords = ["sagemath", "template", "mathematics"]
license = { file = "LICENSE" }
classifiers = [
"Intended Audience :: Science/Research",
"License :: Other/Proprietary License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Topic :: Scientific/Engineering :: Mathematics"
]
[project.urls]
Documentation = "https://git.32bit.cafe/eloitor/SageMath_package_template"
Source = "https://git.32bit.cafe/eloitor/SageMath_package_template"
Issues = "https://git.32bit.cafe/eloitor/SageMath_package_template/issues"
[tool.setuptools]
include-package-data = true
[tool.setuptools.packages.find]
where = ["."]
include = ["demo_package*"]

0
tests/__init__.py Normal file
View File

12
tests/test_utils.py Normal file
View File

@ -0,0 +1,12 @@
import unittest
from demo_package import greet
class GreetTests(unittest.TestCase):
def test_returns_expected_greeting(self) -> None:
self.assertEqual(greet("Ada"), "Hello, Ada!")
if __name__ == "__main__":
unittest.main()