Presentation made at EuroPython 2018 - Edinburgh (by Almar Klein)
This Notebook is:
In [ ]:
# in RISE mode, click <Shift>+<Enter> to execute a cell
def find_prime(nth):
n = 0
i = -1
while n < nth:
i = i + 1
if i <= 1:
continue # nope
elif i == 2:
n = n + 1
else:
gotit = 1
for j in range(2, i//2+1):
if i % j == 0:
gotit = 0
break
if gotit == 1:
n = n + 1
return i
In [ ]:
%time find_prime(1000)
Note:
In [ ]:
# in RISE mode, click <Shift>+<Enter> to execute a cell
from ppci import wasm
In [ ]:
from ppci.lang.python import python_to_wasm
def main():
print(find_prime(1000))
m = python_to_wasm(main, find_prime)
In [ ]:
# WASM (somewhat) readable machine code
m.show()
In [ ]:
# WASM binary format
m.show_bytes()
In [ ]:
# WASM interface
m.show_interface()
In [ ]:
wasm.run_wasm_in_notebook(m)
In [ ]:
wasm.run_wasm_in_node(m)
In [ ]:
# this doesn't currently work on a Python 32 bit, when run on a Windows 64 bit
@wasm.wasmify
def find_prime2(nth):
n = 0
i = -1
while n < nth:
i = i + 1
if i <= 1:
continue # nope
elif i == 2:
n = n + 1
else:
gotit = 1
for j in range(2, i//2+1):
if i % j == 0:
gotit = 0
break
if gotit == 1:
n = n + 1
return i
In [ ]:
%time find_prime2(1000)
In [ ]:
from ppci import wasm
m = wasm.Module(open(r'wasm/rocket.wasm', 'rb'))
m
In [ ]:
m.show_interface()
In [ ]:
# abstract of rocket_qt.py (do not run)
class PythonRocketGame:
# ...
def wasm_sin(self, a:float) -> float:
return math.sin(a)
def wasm_cos(self, a:float) -> float:
return math.cos(a)
def wasm_Math_atan(self, a:float) -> float:
return math.atan(a)
def wasm_clear_screen(self) -> None:
# ...
def wasm_draw_bullet(self, x:float, y:float) -> None:
# ...
def wasm_draw_enemy(self, x:float, y:float) -> None:
# ...
def wasm_draw_particle(self, x:float, y:float, a:float) -> None:
# ...
def wasm_draw_player(self, x:float, y:float, a:float) -> None:
# ...
def wasm_draw_score(self, score:float) -> None:
# ...
In [ ]:
from rocket_qt import QtRocketGame
game = QtRocketGame()
In [ ]:
# you may have to switch to the QT window appearing on the side of your browser session
game.run()
In [ ]:
# let's write the AI in C
print(open('wasm/ai2.c', 'rt').read())
In [ ]:
# use https://wasdk.github.io/WasmFiddle/ to convert ai.c in ai2.wasm
from ppci import wasm
ai2 = wasm.Module(open('wasm/ai2.wasm', 'rb'))
In [ ]:
ai2.show_interface()
In [ ]:
from rocket_ai import AiRocketGame
game = AiRocketGame(ai2)
game.run()