-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8081442
commit ef1e3ed
Showing
15 changed files
with
146 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
!print Hello world! | ||
!echo Hello world! |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
!cat examples/shell.yaml: | ||
? !wc -c | ||
? !grep grep: !wc -c | ||
? !tail -2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
setuptools.setup( | ||
name="widip", | ||
version="0.0.1", | ||
version="0.0.2", | ||
author="Martin Coll", | ||
author_email="[email protected]", | ||
description="Widip is an interactive environment for computing with wiring diagrams in modern systems", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import sys | ||
import warnings | ||
|
||
from .watch import watch_main, shell_main, stream_main | ||
from .watch import watch_main, shell_main | ||
from .widish import widish_main | ||
|
||
|
||
warnings.filterwarnings("ignore") | ||
match sys.argv: | ||
case [_]: | ||
watch_main() | ||
shell_main("bin/yaml/shell.yaml") | ||
case [_, file_name]: stream_main(open(file_name)) | ||
case [_, file_name]: widish_main(file_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from multiprocessing import Pipe | ||
from pathlib import Path | ||
from subprocess import PIPE, Popen | ||
import sys | ||
|
||
from discopy.frobenius import Category, Functor, Ty, Box | ||
from discopy.frobenius import Hypergraph as H, Id, Spider | ||
from discopy import python | ||
|
||
from .files import stream_diagram | ||
|
||
|
||
|
||
class IORun(python.Function): | ||
@classmethod | ||
def spiders(cls, n_legs_in: int, n_legs_out: int, typ: Ty): | ||
def step(*processes: IOProcess): | ||
""" | ||
A many-to-many pipe. | ||
""" | ||
assert typ == Ty("io") | ||
assert len(processes) == n_legs_in | ||
return tuple( | ||
IOSpiderProcess(*processes) | ||
for _ in range(n_legs_out)) | ||
return IORun( | ||
inside=step, | ||
dom=Ty(*("io" for _ in range(n_legs_in))), | ||
cod=Ty(*("io" for _ in range(n_legs_out)))) | ||
|
||
|
||
def command_io_f(diagram): | ||
""" | ||
close input parameters (constants) | ||
drop outputs matching input parameters | ||
all boxes are io->[io]""" | ||
def command_io(b): | ||
return Id().tensor(*( | ||
Id("io") @ Spider(0, 1, t) >> | ||
Box(b.name, | ||
Ty("io") @ t, | ||
Ty("io")) >> | ||
H.spiders(1, len(b.cod), Ty("io")).to_diagram() | ||
for t in b.dom)) | ||
f = Functor( | ||
lambda x: Ty("io"), | ||
lambda b: command_io(b),) | ||
diagram = f(diagram) | ||
return ( | ||
# H.spiders(0, len(diagram.dom), Ty("io")).to_diagram() >> | ||
diagram >> | ||
H.spiders(len(diagram.cod), 1, Ty("io")).to_diagram()) | ||
|
||
|
||
class IOProcess: | ||
"""mutual with IOSpiderProcess""" | ||
def __init__(self, box, *spider_processes): | ||
assert box.dom[0] == Ty("io") | ||
assert box.cod == Ty("io") | ||
self.spider_processes = spider_processes | ||
self.popen_args = [box.name, *(t.name for t in box.dom[1:])] | ||
self.pipe_in, self.pipe_out = Pipe(duplex=False) | ||
|
||
def communicate(self, input): | ||
# print("process:", self.spider_processes) | ||
# i = self.pipe_in.recv() | ||
o = "" | ||
for p in self.spider_processes: | ||
o += p.communicate(input) | ||
# print(o, self.popen_args) | ||
popen = Popen(self.popen_args, stdin=PIPE, stdout=PIPE, text=True) | ||
(o, _) = popen.communicate(o) | ||
# print(o, self.popen_args) | ||
return o | ||
|
||
|
||
class IOSpiderProcess: | ||
""" | ||
takes many processes and creates n copies of their joined outputs | ||
mutual with IOSpider""" | ||
def __init__(self, *processes: IOProcess): | ||
self.processes = processes | ||
self.pipe_in, self.pipe_out = Pipe(duplex=False) | ||
|
||
def communicate(self, input): | ||
# print("spiderprocess:", [p.popen for p in self.processes]) | ||
w = "" | ||
for p in self.processes: | ||
o = p.communicate(input) | ||
w += o | ||
return w | ||
|
||
shell_f = Functor( | ||
lambda x: Ty("io"), | ||
lambda b: lambda *p: IOProcess(b, *p), | ||
cod=Category(Ty, IORun)) | ||
|
||
|
||
def widish_main(file_name): | ||
path = Path(file_name) | ||
diagram = stream_diagram(path.open()) | ||
diagram = command_io_f(diagram) | ||
diagram.draw(path=path.with_suffix(".jpg")) | ||
process: IOSpiderProcess = shell_f(diagram)() | ||
r = process.communicate("") | ||
sys.stdout.write(r) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.