Python S-expression emulation using tuple-like objects.
Project description
etuples
Python S-expression emulation using tuple-like objects.
Examples
etuple
s 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))
etuple
s can also be evaluated:
>>> et.eval_obj
3
Evaluated etuple
s are cached:
>>> et = etuple(add, "a", "b")
>>> et.eval_obj
'ab'
>>> et.eval_obj is et.eval_obj
True
Reconstructed etuple
s 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file etuples-0.1.1.tar.gz
.
File metadata
- Download URL: etuples-0.1.1.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 511b0458d5fa3f426ba5a06dba249a59beddb67f1994b2808e7a6e86ff464db3 |
|
MD5 | 3322e2f6ad14ec692943a913ef837579 |
|
BLAKE2b-256 | 6e3f5b71c0099569e328b50bf0fd88c3dbbb2bceed4ae760471a1d7c6f7d2a80 |
Provenance
File details
Details for the file etuples-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: etuples-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 185ab49f79ba04c5af03073b1341c9aef3230c2d74d2ed6b8dd31a7d49dbeb7c |
|
MD5 | 12b79516ec836b122f8a66c404a2c214 |
|
BLAKE2b-256 | b3a874387b0380c42794ba58addd4edb6be9cf4e79c846a2b6a61eded52f80a5 |