code works on desktop python v3.7.0 but not on micropython v1.9.4

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Xpl0itR
Posts: 2
Joined: Sat Mar 09, 2019 2:38 pm

code works on desktop python v3.7.0 but not on micropython v1.9.4

Post by Xpl0itR » Sat Mar 09, 2019 2:54 pm

I wrote some code to automate Euler's Method for Differential Equations. It works on desktop python v3.7.0 but not on micropython v1.9.4

Code: Select all

def function(x, y, h):
    derivalue = float(eval(dydx))
    print("|| "+str(round(x ,2))+" || "+str(round(y ,2))+" || "+str(round(derivalue ,2))+" || "+str(round(x+h ,2))+" || "+str(round(y+(derivalue*h) ,2))+" ||")
    global xCoord
    global yCoord
    xCoord = x + h
    yCoord = y+(derivalue*h)

iterations = int(input("Iterations: "))
xCoord = float(input("Initial x coordinate: "))
yCoord = float(input("Initial y coordinate: "))
hSteps = float(input("Step size: "))
dydx=input("dy/dx=")

print("==============================================")
print("||  X  ||  Y  || dy/dx || x+h || y+(dy/dx)h ||")
print("==============================================")

for i in range(iterations):
    function(xCoord, yCoord, hSteps)
if i enter:

Code: Select all

Iterations: 4
Initial x coordinate: 1
Initial y coordinate: 1
Step size: 0.5
dy/dx=x**2 + y**2
on Desktop python i get:

Code: Select all

===========================
||  X  ||  Y  || dy/dx || x+h || y+(dy/dx)h ||
===========================
|| 1.0 || 1.0 || 2.0 || 1.5 || 2.0 ||
|| 1.5 || 2.0 || 6.25 || 2.0 || 5.12 ||
|| 2.0 || 5.12 || 30.27 || 2.5 || 20.26 ||
|| 2.5 || 20.26 || 416.63 || 3.0 || 228.57 ||
but on Micropython i get:

Code: Select all

===========================
||  X  ||  Y  || dy/dx || x+h || y+(dy/dx)h ||
===========================
Traceback (most recent call last):
  File "<stdin>", line 20, in <module>
  File "<stdin>", line 2, in function
  File "<string>", line 1, in <module>
NameError: name 'x' is not defined
What am i doing wrong?

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: code works on desktop python v3.7.0 but not on micropython v1.9.4

Post by fstengel » Sat Mar 09, 2019 3:18 pm

The problem lies with eval: it only works in the global scope. Due to micropython's implementation, the only names it can see are the global ones. See post 4 in this thread

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: code works on desktop python v3.7.0 but not on micropython v1.9.4

Post by Roberthh » Sat Mar 09, 2019 4:11 pm

The locals() dictionary is identical to globals(). Even if that is documented, I would consider that as a surprising omission. As a workaround you can supply a specific dictionary to your eval() call, like

Code: Select all

    derivalue = float(eval(dydx, globals(), {"x":x, "y":y}))

fstengel
Posts: 55
Joined: Tue Apr 17, 2018 4:37 pm

Re: code works on desktop python v3.7.0 but not on micropython v1.9.4

Post by fstengel » Sat Mar 09, 2019 4:42 pm

It is documented there. I gather it is one of the compromises one has to live with when using micropython.

Xpl0itR
Posts: 2
Joined: Sat Mar 09, 2019 2:38 pm

Re: code works on desktop python v3.7.0 but not on micropython v1.9.4

Post by Xpl0itR » Sat Mar 09, 2019 4:44 pm

I got it working now, Thanks guys.

Post Reply