Pydialect makes Python into a language platform, à la Racket. It provides the plumbing that allows to create, in Python, dialects that compile into Python at import time.
An extension to the Python language doesn’t need to make it into the Python core, or even be desirable for inclusion into the Python core, in order to be useful.
Building on functions and syntactic macros, customization of the language itself is one more tool for the programmer to extract patterns, at a higher level. Hence, beside language experimentation, such extensions can be used as a framework that allows shorter and/or more readable programs.
Pydialect places language-creation power in the hands of its users, without the need to go to extreme lengths to hack CPython itself or implement from scratch a custom language that compiles to Python AST or bytecode.
Pydialect is geared toward creating languages that extend Python and look almost like Python, but extend or modify its syntax and/or semantics. Hence dialects.
That said, Pydialect itself is only a lightweight infrastructure hook that makes it convenient to define and use dialects. To implement the actual semantics for your dialect (which is where all the interesting things happen), you may want to look at [MacroPy](https://github.com/azazel75/macropy). Examples can be found in [unpythonic](https://github.com/Technologicat/unpythonic); see especially the macros (comprising about one half of unpythonic). On packaging a set of semantics into a Pydialect definition, look at the example dialects included in the Pydialect distribution.
Example of a module using a dialect:
from __lang__ import lispython
print("hello, my dialect is {}".format(__lang__))
c = cons(1, 2)
assert tuple(c) == (1, 2)
assert car(c) == 1
assert cdr(c) == 2
assert ll(1, 2, 3) == llist((1, 2, 3))
x = let[(a, 21) in 2*a]
assert x == 42
x = letseq[((a, 1),
(a, 2*a),
(a, 2*a)) in
a]
assert x == 4
a = lambda x: cond[x < 0, "nope",
x % 2 == 0, "even",
"odd"]
assert a(-1) == "nope"
assert a(2) == "even"
assert a(3) == "odd"
def fact(n):
def f(k, acc):
if k == 1:
return acc
f(k - 1, k*acc) # implicit return in tail position, like in Lisps
f(n, acc=1)
assert fact(4) == 24
fact(5000) # automatic TCO, no crash
square = lambda x: x**2
assert square(3) == 9
assert square.__name__ == "square" # lambdas are auto-named
Metadata
Release files for pydialect 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pydialect-0.1.1.tar.gz | 18.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydialect-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 39.6 kB
Release files / pydialect-0.1.1.tar.gz
| Download URL | pydialect-0.1.1.tar.gz |
|---|---|
| Size | 18.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
29cbba1bcd25223675ee640bd4df3d7587758b0dd7b06d7056e423e524e17684
|
|
BLAKE2b-256 checksum How to use checksums |
e744a8c6aa99afe909f391cf4ff54bf6633704018f646616c3c628315fdaa690
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.4.3
|
Release files / pydialect-0.1.1-py3-none-any.whl
| Download URL | pydialect-0.1.1-py3-none-any.whl |
|---|---|
| Size | 20.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fc386af3e8ae66cbc6d6716080924ae42987bc9a4d9a43939435d6f521e2edb
|
|
BLAKE2b-256 checksum How to use checksums |
05924db1c9879c5adfa473196f81ebdc59a497ebcb5e1d3189c14a8980a23b86
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.4.3
|