etuples
Python S-expression emulation using tuple-like objects.
Examples
etuples are like tuples:
>>> from operator import add
>>> from etuples import etuple, etuplize
>>> et = etuple(add, 1, 2)
>>> et
ExpressionTuple((<built-in function add>, 1, 2))
>>> from IPython.lib.pretty import pprint
>>> pprint(et)
e(<function _operator.add(a, b, /)>, 1, 2)
>>> et[0:2]
ExpressionTuple((<built-in function add>, 1))
etuples can also be evaluated:
>>> et.eval_obj
3
Evaluated etuples are cached:
>>> et = etuple(add, "a", "b")
>>> et.eval_obj
'ab'
>>> et.eval_obj is et.eval_obj
True
Reconstructed etuples and their evaluation results are preserved across tuple operations:
>>> et_new = (et[0],) + et[1:]
>>> et_new is et
True
>>> et_new.eval_obj is et.eval_obj
True
rator, rands, and apply will return the operator, the operands, and apply the operation to the operands:
>>> from etuples import rator, rands, apply
>>> et = etuple(add, 1, 2)
>>> rator(et)
<built-in function add>
>>> rands(et)
ExpressionTuple((1, 2))
>>> apply(rator(et), rands(et))
3
rator and rands are multipledispatch functions that can be extended to handle arbitrary objects:
from etuples.core import ExpressionTuple
from collections.abc import Sequence
class Node:
def __init__(self, rator, rands):
self.rator, self.rands = rator, rands
def __eq__(self, other):
return self.rator == other.rator and self.rands == other.rands
class Operator:
def __init__(self, op_name):
self.op_name = op_name
def __call__(self, *args):
return Node(Operator(self.op_name), args)
def __repr__(self):
return self.op_name
def __eq__(self, other):
return self.op_name == other.op_name
rands.add((Node,), lambda x: x.rands)
rator.add((Node,), lambda x: x.rator)
@apply.register(Operator, (Sequence, ExpressionTuple))
def apply_Operator(rator, rands):
return Node(rator, rands)
>>> mul_op, add_op = Operator("*"), Operator("+")
>>> mul_node = Node(mul_op, [1, 2])
>>> add_node = Node(add_op, [mul_node, 3])
etuplize will convert non-tuple objects into their corresponding etuple form:
>>> et = etuplize(add_node)
>>> pprint(et)
e(+, e(*, 1, 2), 3)
>>> et.eval_obj is add_node
True
etuplize can also do shallow to object-to-etuple conversions:
>>> et = etuplize(add_node, shallow=True)
>>> pprint(et)
e(+, <__main__.Node at 0x7f347361a080>, 3)
Installation
Using pip:
pip install etuples
To install from source:
git clone git@github.com:pythological/etuples.git
cd etuples
pip install -r requirements.txt
Tests can be run with the provided Makefile:
make check
Metadata
Release files for etuples 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| etuples-0.1.0.tar.gz | 21.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| etuples-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.2 kB
Release files / etuples-0.1.0.tar.gz
| Download URL | etuples-0.1.0.tar.gz |
|---|---|
| Size | 21.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7814ede7a0f9b11965f3bee96526c0f57634dfea86810fe1396e8597ff7ff26a
|
|
BLAKE2b-256 checksum How to use checksums |
d96d4670d4cb422c9ce9d08486a933bb14ed153fb5c31f85440cf0ee3949233b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5
|
Release files / etuples-0.1.0-py3-none-any.whl
| Download URL | etuples-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce939a46819b7ec6825d9217d173e9eab3c15df2215be559810f057685e5ba9a
|
|
BLAKE2b-256 checksum How to use checksums |
44a0432f7a7759781148bfed872962ad041f19732f16956a91800fb53c37bb6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2.post20191203 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5
|