Submodules and Classes in C modules

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zpif
Posts: 10
Joined: Mon Jun 14, 2021 12:05 pm

Submodules and Classes in C modules

Post by zpif » Wed Jun 23, 2021 8:36 am

Hi,

I am writing a custom C module for Micropython. I have two questions:
  • Is it possible to have submodules?
  • Is it possible to define classes?
Thanks,
Frank

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Submodules and Classes in C modules

Post by stijn » Wed Jun 23, 2021 8:54 am

The answers to both questions is 'yes', and if you look through the source code (search for module definitions e.g around code like ".base = { &mp_type_module }," you should find examples of both.

zpif
Posts: 10
Joined: Mon Jun 14, 2021 12:05 pm

Re: Submodules and Classes in C modules

Post by zpif » Wed Jun 23, 2021 10:26 am

Thank you stijn for your quick response.

I did find plenty of examples where classes are defined, thank you. However, I did not find examples of modules that contain modules.

Could you please point me to one?

Thank you,
Frank

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Submodules and Classes in C modules

Post by stijn » Wed Jun 23, 2021 12:19 pm

Sorry my mistake, I did not actually try to look for nested modules. Principle would be that you create a module object just like any other, than add that object to the module you want it to be nested in. Pseudocode

Code: Select all

mp_obj_module_t nested = {
    .base = { &mp_type_module },
    .globals = ...
};

STATIC const mp_rom_map_elem_t mainmodule_globals_table[] = {
...
{ MP_ROM_QSTR(MP_QSTR_nested), MP_ROM_PTR(&nested) },
}

...
However this might not be what you're after; this works, but only in that if you do 'import mainmodule' you get access to 'mainmodule.nested'.

But looking at how the code for importing works this you cannot use 'import mainmodule.nested'. That only works for actual 'packages' (which AFAIK is the offcial name for a moule which contains other modules) and they are always filessystem-based it seems (did not check in detail though, someone might want to correct me here).

Anyway not a huge problem: mix Python and C modules for that. So in C you create 2 modules: '_mainmodule' and '_nested'. Then you add a directory which can be found via MICROPYPATH and contains:

Code: Select all

mainmodule\
    __init__.py
    nested.py


In __init__.py you put 'from _mainmodule import *' and in nested.py 'from _nested import *'

katesimon123
Posts: 15
Joined: Mon Jun 14, 2021 12:49 am

Re: Submodules and Classes in C modules

Post by katesimon123 » Thu Jun 24, 2021 5:54 am

Try this one out:

Code: Select all

mypackage
├── __init__.py
├── one.py  # contains "import two"
└── two
    ├── __init__.py
    ├── two.py  # contains "import three"
    └── three
        ├── __init__.py
        └── three.py
And then, you can access the package with:

Code: Select all

import mypackage.one
import mypackage.one.two
import mypackage.one.two.three
You can read it in more detail from Introduction to Classes.
Last edited by katesimon123 on Sun Jul 11, 2021 2:19 am, edited 1 time in total.

zpif
Posts: 10
Joined: Mon Jun 14, 2021 12:05 pm

Re: Submodules and Classes in C modules

Post by zpif » Thu Jun 24, 2021 8:39 am

Actually, I was looking for stijn's first solution of nested modules, and your suggestion works well, thank you!

But I now realize that this set-up might not be very python-ish, and that I should use the other solution instead, to have python scripts with the proper directory structure set-up importing the C packages internally.

So thank you both!

zpif
Posts: 10
Joined: Mon Jun 14, 2021 12:05 pm

Re: Submodules and Classes in C modules

Post by zpif » Thu Jun 24, 2021 12:44 pm

A follow-up question: I would like to run some initialization code as my module is loaded. I've tried adding an "__init__" method to the (toplevel) .globals dictionary, but the function is not called automatically.

Calling "mymodule.__init__()" manually after import works, so I am confident that this is not a typo.

Is this something that ought to work?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Submodules and Classes in C modules

Post by stijn » Thu Jun 24, 2021 1:10 pm

You need a build which has MICROPY_MODULE_BUILTIN_INIT set to 1 for that, not all ports have it like that.

zpif
Posts: 10
Joined: Mon Jun 14, 2021 12:05 pm

Re: Submodules and Classes in C modules

Post by zpif » Thu Jun 24, 2021 1:56 pm

Ah, that #define fixed it. Excellent, thank you again!

Post Reply