Skip to main content

Draw little expression graphs; made to be hacked on.

Project description

expr

Create simple visualisations of mathematical operations on small datasets by rendering an expression graph, show your friends or serialise it for later.

Contents

Usage

Examples follow using the Python interactive shell

Starting out

Import some things from the module

from expr import Expr, NumExpr

Construct an expression

expr = Expr(
    operation_name='+',
    arguments=[
        NumExpr(number=1),
        Expr(
            operation_name='/',
            arguments=[
                NumExpr(number=2),
                NumExpr(number=3),
            ]
        )
    ]
)

Get an answer

>> expr.resolve()
1.6666666666666665

Draw a graph

>> graph = expr.graph()
>> graph.write_png('example.png')
True

example.png

example

Less verbosity

Import things using as to save your typing fingers by aliasing those characters away

>>> from expr import Expr as E, NumExpr as N
>>> expr = E('/', [N(22), N(7)])
>>> expr.resolve()
3.142857142857143
>>> expr.graph().write_png('pi.png')
True

pi.png

pi

Getting pandas involved

We can create expressions that involve more than just numbers …

>>> import pandas
>>> from expr import (
...     Expr as E,
...     NumExpr as N,
...     DataFrameExpr as D,
... )

Create some stupid datasets

>>> def two_by_four():
...     data = [(n + 1, n + 1) for n in range(4)]
...     return pandas.DataFrame.from_records(data=data, columns=['a', 'b'])

>>> df_A = two_by_four()
>>> df_B = two_by_four()
>>> df_A
   a  b
0  1  1
1  2  2
2  3  3
3  4  4

Create the expression object, the DataFrameExpr object (aliased here as D) takes an optional argument name which will be used as a label if present, otherwise an automatically generated label will applied.

>>> expr = E('*', [N(3), E('+', [D(df_A, 'A'), D(df_B, 'B')])])
>>> expr.graph().write_png('dataframe.png')
True
>>> expr.resolve()
    0   1
0   6   6
1  12  12
2  18  18
3  24  24

dataframe.png

dataframe

Known Issues

If you like YAML, you may encounter some issues serialising pandas objects, but JSON should be fine.

Also

Colours courtesy of clrs.cc

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

expr-0.0.1f.tar.gz (4.9 kB view hashes)

Uploaded Source

Supported by

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