2024-heraklion-scientific-p.../code_snippets/factory_methods.ipynb

183 lines
3.9 KiB
Plaintext
Raw Permalink Normal View History

2024-08-27 14:52:41 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:14:28.926189Z",
"start_time": "2022-08-12T12:14:28.923089Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"import json\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:21:00.003395Z",
"start_time": "2022-08-12T12:20:59.997851Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"dict_ = {'a': 3.1, 'b': 4.2}\n",
"with open('my_class.json', 'w') as f:\n",
" json.dump(dict_, f)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:23:03.889266Z",
"start_time": "2022-08-12T12:23:03.883050Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"class MyClass:\n",
" \n",
" def __init__(self, a, b):\n",
" \"\"\"The basic constructor takes 'raw' values.\"\"\"\n",
" self.a = a\n",
" self.b = b\n",
" \n",
" @classmethod\n",
" def from_random_values(cls, random_state=np.random):\n",
" \"\"\"Create a MyClass instance with random parameters.\"\"\"\n",
" a = random_state.rand()\n",
" b = random_state.randn()\n",
" return cls(a, b)\n",
" \n",
" @classmethod\n",
" def from_json(cls, json_fname):\n",
" \"\"\"Create a MyClass instance with parameters read form a json file.\"\"\"\n",
" with open(json_fname, 'r') as f:\n",
" dict_ = json.load(f)\n",
" a = dict_['a']\n",
" b = dict_['b']\n",
" return cls(a, b)\n",
"\n",
"my_class = MyClass.from_random_values()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:23:12.242599Z",
"start_time": "2022-08-12T12:23:12.237477Z"
},
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 0.842940228048758, 'b': 0.2797222990193814}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_class = MyClass.from_random_values()\n",
"my_class.__dict__"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"ExecuteTime": {
"end_time": "2022-08-12T12:23:44.439726Z",
"start_time": "2022-08-12T12:23:44.432540Z"
},
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 3.1, 'b': 4.2}"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_class = MyClass.from_json('my_class.json')\n",
"my_class.__dict__"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python [conda env:bog]",
"language": "python",
"name": "conda-env-bog-py"
},
"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.6.5"
},
"toc": {
"nav_menu": {
"height": "12px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}