Skip to main content

The Source Academy Python standard library for CPython: run CS1101S / SICP (Python edition) programs under plain CPython.

Project description

sourceacademy-sicp

The Source Academy Python standard library, packaged for plain CPython.

Source Academy runs a subset of Python (via py-slang) that adds a standard library for the Structure and Interpretation of Computer Programs (SICP) — pair, head, tail, llist, the math_* functions, streams, and so on. This package provides those same names to ordinary CPython, so that code written for CS1101S or the SICP Python edition runs the same way on your own machine.

Install

pip install sourceacademy-sicp

Not on PyPI yet. No GitHub Release has been cut, so the publish job below has never run and this currently fails with "No matching distribution found". Until a release exists, install straight from this repository instead:

pip install "git+https://github.com/source-academy/py-slang.git#subdirectory=python"

Use

Put this at the top of your program:

from sicp import *

Then the Source Academy Python standard library is available:

xs = llist(1, 2, 3)
print(xs)                       # [1, [2, [3, None]]]
print_llist(xs)                 # llist(1, 2, 3)
print(list_length(build_list(lambda i: i * i, 5)))   # 5
print(math_sqrt(2))             # 1.4142135623730951
print(eval_stream(integers_from(1), 4))              # [1, [2, [3, [4, None]]]]

Representation

A pair is a two-element Python list [head, tail], and the empty list is None — exactly as in Source Academy Python. A two-element list is therefore also a pair (is_pair and is_list are both true), matching the frontend's value model, so results print and compare directly against the textbook:

pair("x", 9)      # ['x', 9]
llist(1, 2, 3)    # [1, [2, [3, None]]]

What's included

The library mirrors the groups in py-slang's standard library:

submodule provides
sicp.misc error, arity, real, imag, is_none / is_integer / is_float / is_complex / is_string / is_boolean / is_function / is_number, random_random, time_time
sicp.math math_pi, math_e, math_tau, math_inf, math_nan, and the math_* functions (math_sqrt, math_sin, math_floor, math_comb, …)
sicp.linked_list pair, head, tail, is_pair, llist, print_llist, plus map, filter, reduce, reverse, append, length, member, remove, enum_llist, …
sicp.pair_mutators set_head, set_tail
sicp.list is_list, list_length, equal, build_list
sicp.stream stream, stream_map, stream_filter, stream_ref, integers_from, eval_stream, …

from sicp import * brings the whole superset into scope, mirroring the environment students have in Source Academy's Python.

For the full documentation of every function — signatures, descriptions, and worked examples — see the Python §4 standard library reference. Its groups (MISC, MATH, LINKED LISTS, PAIR MUTATORS, LISTS, STREAM, MCE) correspond to the sicp.<group> submodules above.

Compatibility

  • Requires Python 3.10+.
  • is_number mirrors Scheme's number? primitive as used in the textbook.
  • round, abs, len, max, min, str, repr, int, float, complex, bool, print, and input are plain CPython builtins, not reimplementations of Source Academy Python's versions, so a few edge cases differ:
    • arity() raises on a handful of CPython builtins whose signature isn't introspectable (e.g. arity(print), arity(max), arity(min), arity(str)), where Source Academy Python's own arity returns a value.
    • is_function(int) and is_function(str) are True here, since CPython's callable() treats classes as callable — Source Academy Python's tag-based check would say False.
    • max/min accept the single-iterable form (max([1, 2, 3])); Source Academy Python's versions require two or more direct arguments.

Source & development

This package lives in the py-slang repository under python/. py-slang is the reference implementation of Source Academy Python, so keeping the library here — next to the standard library it mirrors (src/stdlib/) — is what keeps the two from drifting apart. Each sicp.<group> submodule corresponds to a py-slang stdlib group.

cd python
python -m pip install -e .        # editable install for development
python -m pytest                  # or: PYTHONPATH=. python tests/test_sicp.py
python -m build                   # build the wheel + sdist

Releasing

Publishing to PyPI is not automatic on push or merge. The CI test job runs on every change under python/**, but the publish job runs only when a GitHub Release is published (.github/workflows/python-package.yml).

One-time setup: configure a PyPI Trusted Publisher for the sourceacademy-sicp project pointing at this repository and the python-package workflow (or swap the publish step for a token-based upload).

To cut a release:

  1. Bump version in pyproject.toml. PyPI rejects re-uploads of an existing version, so this must change every time.
  2. Merge to main.
  3. Create and publish a GitHub Release (a tag such as sicp-vX.Y.Z). Publishing it triggers the publish job, which builds the wheel + sdist and uploads them to PyPI.

License

Apache-2.0. Part of the Source Academy project.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sourceacademy_sicp-0.1.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sourceacademy_sicp-0.1.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file sourceacademy_sicp-0.1.0.tar.gz.

File metadata

  • Download URL: sourceacademy_sicp-0.1.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sourceacademy_sicp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dd25ea29d6cfe4d0ab21aed0ffee8332c8cd281817516caa1b6577ffe9a5d5c5
MD5 cd1cc78c687f20649d29f834d867e091
BLAKE2b-256 9cc01ddcc4ad4831d6e9a928210c272568ba9d4c6f1964e6f187fe23d0dd7957

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourceacademy_sicp-0.1.0.tar.gz:

Publisher: python-package.yml on source-academy/py-slang

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sourceacademy_sicp-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sourceacademy_sicp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c8d72bb0b25fc58e762373342dc31b053b9cb5ee4ad6822dca965537d37d8c95
MD5 4d8753b52906f5dbdeb311b7b41e3e5b
BLAKE2b-256 11a8e684ceb9622e8934f655dc9453de5c0f50d86ddd9bdac0a9fbcc8867534a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sourceacademy_sicp-0.1.0-py3-none-any.whl:

Publisher: python-package.yml on source-academy/py-slang

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page