Skip to content

Instantly share code, notes, and snippets.

@kaizu
Created June 9, 2021 09:02
Show Gist options
  • Save kaizu/755a9cf246cc26873f7d702565a0677c to your computer and use it in GitHub Desktop.
Save kaizu/755a9cf246cc26873f7d702565a0677c to your computer and use it in GitHub Desktop.
Untitled0.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Untitled0.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyMCI/hDqSR9byHbrYboaKnx",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kaizu/755a9cf246cc26873f7d702565a0677c/untitled0.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Gp85KQVp4KX4"
},
"source": [
"import copy, types, operator"
],
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "EgI-hP8-6WTg"
},
"source": [
"class _Process():\n",
" def __init__(self, name, logger):\n",
" self.name = name\n",
" self.logger = logger\n",
" self.ports = {}\n",
" \n",
" def __le__(self, pair):\n",
" name, value = pair\n",
" assert isinstance(value, Port)\n",
" value.parent = self\n",
" self.ports[name] = value\n",
"\n",
" def __ge__(self, value):\n",
" return operator.le(self, value)\n",
"\n",
" def __getattr__(self, name):\n",
" return self.ports[name]\n",
"\n",
"class Port():\n",
"\n",
" def __init__(self, kind, name):\n",
" self.kind = kind\n",
" self.name = name\n",
" self.parent = None\n",
" \n",
" def __lshift__(self, another):\n",
" self.parent.logger.append((dict(kind=self.kind, name=self.name, parent=self.parent.name), dict(kind=another.kind, name=another.name, parent=another.parent.name)))"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "1b-Qdijx3CsI"
},
"source": [
"def decorator(func):\n",
" vardict = copy.copy(func.__globals__)\n",
" func_code = func.__code__\n",
" name = func.__name__\n",
" defaults = func.__defaults__\n",
" logger = []\n",
" vardict['logger'] = logger\n",
" vardict['Process'] = lambda name: _Process(name, logger)\n",
" # vardict['Port'] = lambda kind, name: _Port(kind, name)\n",
" newfunc = types.FunctionType(func_code, vardict, name=name, argdefs=defaults)\n",
" def wrapped():\n",
" newfunc()\n",
" return logger\n",
" return wrapped"
],
"execution_count": 3,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Ejmh_VOJ3H3y"
},
"source": [
"@decorator\n",
"def test1():\n",
" s = Process('start')\n",
" s >= ('plate1', Port('physical', 'plate96'))\n",
"\n",
" p = Process('process')\n",
" p <= ('inplate', Port('physical', 'plate96'))\n",
"\n",
" p.inplate << s.plate1"
],
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Kb5ZawQY3LBv",
"outputId": "cddc1bd3-7810-405b-b0b5-70441c0a50dd"
},
"source": [
"test1()"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[({'kind': 'physical', 'name': 'plate96', 'parent': 'process'},\n",
" {'kind': 'physical', 'name': 'plate96', 'parent': 'start'})]"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "p5GGp6Ne90_5"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment