@ -284,36 +284,11 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 61 ,
"execution_count": 70 ,
"metadata": {
"metadata": {
"ExecuteTime": {
"ExecuteTime": {
"end_time": "2020-01-01T22:52:25.985499Z",
"end_time": "2020-01-02T07:23:44.234420Z",
"start_time": "2020-01-01T22:52:25.981463Z"
"start_time": "2020-01-02T07:23:37.004949Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'/home/v923z/sandbox/micropython/v1.11/micropython/ports/unix'"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pwd"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-01T22:54:39.509537Z",
"start_time": "2020-01-01T22:54:35.649034Z"
}
}
},
},
"outputs": [
"outputs": [
@ -357,6 +332,69 @@
"convert_notebook(notebook,'../../../usermod/docs/source/usermods_%02d.rst'%i)"
"convert_notebook(notebook,'../../../usermod/docs/source/usermods_%02d.rst'%i)"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## conf.py"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"ExecuteTime": {
"end_time": "2020-01-02T07:26:56.308977Z",
"start_time": "2020-01-02T07:26:56.288822Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting ../../../usermod/docs/source/conf.py\n"
]
}
],
"source": [
"%%writefile ../../../usermod/docs/source/conf.py\n",
"\n",
"# -- Project information -----------------------------------------------------\n",
"\n",
"project = 'micropython-usermod'\n",
"copyright = '2019-2020, Zoltán Vörös'\n",
"author = 'Zoltán Vörös'\n",
"\n",
"# The full version, including alpha/beta/rc tags\n",
"release = '1.41'\n",
"\n",
"# -- General configuration ---------------------------------------------------\n",
"\n",
"extensions = [\n",
"]\n",
"\n",
"# Add any paths that contain templates here, relative to this directory.\n",
"templates_path = ['_templates']\n",
"\n",
"exclude_patterns = []\n",
"\n",
"html_theme = 'sphinx_rtd_theme'\n",
"\n",
"html_static_path = ['_static']\n",
"\n",
"master_doc = 'index'\n",
"\n",
"author=u'Zoltán Vörös'\n",
"copyright=author\n",
"language='en'\n",
"\n",
"latex_documents = [\n",
"(master_doc, 'usermod.tex', 'Micropython usermod documentation', \n",
"'Zoltán Vörös', 'manual'),\n",
"]"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {},
"metadata": {},
@ -2926,11 +2964,11 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 5 6,
"execution_count": 67 ,
"metadata": {
"metadata": {
"ExecuteTime": {
"ExecuteTime": {
"end_time": "2020-01-01T22:30:55.469026 Z",
"end_time": "2020-01-02T07:02:06.806470 Z",
"start_time": "2020-01-01T22:30:55.462399 Z"
"start_time": "2020-01-02T07:02:06.755307 Z"
}
}
},
},
"outputs": [
"outputs": [
@ -2938,7 +2976,7 @@
"name": "stdout",
"name": "stdout",
"output_type": "stream",
"output_type": "stream",
"text": [
"text": [
"written 2199 bytes to /properties/properties.c\n"
"written 2205 bytes to /properties/properties.c\n"
]
]
}
}
],
],
@ -2977,9 +3015,9 @@
"\n",
"\n",
"STATIC MP_DEFINE_CONST_DICT(propertyclass_locals_dict, propertyclass_locals_dict_table);\n",
"STATIC MP_DEFINE_CONST_DICT(propertyclass_locals_dict, propertyclass_locals_dict_table);\n",
"\n",
"\n",
"STATIC void propertyclass_attr(mp_obj_t self, qstr attribute, mp_obj_t *destination) {\n",
"STATIC void propertyclass_attr(mp_obj_t self_in , qstr attribute, mp_obj_t *destination) {\n",
" if(attribute == MP_QSTR_x) {\n",
" if(attribute == MP_QSTR_x) {\n",
" destination[0] = propertyclass_x(self);\n",
" destination[0] = propertyclass_x(self_in );\n",
" }\n",
" }\n",
"}\n",
"}\n",
"\n",
"\n",
@ -3009,6 +3047,36 @@
"MP_REGISTER_MODULE(MP_QSTR_propertyclass, propertyclass_user_cmodule, MODULE_PROPERTYCLASS_ENABLED);"
"MP_REGISTER_MODULE(MP_QSTR_propertyclass, propertyclass_user_cmodule, MODULE_PROPERTYCLASS_ENABLED);"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before we compile the module, I would like to add two comments to what was said above. \n",
"\n",
"First, in the function that we assigned to `.attr`, \n",
"\n",
"```c\n",
"STATIC void propertyclass_attr(mp_obj_t self_in, qstr attribute, mp_obj_t *destination) {\n",
" if(attribute == MP_QSTR_x) {\n",
" destination[0] = propertyclass_x(self_in);\n",
" }\n",
"}\n",
"```\n",
"we called a function on `self_in`, `propertyclass_x()`, and assigned the results to `destination[0]`. However, this extra trip is not absolutely necessary: we could have equally done something along these lines:\n",
"\n",
"```c\n",
"STATIC void propertyclass_attr(mp_obj_t self_in, qstr attribute, mp_obj_t *destination) {\n",
" if(attribute == MP_QSTR_x) {\n",
" propertyclass_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
" destination[0] = mp_obj_new_float(self->x);\n",
" }\n",
"}\n",
"```\n",
"The case in point being that `destination[0]` is simply an `mp_obj_t` object, it does not matter, where and how it is produced. Since `self` is available to `propertyclass_attr`, if the property is simple, as above, one can save the function call, and do everything in place.\n",
"\n",
"Second, more examples on implementing properties can be found in [py/profile.c](https://github.com/micropython/micropython/blob/master/py/profile.c). Just look for the `.attr` string, and the associated functions!"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
@ -3032,8 +3100,8 @@
"execution_count": null,
"execution_count": null,
"metadata": {
"metadata": {
"ExecuteTime": {
"ExecuteTime": {
"end_time": "2020-01-01T22:31:24.701373 Z",
"end_time": "2020-01-02T07:02:49.386912 Z",
"start_time": "2020-01-01T22:31:00.93314 4Z"
"start_time": "2020-01-02T07:02:11.26413 4Z"
},
},
"scrolled": false
"scrolled": false
},
},
@ -3045,11 +3113,11 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": 5 9,
"execution_count": 6 9,
"metadata": {
"metadata": {
"ExecuteTime": {
"ExecuteTime": {
"end_time": "2020-01-01T22:31:30.733565 Z",
"end_time": "2020-01-02T07:23:00.816053 Z",
"start_time": "2020-01-01T22:31:30.722234 Z"
"start_time": "2020-01-02T07:23:00.458819 Z"
}
}
},
},
"outputs": [
"outputs": [