"Now, in order to see, how we can work with this structure, we are going to define a new type that simply stores the three values. The module will also have a method called `length`, returning the absolute value of the vector. Also note that here we check the type of the argument, and bail out, if it is not a vector. The beauty of all this is that once the type is defined, the available micropython methods just work. Can you still recall the \n",
"```c\n",
"MP_OBJ_IS_TYPE(myobject, &my_type)\n",
"mp_obj_is_type(myobject, &my_type)\n",
"```\n",
"macro in Section [Type checking?](#Type-checking) I thought so."
]
@ -3464,7 +3464,7 @@
" print('something went terribly wrong`)\n",
"```\n",
"\n",
"construct, or you can inspect the type of the variable at the C level. Unfortunately, there does not seem to be a type identifier for iterables in general, so you have to check, whether the argument is a list, tuple, range, etc. This can be done by calling the `MP_OBJ_IS_TYPE` macro, and see which Boolean it returns, if you pass `&mp_type_tuple`, `&mp_type_list`, `&mp_type_range` etc. to it, as we discussed in the section [Object representation](#Object-representation). \n",
"construct, or you can inspect the type of the variable at the C level. Unfortunately, there does not seem to be a type identifier for iterables in general, so you have to check, whether the argument is a list, tuple, range, etc. This can be done by calling the `mp_obj_is_type` macro, and see which Boolean it returns, if you pass `&mp_type_tuple`, `&mp_type_list`, `&mp_type_range` etc. to it, as we discussed in the section [Object representation](#Object-representation). \n",
"\n",
"The complete code listing of `consumeiterable.c` follows below. If you ask me, this is a lot of code just to replace a python one-liner."
]
@ -4485,7 +4485,7 @@
" if (value == MP_OBJ_SENTINEL) { // simply return the values at index, no assignment\n",
"\n",
"#if MICROPY_PY_BUILTINS_SLICE\n",
" if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {\n",
" if (mp_obj_is_type(index, &mp_type_slice)) {\n",
" uint16_t len = (slice.stop - slice.start) / slice.step;\n",
@ -4514,7 +4514,7 @@
"As advertised, we treat only the case, when `value` is empty, i.e., it is equal to an `MP_OBJ_SENTINEL`. Now, there is no point in trying to read out the parameters of a slice, if the slice object is not even defined, is there? This is the case for the minimal ports. So, in order to prevent nasty things from happening, we insert the `#if/#endif` macro with the parameter `MICROPY_PY_BUILTINS_SLICE`. Provided that `MICROPY_PY_BUILTINS_SLICE` is defined, we inspect the index, and find out if it is a slice by calling \n",
"\n",
"```c\n",
"MP_OBJ_IS_TYPE(index, &mp_type_slice)\n",
"mp_obj_is_type(index, &mp_type_slice)\n",
"```\n",
"\n",
"If so, we attempt to load the slice parameters into the `slice` object with\n",
@ -4528,11 +4528,11 @@
},
{
"cell_type": "code",
"execution_count": 104,
"execution_count": 72,
"metadata": {
"ExecuteTime": {
"end_time": "2019-08-07T04:57:30.535065Z",
"start_time": "2019-08-07T04:57:30.525381Z"
"end_time": "2020-01-02T07:40:11.299734Z",
"start_time": "2020-01-02T07:40:11.277483Z"
}
},
"outputs": [
@ -4540,7 +4540,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"written 4698 bytes to /sliceiterable/sliceiterable.c\n"
"written 4703 bytes to /sliceiterable/sliceiterable.c\n"
]
}
],
@ -4598,7 +4598,7 @@
" if (value == MP_OBJ_SENTINEL) { // simply return the values at index, no assignment\n",
"\n",
"#if MICROPY_PY_BUILTINS_SLICE\n",
" if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {\n",
" if (mp_obj_is_type(index, &mp_type_slice)) {\n",