Skip to main content

Link functions up into callable objects (DAGs)

Project description

meshed

Link functions up into callable objects (DAGs)

To install: pip install meshed

Quick Start

from meshed import DAG

def this(a, b=1):
    return a + b
def that(x, b=1):
    return x * b
def combine(this, that):
    return (this, that)

dag = DAG((this, that, combine))
print(dag.synopsis_string())
x,b -> that_ -> that
a,b -> this_ -> this
this,that -> combine_ -> combine

But what does it do?

It's a callable, with a signature:

from inspect import signature
signature(dag)
<Signature (x, a, b=1)>

And when you call it, it executes the dag from the root values you give it and returns the leaf output values.

dag(1, 2, 3)  # (a+b,x*b) == (2+3,1*3) == (5, 3)
(5, 3)
dag(1, 2)  # (a+b,x*b) == (2+1,1*1) == (3, 1)
(3, 1)

You can see (and save image, or ascii art) the dag:

dag.dot_digraph()

You can extend a dag

dag2 = DAG([*dag, lambda this, a: this + a])
dag2.dot_digraph()

You can get a sub-dag by specifying desired input(s) and outputs.

dag2[['that', 'this'], 'combine'].dot_digraph()

Note on flexibility

The above DAG was created straight from the functions, using only the names of the functions and their arguments to define how to hook the network up.

But if you didn't write those functions specifically for that purpose, or you want to use someone else's functions, we got you covered.

You can define the name of the node (the name argument), the name of the output (the out argument) and a mapping from the function's arguments names to "network names" (through the bind argument). The edges of the DAG are defined by matching out TO bind.

itools module

Tools that enable operations on graphs where graphs are represented by an adjacency Mapping.

Again.

Graphs: You know them. Networks. Nodes and edges, and the ecosystem descriptive or transformative functions surrounding these. Few languages have builtin support for the graph data structure, but all have their libraries to compensate.

The one you're looking at focuses on the representation of a graph as Mapping encoding its adjacency list. That is, a dictionary-like interface that specifies the graph by specifying for each node what nodes it's adjacent to:

assert graph[source_node] == iterator_of_nodes_that_source_node_has_edges_to

We emphasize that there is no specific graph instance that you need to squeeze your graph into to be able to use the functions of meshed. Suffices that your graph's structure is expressed by that dict-like interface -- which grown-ups call Mapping (see the collections.abc or typing standard libs for more information).

You'll find a lot of Mappings around pythons. And if the object you want to work with doesn't have that interface, you can easily create one using one of the many tools of py2store meant exactly for that purpose.

Examples

>>> from meshed.itools import edges, nodes, isolated_nodes
>>> graph = dict(a='c', b='ce', c='abde', d='c', e=['c', 'b'], f={})
>>> sorted(edges(graph))
[('a', 'c'), ('b', 'c'), ('b', 'e'), ('c', 'a'), ('c', 'b'), ('c', 'd'), ('c', 'e'), ('d', 'c'), ('e', 'b'), ('e', 'c')]
>>> sorted(nodes(graph))
['a', 'b', 'c', 'd', 'e', 'f']
>>> set(isolated_nodes(graph))
{'f'}
>>>
>>> from meshed.makers import edge_reversed_graph
>>> g = dict(a='c', b='cd', c='abd', e='')
>>> assert edge_reversed_graph(g) == {'c': ['a', 'b'], 'd': ['b', 'c'], 'a': ['c'], 'b': ['c'], 'e': []}
>>> reverse_g_with_sets = edge_reversed_graph(g, set, set.add)
>>> assert reverse_g_with_sets == {'c': {'a', 'b'}, 'd': {'b', 'c'}, 'a': {'c'}, 'b': {'c'}, 'e': set([])}

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

meshed-0.1.12.tar.gz (45.1 kB view details)

Uploaded Source

Built Distribution

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

meshed-0.1.12-py3-none-any.whl (48.7 kB view details)

Uploaded Python 3

File details

Details for the file meshed-0.1.12.tar.gz.

File metadata

  • Download URL: meshed-0.1.12.tar.gz
  • Upload date:
  • Size: 45.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for meshed-0.1.12.tar.gz
Algorithm Hash digest
SHA256 2fb6794a7f9caf2faa7078ff543f60534492c487bfe75bc41e809578f1eb693d
MD5 1c8d6453de88db41ea7cf1b372f68c2e
BLAKE2b-256 bc0097a3672296035563c6c6ba042eb668b82c939dfb96963f7bbdf93d34ad27

See more details on using hashes here.

File details

Details for the file meshed-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: meshed-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 48.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for meshed-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 fccfe7c383e1abe3ab6cf5de662eb4dd1bf6aac9fcebd6e6cd32156887e5de7b
MD5 8919f74d82a64da26faad99982953195
BLAKE2b-256 c7a3037e00d1d9a5fba2f327ddd650aa053d015ce23eb20d6970d1a7e16e0a17

See more details on using hashes here.

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