Skip to content

Instantly share code, notes, and snippets.

@Carreau
Created November 10, 2022 09:30
Show Gist options
  • Save Carreau/62350dabcd75b90eccffb9fca7c2c431 to your computer and use it in GitHub Desktop.
Save Carreau/62350dabcd75b90eccffb9fca7c2c431 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "9c309f4a",
"metadata": {},
"source": [
"In this notebook we try to rewrite the serialiser to comform to what Myst Is doing:\n",
"\n",
"https://spec.myst.tools/spec/overview\n",
"\n",
"\n",
"In particular the tags will be _in_ the datastructure and not outside. "
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "5dcd2f00",
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from typing import List, Optional, Union\n",
"base_types = {int, str, bool, type(None)}\n",
"from typing import get_type_hints as gth\n",
"\n",
"@dataclass\n",
"class Bar:\n",
" x:str\n",
" y:Union[int, bool]\n",
"\n",
"@dataclass\n",
"class Foo:\n",
" a:int\n",
" b:List[int]\n",
" c:Bar\n",
" \n",
"f = Foo(1, [1,3], Bar(\"str\", False) ) \n",
"\n",
"def serialize(instance, annotation):\n",
" \n",
"\n",
" if (annotation in base_types):\n",
" print('BASE', instance)\n",
" assert isinstance(instance, annotation), f\"{instance} {annotation}\"\n",
" return instance\n",
"\n",
" origin = getattr(annotation, \"__origin__\", None)\n",
" if origin is list:\n",
" assert isinstance(instance, origin)\n",
" inner_annotation = annotation.__args__\n",
" # assert len(inner_annotation) == 1, inner_annotation\n",
" return [serialize(x, inner_annotation[0]) for x in instance]\n",
" if origin is dict:\n",
" assert isinstance(instance, origin)\n",
" key_annotation, value_annotation = annotation.__args__\n",
" # assert key_annotation == str, key_annotation\n",
" return {k: serialize(v, value_annotation) for k, v in instance.items()}\n",
" if getattr(annotation, \"__origin__\", None) is Union:\n",
"\n",
" inner_annotation = annotation.__args__\n",
" if len(inner_annotation) == 2 and inner_annotation[1] == type(None):\n",
" # assert inner_annotation[0] is not None\n",
" # here we are optional; we _likely_ can avoid doing the union trick and store just the type, or null\n",
" if instance is None:\n",
" return None\n",
" else:\n",
" return serialize(instance, inner_annotation[0])\n",
" assert (\n",
" type(instance) in inner_annotation\n",
" ), f\"{type(instance)} not in {inner_annotation}, {instance} or type {type(instance)}\"\n",
" ma = [x for x in inner_annotation if type(instance) is x]\n",
" # assert len(ma) == 1\n",
" ann_ = ma[0]\n",
" return {\"type\": ann_.__name__, \"data\": serialize(instance, ann_)}\n",
" if (\n",
" (type(annotation) is type)\n",
" and type.__module__ not in (\"builtins\", \"typing\")\n",
" and (instance.__class__.__name__ == getattr(annotation, \"__name__\", None))\n",
" or type(instance) == annotation\n",
" ):\n",
" data = {}\n",
" data['type'] = type(instance).__name__\n",
" for k, ann in gth(type(instance)).items():\n",
" data[k] = serialize(getattr(instance, k), ann)\n",
" return data\n",
" print(instance, (type(annotation) is type), type.__module__, type(instance) == annotation)\n",
" \n",
"\n",
"\n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "f3fa7f1d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'builtins'"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type.__module__"
]
},
{
"cell_type": "code",
"execution_count": 95,
"id": "0879d9fa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BASE 1\n",
"BASE 1\n",
"BASE 3\n",
"BASE str\n",
"BASE False\n"
]
},
{
"data": {
"text/plain": [
"{'type': 'Foo',\n",
" 'a': 1,\n",
" 'b': [1, 3],\n",
" 'c': {'type': 'Bar', 'x': 'str', 'y': {'type': 'bool', 'data': False}}}"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"serialize(f, Foo)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "0ccd42d1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 9,
"id": "cfab80d8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Foo'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "57542ac2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment