Notifications
Clear all

What does this Python code do?

8 Posts
2 Users
0 Reactions
655 Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
Topic starter  

I am looking at installing some Python onto my PICO UPS. I am not sure what the following code does, and slightly unsure how to install the sketch/script? I seem to recall it's different from the Arduino IDE. Here is the code

if __name__=='__main__':

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
Quote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1190
 

When writing code in python, one often breaks up the code into modules. (and a bunch of modules is a package). A module is a chuck if code you've saved as a my_program.py or whatever name you give.

Its possible for an individual module to run standalone or to be imported so as to use its functions and or classes etc in the program (or module) in to which its being imported.

So the if __name__ == '__main__':   is just a way of saying if this program is running as a standalone program run the code that follows, but do not run the following code beneath this statement if this program is being imported into another module.

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
Topic starter  

@byron So does that mean I could write a module or even a package that does not have that statement that it is like a runtime library but is installed on the PICO? If I name it RonABCD do I then in a main say import RonABCD or import RonXYZ from RonABCD assuming there was a ?? sub routine named RonXYZ in RonABCD??

I am AFK for a bit to go get another booster.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1190
 

@zander  - Not sure what booster you are getting, but I think you could do with one.  Or maybe I could do with one as your sentence proceeding is a bit hard reading for me 😀 

Probably a very quick example will make it all crystal clear.

module1.py 

def say_hello(person):
    print('hello ', person)
    
if __name__ == '__main__':
    say_hello('Byron')

module2.py

import module1 as m1

m1.say_hello('Rom')

 

As you can see if you run module1.py it says Hello Byron.  

When module2.py is run, and you can see its imported module1, the function call thats made after the if __name__ == '__main__ '  in module1.py is not executed.  It does not say hello Byron.   However the function definition as defined before that statement can be used in module2 so it can be called to say Hello Rom.

Hope the booster works.

 

 

 


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1190
 

@zander,

another thought.  Dont get the if __name__ == '__main__':  confused with creating a program (module) that you name main.py.

In micropython (not python), when the microprocessor running mircropython starts up it will look to see if it can find a module called main.py and it will automatically execute it.  (Note that when the pico is connected to the Thonny IDE this automatic running of a main.py does not seem to happen)

The sequence is to automatically run a program called boot.py  (if there is one) followed by main.py.  Only code relevant to setting the microprocessor up should be put into the boot.py.    

And note its quite possible to accidentally put some tightly looping code into main.py which a control C will not abort your program (or maybe Ctrl C has been deliberately disabled), usually leaving a sad user thinking their pico is now bricked.  There is a special way to re-flash you pico with a flash_nuke.uf2 file but you will loose all your programs stored on the pico.  Always make sure you copy all the programs you created on the pico to your main computer.   

When I have a main.py program on my pico I always start it up with a 5 second sleep time to enable the program to be aborted on startup with a ctrl C in case of cockups 😎 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
Topic starter  

@byron I think that is backward from what I was thinking/hoping. I will use those 2 snippets to experiment to see if I can get it to work the way I think it should. I am fairly sure your previous explanation was correct but I think the the example is backward. I will experiment with it.

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7782
Topic starter  

@byron python micropython ???what is the diff??? DONT answer, just tell me what I need to do to create a program like a C main program that runs as soon as power is on the PICO. Remember it is many miles from electricity, cell, wifi. Just an old man and his PICO plus a battery and maybe some critters. I THOUGHT I would code in Thonny on my Rpi4 and then do something to get the executable code onto the PICO and it will run when power is applied to the PICO. Keep it simple for this old student.

Just remember there can only be one king of the dark side, so after I get all your secrets, well you know what I have to do. 🤠 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1190
 

@zander

Its almost my bedtime so I give you a brief checklist.  A fuller, more verbose (and probably boring) explanation can follow tomorrow if required, just let me know if this turns out to be needed.

1. Make sure you have loaded micropython onto your pico.  Grap the micropython file relevant to your pico, and note there are currently different versions for the pico and the picoW.  

 
2. Load this file onto your pico by holding down the boot button  whilst connecting a usb lead from your mac to the to the usb port on your pico to power it up.  Release the button and you should see a drive called rpi (or something like that) appear on your mac.
Copy the file downloaded (copy and paste) to the rpi drive.  Once loaded the rpi drive will automatically disappear from your mac (with the usual complains from your mac that you failed to eject the drive before disconnecting it)
 
3.  Do NOT load any more arduino code onto you pico or zap goes the micropython. 😥 
 
4. Fire up Thonny on your mac.  (or maybe you rpi 4)  Go to preferences > interpreter and select micropython pico.  On the same screen select the port you pico is on.
 
5. Your Thonny should now show (in its REPL window) that you are connected to microptyon and display the mp version etc.
 
6. Create a program and save it.  e.g. Ron.py.  Run your program in Thonny.
 
7. When it runs ok then either rename it main.py or create a new main.py file and in that file import Ron.py (note you import your Ron.py file with the statement - import Ron  (no .py)
 
8. Whatever is in main.py should now run when the pico is connected to power to run standalone with no screen or IDE attached.
 
9. As you have no screen attached, a good test for you first main.py is to make the program flash its onboard LED.
 
 
Have fun, and its goodnight from me. 😀 
 
 
 

   
ReplyQuote