VEXcode V5 2.0.7 Python VM updates

The latest VEXcode release (2.0.7) did include an updated Python VM with a few new features.

1 - This release is based on micropython V1.13
we previously used 1.12, so several months worth of bug fixes to the core of the Python VM

2 - improved file support
you can now use standard Python file IO.
the open() function is supported.

3 - import of modules from SD Card

an example of this.

import and file IO demo
# Library imports
from vex import *

# import functions from on SD Card
from james import * # pylint: disable=import-error

# open a file for writing
f = open("test.txt", 'w')

# write to file using print
print("Hello", file=f)
print("Goodbye", file=f)

# now open the same file for reading
f = open("test.txt", 'r')
# and read lines, show on terminal

# functions from the james module
# pylint: disable=undefined-variable

Note: I am having to disable some of the linting we now do on the Python source to build this without showing an error dialog in VEXcode before the download.

the module that is imported
# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while a < n:
        a, b = b, a+b
    return result

place this in a file “” (or whatever you want to call it) on the SD card

This is primarily for import of your own code, most publicly available modules will probably not import without modification.


supporting standard fileio has some interesting side effects, for example, as V5 ports can be now opened using this API, generic serial comms is now supported in Python in the same way as it is in C++.

f = open("/dev/port1",'w')

would allow output on port1 with default baud rate (115200)

4 - asyncio support
As micropython now has asyncio as an available module, we can support this.

asyncio example
# Library imports
from vex import *

# Begin project code
    import uasyncio as asyncio
except ImportError:
        import asyncio
    except ImportError:
        print("asyncio not available")
        raise SystemExit

    import utime

    ticks = utime.ticks_ms
    ticks_diff = utime.ticks_diff
    import time

    ticks = lambda: int(time.time() * 1000)
    ticks_diff = lambda t1, t0: t1 - t0

async def delay_print(t, s):
    await asyncio.sleep(t)

async def main():

    await asyncio.sleep(0.001)
    print("after sleep")

    t0 = ticks()
    await delay_print(0.2, "short")
    t1 = ticks()
    await delay_print(0.8, "long")
    t2 = ticks()
    await delay_print(-1, "negative")
    t3 = ticks()

        "took {} {} {}".format(
            round(ticks_diff(t1, t0), -1),
            round(ticks_diff(t2, t1), -1),
            round(ticks_diff(t3, t2), -1),