[Solved] : How to create library ?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Patriboom
Posts: 7
Joined: Tue Feb 08, 2022 5:20 am

[Solved] : How to create library ?

Post by Patriboom » Tue Feb 08, 2022 5:40 am

Hello all,

According to the researches I've done, I expected to be able to call functions from outside file into my script like this:

Code: Select all

from OLEDformes import *

....
...

#and call my functions (from OLEDformes.py file like this: 
    fillLozg(AffGrd, 80, 33, 15, 1)
    fillTrgl(AffGrd, 20, 43, 18, 1)

(In the main program)

And have simple function defined in the called file like:

Code: Select all

def fillLozg:
	blabla
	
def fillTrgl:
	blabla
But it doesn't work.
The return message is something like « such function is not defined. »

Is it a difference between Python and MicroPython or have I did something wrong ?

Thanks
Last edited by Patriboom on Tue Feb 08, 2022 4:53 pm, edited 1 time in total.

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: How to create library ?

Post by karfas » Tue Feb 08, 2022 1:53 pm

Your function definitions aren't functions.
According to https://www.w3schools.com/python/python_functions.asp a python function is defined as

Code: Select all

def my_function():
  print("Hello from a function")
Note the parentheses "()" after the function name.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Patriboom
Posts: 7
Joined: Tue Feb 08, 2022 5:20 am

Re: How to create library ?

Post by Patriboom » Tue Feb 08, 2022 4:43 pm

Sorry for that, mistyped the function

Here is a copy-paste of the function:

Code: Select all

def fill (coul = 1):
    for x in range (0, 128):
        for y in range (0, 64):
            self.pixel(x,y,coul)

Now, the original code calling the function (from main.py) :

Code: Select all

# Display Image & text on I2C driven ssd1306 OLED display 
from machine import Pin, I2C
from SSD1306 import SSD1306_I2C
import framebuf
import machine
import utime
from OLEDformes import *

#other commands

    AffGrd.show()
    utime.sleep(3)
    fillLozg(AffGrd, 80, 33, 15, 1)
    fillTrgl(AffGrd, 20, 43, 18, 1)

And received this error code:

Code: Select all

Traceback (most recent call last):
  File "<stdin>", line 52, in <module>
  File "OLEDformes.py", line 21, in fillLozg
NameError: name 'self' isn't defined
I think I'll fix it easy.

Thanks.

I'll be back with news.

Patriboom
Posts: 7
Joined: Tue Feb 08, 2022 5:20 am

Re: How to create library ?

Post by Patriboom » Tue Feb 08, 2022 4:52 pm

Good, good now!

My problem was on updating the file. Doing only « save » didn't replace the file or the cache.
I changed the called file's name and now it works.

Thanks for your help.
Sorry for so stupid question.

Post Reply