Browse Source

extended the section on properties

pull/8/head
Zoltán Vörös 5 years ago
parent
commit
af8f36b4aa
  1. 146
      docs/micropython-usermod.ipynb
  2. 36
      docs/source/conf.py
  3. 41
      docs/source/usermods_08.rst
  4. 4
      snippets/properties/properties.c

146
docs/micropython-usermod.ipynb

@ -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": 56, "execution_count": 67,
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2020-01-01T22:30:55.469026Z", "end_time": "2020-01-02T07:02:06.806470Z",
"start_time": "2020-01-01T22:30:55.462399Z" "start_time": "2020-01-02T07:02:06.755307Z"
} }
}, },
"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.701373Z", "end_time": "2020-01-02T07:02:49.386912Z",
"start_time": "2020-01-01T22:31:00.933144Z" "start_time": "2020-01-02T07:02:11.264134Z"
}, },
"scrolled": false "scrolled": false
}, },
@ -3045,11 +3113,11 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 69,
"metadata": { "metadata": {
"ExecuteTime": { "ExecuteTime": {
"end_time": "2020-01-01T22:31:30.733565Z", "end_time": "2020-01-02T07:23:00.816053Z",
"start_time": "2020-01-01T22:31:30.722234Z" "start_time": "2020-01-02T07:23:00.458819Z"
} }
}, },
"outputs": [ "outputs": [

36
docs/source/conf.py

@ -1,19 +1,3 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# http://www.sphinx-doc.org/en/master/config
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
# -- Project information ----------------------------------------------------- # -- Project information -----------------------------------------------------
@ -22,36 +6,20 @@ copyright = '2019-2020, Zoltán Vörös'
author = 'Zoltán Vörös' author = 'Zoltán Vörös'
# The full version, including alpha/beta/rc tags # The full version, including alpha/beta/rc tags
release = '1.4' release = '1.41'
# -- General configuration --------------------------------------------------- # -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [ extensions = [
] ]
# Add any paths that contain templates here, relative to this directory. # Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates'] templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = [] exclude_patterns = []
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme' html_theme = 'sphinx_rtd_theme'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static'] html_static_path = ['_static']
master_doc = 'index' master_doc = 'index'
@ -63,4 +31,4 @@ language='en'
latex_documents = [ latex_documents = [
(master_doc, 'usermod.tex', 'Micropython usermod documentation', (master_doc, 'usermod.tex', 'Micropython usermod documentation',
'Zoltán Vörös', 'manual'), 'Zoltán Vörös', 'manual'),
] ]

41
docs/source/usermods_08.rst

@ -706,9 +706,9 @@ https://github.com/v923z/micropython-usermod/tree/master/snippets/properties/pro
STATIC MP_DEFINE_CONST_DICT(propertyclass_locals_dict, propertyclass_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(propertyclass_locals_dict, propertyclass_locals_dict_table);
STATIC void propertyclass_attr(mp_obj_t self, qstr attribute, mp_obj_t *destination) { STATIC void propertyclass_attr(mp_obj_t self_in, qstr attribute, mp_obj_t *destination) {
if(attribute == MP_QSTR_x) { if(attribute == MP_QSTR_x) {
destination[0] = propertyclass_x(self); destination[0] = propertyclass_x(self_in);
} }
} }
@ -737,6 +737,43 @@ https://github.com/v923z/micropython-usermod/tree/master/snippets/properties/pro
MP_REGISTER_MODULE(MP_QSTR_propertyclass, propertyclass_user_cmodule, MODULE_PROPERTYCLASS_ENABLED); MP_REGISTER_MODULE(MP_QSTR_propertyclass, propertyclass_user_cmodule, MODULE_PROPERTYCLASS_ENABLED);
Before we compile the module, I would like to add two comments to what
was said above.
First, in the function that we assigned to ``.attr``,
.. code:: c
STATIC void propertyclass_attr(mp_obj_t self_in, qstr attribute, mp_obj_t *destination) {
if(attribute == MP_QSTR_x) {
destination[0] = propertyclass_x(self_in);
}
}
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:
.. code:: c
STATIC void propertyclass_attr(mp_obj_t self_in, qstr attribute, mp_obj_t *destination) {
if(attribute == MP_QSTR_x) {
propertyclass_obj_t *self = MP_OBJ_TO_PTR(self_in);
destination[0] = mp_obj_new_float(self->x);
}
}
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.
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!
https://github.com/v923z/micropython-usermod/tree/master/snippets/properties/micropython.mk https://github.com/v923z/micropython-usermod/tree/master/snippets/properties/micropython.mk
.. code:: make .. code:: make

4
snippets/properties/properties.c

@ -40,9 +40,9 @@ STATIC const mp_rom_map_elem_t propertyclass_locals_dict_table[] = {
STATIC MP_DEFINE_CONST_DICT(propertyclass_locals_dict, propertyclass_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(propertyclass_locals_dict, propertyclass_locals_dict_table);
STATIC void propertyclass_attr(mp_obj_t self, qstr attribute, mp_obj_t *destination) { STATIC void propertyclass_attr(mp_obj_t self_in, qstr attribute, mp_obj_t *destination) {
if(attribute == MP_QSTR_x) { if(attribute == MP_QSTR_x) {
destination[0] = propertyclass_x(self); destination[0] = propertyclass_x(self_in);
} }
} }

Loading…
Cancel
Save