How to organize code

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
sushyoshi
Posts: 21
Joined: Sat Feb 05, 2022 9:25 am

How to organize code

Post by sushyoshi » Fri Mar 04, 2022 1:53 am

Hello everyone,

I have been working on a project on a Pico however my code is getting very long.

How do I organize my code and separate it into different .py files? I was thinking about doing like the import modules, but I am not sure how to do it correctly. Something like this:

Code: Select all

from machine import I2C, I2S, Pin, SPI
import time
import array
import math

import my_arrays
import my_functions
import my_constants
import my_variables
Thanks for any suggestion.

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

Re: How to organize code

Post by stijn » Fri Mar 04, 2022 7:47 am

You probably should have a look at other small Python projects, and micropython's own python code to see how things are structured.

Modules are for grouping functionality i.e. functions and classes, something like 'import my_variables' and 'import my_arrays' is the opposite and looks like a red herring because it would mean you have a lot of global state which is generally, and imo even on microcontrollers, problematic once the code starts to grow. Also makes it really hard to share code between projects, whereas proper modules are ideal for that.

So a simple exercise is to think: suppose you start another project, slightly similar: all the functionality which you have now and could also be useful in another project should be relatively small classes and functions which fo in a module so that you could use them in the other project.

sushyoshi
Posts: 21
Joined: Sat Feb 05, 2022 9:25 am

Re: How to organize code

Post by sushyoshi » Fri Mar 04, 2022 3:49 pm

Thanks. That was a very good advice.
My idea to put the arrays in a module was mostly because they are so big and make the main code difficult to read. Like you have mentioned, I am already having problems with global variables at this point so I will follow your advice and restrain from using them. But for the functions, it works perfectly.
Now I just have to find a way to organize the arrays. I am thinking of storing them in a separate file in the internal flash memory. The problem is that I want to be able to write arrays on a file while the Pico is running (by running functions to create arrays) and also be able to read them. What would you recommend the best approach for this?

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

Re: How to organize code

Post by stijn » Fri Mar 04, 2022 7:33 pm

Bit hard to tell, but sounds ok to me to put those arrays in a separate module for clarity; doesn't necessarily mean they should be globals though: you could have functions like

Code: Select all

def LargeArrayA():
  return [......]

def LargeArrayB():
  return [......]
But that's just an idea, I'd probably recommend trying out different things and see what is most convenient.

If you want the arrays in files, look at using json. Or if they're all floating point numbers for instance then it's faster and less memory intensive to use the array module (but the file will be binary and not human readable).

sushyoshi
Posts: 21
Joined: Sat Feb 05, 2022 9:25 am

Re: How to organize code

Post by sushyoshi » Mon Mar 07, 2022 2:31 am

Thanks.

The arrays are all int so I will look into using json for it. Good advice!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to organize code

Post by pythoncoder » Mon Mar 07, 2022 1:28 pm

If this is for your audio waveform project please see my comment here. As far as I can see you only need two arrays.
Peter Hinch
Index to my micropython libraries.

Post Reply