core: more rearrangements; util: keyword_dict function
This commit is contained in:
parent
affe948296
commit
965543252f
3 changed files with 23 additions and 20 deletions
|
|
@ -26,6 +26,22 @@ type Handler = Callable[[Message, Scope],
|
||||||
type Handlers = Handler | None | list[Handlers]
|
type Handlers = Handler | None | list[Handlers]
|
||||||
type Actions = dict[Symbol, Handlers]
|
type Actions = dict[Symbol, Handlers]
|
||||||
|
|
||||||
|
def neuron(scope: Scope) -> Mailbox:
|
||||||
|
async def process(msg: Message) -> actor.Behavior | None:
|
||||||
|
nonlocal scope
|
||||||
|
scope = await scope.proc(msg, scope) or scope
|
||||||
|
return process
|
||||||
|
return actor.create(process)
|
||||||
|
|
||||||
|
def synapse(rcvr: Mailbox, op: SynOp = lambda x: x) -> Syn:
|
||||||
|
async def syn(msg: Message) -> None:
|
||||||
|
# op = update_op(msg) (=> return?)
|
||||||
|
# rv = check_return(msg) => return rv
|
||||||
|
msg_new = op(msg)
|
||||||
|
if msg_new is not None:
|
||||||
|
await rcvr.put(msg_new)
|
||||||
|
return syn
|
||||||
|
|
||||||
env_dummy: Mailbox = asyncio.Queue()
|
env_dummy: Mailbox = asyncio.Queue()
|
||||||
|
|
||||||
def notify() -> Handler:
|
def notify() -> Handler:
|
||||||
|
|
@ -58,22 +74,6 @@ def gen_program(spec: ProgramSpec, default: Proc = gen_proc({})) -> Program:
|
||||||
return proc
|
return proc
|
||||||
return program
|
return program
|
||||||
|
|
||||||
def neuron(scope: Scope) -> Mailbox:
|
|
||||||
async def process(msg: Message) -> actor.Behavior | None:
|
|
||||||
nonlocal scope
|
|
||||||
scope = await scope.proc(msg, scope) or scope
|
|
||||||
return process
|
|
||||||
return actor.create(process)
|
|
||||||
|
|
||||||
def synapse(rcvr: Mailbox, op: SynOp = lambda x: x) -> Syn:
|
|
||||||
async def syn(msg: Message) -> None:
|
|
||||||
# op = update_op(msg) (=> return?)
|
|
||||||
# rv = check_return(msg) => return rv
|
|
||||||
msg_new = op(msg)
|
|
||||||
if msg_new is not None:
|
|
||||||
await rcvr.put(msg_new)
|
|
||||||
return syn
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Scope:
|
class Scope:
|
||||||
|
|
||||||
|
|
@ -127,9 +127,8 @@ def basic_actions() -> Actions:
|
||||||
|
|
||||||
# predefined specialized procs
|
# predefined specialized procs
|
||||||
|
|
||||||
def std_proc(actions: Actions | None = None, finalize=forward(), **kw) -> Proc:
|
def std_proc(actions: Actions | None = None, finalize=forward(), **more) -> Proc:
|
||||||
if actions is None:
|
if actions is None:
|
||||||
act_add = dict((k(key), val) for key, val in kw.items())
|
actions = basic_actions() | util.keyword_dict(more)
|
||||||
actions = basic_actions() | act_add
|
|
||||||
return gen_proc(actions, finalize=finalize)
|
return gen_proc(actions, finalize=finalize)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,9 @@ class Symbols:
|
||||||
|
|
||||||
keywords = Symbols()
|
keywords = Symbols()
|
||||||
|
|
||||||
|
def keyword_dict(d):
|
||||||
|
return dict((keywords(key), val) for key, val in d.items())
|
||||||
|
|
||||||
# list / iterable utilities
|
# list / iterable utilities
|
||||||
|
|
||||||
def flatten(*it, ignore_none=False):
|
def flatten(*it, ignore_none=False):
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from scopes.util import keywords as k, flatten
|
from scopes.util import keywords as k, keyword_dict, flatten
|
||||||
|
|
||||||
def test_keywords():
|
def test_keywords():
|
||||||
assert k.dummy is k.dummy
|
assert k.dummy is k.dummy
|
||||||
|
|
@ -12,6 +12,7 @@ def test_keywords():
|
||||||
assert getattr(k, "abcd"[-2:]) is k.cd
|
assert getattr(k, "abcd"[-2:]) is k.cd
|
||||||
assert k('dummy') is k.dummy
|
assert k('dummy') is k.dummy
|
||||||
assert k('new') is k('new')
|
assert k('new') is k('new')
|
||||||
|
assert keyword_dict(dict(one=1, two=2)) == {k.one: 1, k.two: 2}
|
||||||
|
|
||||||
def test_flatten():
|
def test_flatten():
|
||||||
lst = (1, (None, 2), (3, (4, 5)))
|
lst = (1, (None, 2), (3, (4, 5)))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue