Created
March 7, 2022 09:48
-
-
Save mauicv/4fbf29cc57fdc0c0b1b033c033d4832b to your computer and use it in GitHub Desktop.
recursive-config-example.ipynb
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "recursive-config-example.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"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/mauicv/4fbf29cc57fdc0c0b1b033c033d4832b/recursive-config-example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "VvzoAKnPq-9s" | |
}, | |
"outputs": [], | |
"source": [ | |
"class Object1:\n", | |
" def __init__(self, attr1, attr2, obj1, obj2):\n", | |
" self.attr1 = attr1\n", | |
" self.attr2 = attr2\n", | |
" self.obj1 = obj1\n", | |
" self.obj2 = obj2\n", | |
"\n", | |
" def serialize(self):\n", | |
" return {'cls': 'Object1', **self.__dict__}\n", | |
"\n", | |
"\n", | |
"class Object2:\n", | |
" def __init__(self, attr1, attr2):\n", | |
" self.attr1 = attr1\n", | |
" self.attr2 = attr2\n", | |
"\n", | |
" def serialize(self):\n", | |
" return {'cls': 'Object2', **self.__dict__}\n", | |
"\n", | |
"\n", | |
"class Object3:\n", | |
" def __init__(self, attr1, attr2):\n", | |
" self.attr1 = attr1\n", | |
" self.attr2 = attr2\n", | |
"\n", | |
" def serialize(self):\n", | |
" return {'cls': 'Object3', **self.__dict__}\n", | |
"\n", | |
"\n", | |
"def serialize(obj):\n", | |
" config = {}\n", | |
" for key, value in obj.serialize().items():\n", | |
" if hasattr(value, 'serialize'):\n", | |
" config[key] = serialize(value)\n", | |
" else:\n", | |
" config[key] = value\n", | |
" return config\n", | |
"\n", | |
"\n", | |
"def deserialize(serial_obj):\n", | |
" parent_cls = globals()[serial_obj.get('cls', None)]\n", | |
" for key, value in serial_obj.items():\n", | |
" if isinstance(value, dict) and value.get('cls', None):\n", | |
" serial_obj[key] = deserialize(value)\n", | |
" del serial_obj['cls']\n", | |
" return parent_cls(**serial_obj)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"o1 = Object2(1, 2)\n", | |
"o2 = Object3(2, 1)\n", | |
"first_obj = Object1('a', 'b', o1, o2)\n", | |
"serialized_obj = serialize(first_obj)\n", | |
"print('----')\n", | |
"print(serialized_obj)\n", | |
"second_obj = deserialize(serialized_obj)\n", | |
"print('----')\n", | |
"print(second_obj.__dict__)\n", | |
"print(second_obj.obj2.__dict__)\n", | |
"print(second_obj.obj1.__dict__)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "mMsXrqp5rDSi", | |
"outputId": "b885ac6e-7ff3-4d05-b4ce-9fda0c274909" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"----\n", | |
"{'cls': 'Object1', 'attr1': 'a', 'attr2': 'b', 'obj1': {'cls': 'Object2', 'attr1': 1, 'attr2': 2}, 'obj2': {'cls': 'Object3', 'attr1': 2, 'attr2': 1}}\n", | |
"----\n", | |
"{'attr1': 'a', 'attr2': 'b', 'obj1': <__main__.Object2 object at 0x7f714c7b9dd0>, 'obj2': <__main__.Object3 object at 0x7f714c7bee50>}\n", | |
"{'attr1': 2, 'attr2': 1}\n", | |
"{'attr1': 1, 'attr2': 2}\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"" | |
], | |
"metadata": { | |
"id": "Z-kFBrbGAdBM" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment