work in progress: core package: add Scope class, rearrange tests, add type annotations
This commit is contained in:
parent
353109e105
commit
9aebbff176
9 changed files with 50 additions and 23 deletions
1
apidemo/__init__.py
Normal file
1
apidemo/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# py-scopes: apidemo
|
||||||
2
demo/__init__.py
Normal file
2
demo/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
# py-scopes: demo
|
||||||
|
|
||||||
|
|
@ -1 +1,4 @@
|
||||||
"""package scopes.core"""
|
"""package scopes.core"""
|
||||||
|
|
||||||
|
from .core import *
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ async def loop(q, bhv):
|
||||||
def create(bhv, group=None):
|
def create(bhv, group=None):
|
||||||
q = asyncio.Queue()
|
q = asyncio.Queue()
|
||||||
if group is None:
|
if group is None:
|
||||||
return q, asyncio.create_task(loop(q, bhv))
|
asyncio.create_task(loop(q, bhv))
|
||||||
else:
|
else:
|
||||||
group.create_task(loop(q, bhv))
|
group.create_task(loop(q, bhv))
|
||||||
return q
|
return q
|
||||||
|
|
|
||||||
22
scopes/core/core.py
Normal file
22
scopes/core/core.py
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
# scopes.core.core
|
||||||
|
|
||||||
|
"""The scopes.core package"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from types import CoroutineType, FunctionType
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from scopes.util import keywords as k, Symbol
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Scope:
|
||||||
|
|
||||||
|
value: Any = 0
|
||||||
|
categ: tuple[Symbol, Symbol] = (k.csys, k.c00)
|
||||||
|
stage: Symbol = k.initial
|
||||||
|
proc: CoroutineType | None = None
|
||||||
|
program: dict[tuple[Symbol, Symbol], CoroutineType] = {}
|
||||||
|
syns: list[FunctionType] = []
|
||||||
|
environ: asyncio.Queue | None = None
|
||||||
|
|
||||||
|
|
@ -6,8 +6,8 @@ from scopes.util import keywords as k
|
||||||
|
|
||||||
class Record:
|
class Record:
|
||||||
|
|
||||||
headFields = () # to be defined by subclass
|
headFields: tuple = () # to be defined by subclass
|
||||||
headFieldsAsKeywords = set()
|
headFieldsAsKeywords: set[str] = set()
|
||||||
defaultHeadValue = ''
|
defaultHeadValue = ''
|
||||||
|
|
||||||
def __init__(self, *headValues, data=None, **kw):
|
def __init__(self, *headValues, data=None, **kw):
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
# scopes.testing
|
# scopes.testing
|
||||||
|
|
||||||
"""Common stuff (config, fixtures, ...) for testing"""
|
"""Common stuff (config, fixtures, ...) for testing"""
|
||||||
|
|
||||||
import os, sys
|
import os, sys
|
||||||
sys.path = [os.path.dirname(__file__)] + sys.path
|
sys.path = [os.path.dirname(__file__)] + sys.path
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
# py-scopes: tests.test_actor
|
# py-scopes: tests.test_core
|
||||||
|
|
||||||
""" Tests for scopes.core.actor"""
|
""" Tests for scopes.core modules, including e.g. actor, message"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from scopes import core
|
||||||
from scopes.core import actor, message
|
from scopes.core import actor, message
|
||||||
from scopes.util import keywords as k
|
from scopes.util import keywords as k
|
||||||
|
|
||||||
|
|
@ -13,6 +14,7 @@ collector = []
|
||||||
def show(msg):
|
def show(msg):
|
||||||
collector.append(msg)
|
collector.append(msg)
|
||||||
|
|
||||||
|
|
||||||
async def handle_basic(msg):
|
async def handle_basic(msg):
|
||||||
match msg:
|
match msg:
|
||||||
case k.show: show(msg)
|
case k.show: show(msg)
|
||||||
|
|
@ -38,8 +40,20 @@ async def test_actor():
|
||||||
await q2.put(k.show)
|
await q2.put(k.show)
|
||||||
await q1.put(k.quit)
|
await q1.put(k.quit)
|
||||||
await q2.put(k.quit)
|
await q2.put(k.quit)
|
||||||
#await asyncio.wait([t1, t2])
|
#print(collector)
|
||||||
print(collector)
|
|
||||||
assert len(collector) == 6
|
assert len(collector) == 6
|
||||||
assert collector[-1] == 'actor 1: Goodbye'
|
assert collector[-1] == 'actor 1: Goodbye'
|
||||||
|
|
||||||
|
|
||||||
|
def test_message():
|
||||||
|
msg = message.Message(k.test, k.data, 'person', '4711',
|
||||||
|
data=dict(firstName='Bob', lastName='Miller'))
|
||||||
|
assert msg.head['class'] is k.person
|
||||||
|
assert msg.addr() == (k.test, k.person, '4711')
|
||||||
|
assert msg.categ() == (k.test, k.person)
|
||||||
|
|
||||||
|
|
||||||
|
def test_core():
|
||||||
|
sc = core.Scope()
|
||||||
|
assert sc.value == 0
|
||||||
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
# py-scopes: tests.test_message
|
|
||||||
|
|
||||||
"""Tests for messages and related stuff"""
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from scopes.util import keywords as k
|
|
||||||
|
|
||||||
def test_message():
|
|
||||||
from scopes.core.message import Message
|
|
||||||
msg = Message(k.test, k.data, 'person', '4711',
|
|
||||||
data=dict(firstName='Bob', lastName='Miller'))
|
|
||||||
assert msg.head['class'] is k.person
|
|
||||||
assert msg.addr() == (k.test, k.person, '4711')
|
|
||||||
assert msg.categ() == (k.test, k.person)
|
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue