Supported MicroPython
Supported MicroPython language constructs and modules
Compatibility
The flavor of MicroPython used within the Cuica is based on Micropython
v1.20.0 and supports a majority of the available libraries. Note that
some libraries still use the old convention of starting with u-
.
Because the bytecode is executed on a single microprocessor core, there
is no support for threading to due resource constraints.
Consult the official MicroPython documentation for more information.
Hardware Libraries
The hardware libraries supported by the Cuica are based on the original MicroPython libraries, however to simplify and reduce redundancy, the options specifying which interfaces are restricted to to those interfaces that are available on the Cuica. For example, there's no need to supply a UART interface index since there's only one provided:
uart = UART(115200)
instead of:
uart = UART(0, 115200)
Otherwise, the behavior and interfaces are identical to those provided by MicroPython.
Utility Libraries
The following MicroPython utility modules are available:
uarray
– arrays of numeric dataubinascii
– binary/ASCII conversionsbuiltins
– builtin functions and exceptionsucollections
– collection and container typesuhashlib
– hashing algorithmsuio
– input/output streamsujson
– JSON encoding and decodingmath
– mathematical functionsuos
– basic “operating system” servicesure
– simple regular expressionsustruct
– pack and unpack primitive data typessys
– system specific functionstime
– time related functions
Supported Language Features
Most of the basic language features of Python are supported. The following script illustrates some of what is supported:
# Types
int_var = 10
float_var = 10.5
str_var = 'Hello World'
list_var = [1, 2, 3, 4, 5]
dict_var = {'key': 'value'}
set_var = {1, 2, 3, 4, 5}
tuple_var = (1, 2, 3, 4, 5)
# Functions
len_var = len(list_var)
type_var = type(int_var)
sum_var = sum(list_var)
min_var = min(list_var)
max_var = max(list_var)
abs_var = abs(-10)
round_var = round(10.5)
sorted_var = sorted(list_var)
reversed_var = list(reversed(list_var))
print_var = print('Hello World')
# Type conversion
int_to_float = float(int_var)
float_to_int = int(float_var)
int_to_str = str(int_var)
str_to_int = int('10')
list_to_set = set(list_var)
set_to_list = list(set_var)
# Other functions
range_var = list(range(10))
enumerate_var = list(enumerate(list_var))
zip_var = list(zip(list_var, reversed_var))
# More types
bool_var = True
none_var = None
complex_var = 1 + 2j
bytes_var = b"Hello"
bytearray_var = bytearray(b"Hello")
# More functions
pow_var = pow(2, 3)
divmod_var = divmod(10, 3)
all_var = all(list_var)
any_var = any(list_var)
bin_var = bin(10)
hex_var = hex(255)
oct_var = oct(8)
# Built-in constants
true_var = True
false_var = False
none_var = None
ellipsis_var = ...
# Iterable related functions
filter_var = list(filter(lambda x: x % 2 == 0, list_var))
map_var = list(map(lambda x: x * 2, list_var))
next_var = next(iter(list_var))
# Character and string related functions
chr_var = chr(65)
ord_var = ord('A')
format_var = "{} {}".format("Hello", "World")
# Variations of functions
str_format_var = "{name} is {age} years old".format(name="Alice", age=25)
map_multiple_iter_var = list(map(pow, [2, 3, 4], [2, 2, 2]))
filter_none_var = list(filter(None, [0, 1, False, True, '', 'hello']))
# Exceptions
try:
x = 1 / 0
except ZeroDivisionError as e:
print("Caught an exception:", e)
# Flow control
for i in range(5):
if i == 2:
continue # skip the rest of this loop iteration
elif i == 4:
break # terminate the loop
print(i)
else:
print("Loop ended normally")
# Function definition and invocation
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
print(greet("Alice"))
print(greet("Bob", "Good morning"))
# Class definition and instantiation
class MyClass:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hello, {self.name}!"
obj = MyClass("Alice")# Decorators
def my_decorator(func):
def wrapper(*args, **kwargs):
print("Before function call")
result = func(*args, **kwargs)
print("After function call")
return result
return wrapper
@my_decorator
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
# Generators
def fibonacci(limit):
a, b = 0, 1
while a < limit:
yield a
a, b = b, a + b
for num in fibonacci(10):
print(num)
# List Comprehensions
squares = [x**2 for x in range(10)]
# Dictionary Comprehensions
dict_squares = {x: x**2 for x in range(5)}
# Set Comprehensions
set_squares = {x**2 for x in range(-5, 5)}
# Generator Expressions
gen_squares = (x**2 for x in range(10))
# *args and **kwargs
def foo(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f"{key} = {value}")
foo('hello', 'world', name='Alice', age=25)
# Lambda functions
f = lambda x: x**2
print(f(5))
# Error handling with finally
try:
x = 1 / 0
except ZeroDivisionError as e:
print("Caught an exception:", e)
finally:
print("This block is always executed")
# Assert statement for debugging
try:
x = "hello"
assert isinstance(x, int), "x is not an integer"
except AssertionError as e:
print(e)
print(obj.greet())
class MyContextManager:
def __enter__(self):
print("Entering the context")
return self
def __exit__(self, exc_type, exc_value, traceback):
print("Exiting the context")
# Context Managers
with MyContextManager() as x:
print("Inside the context")
help(print)
Hello World
Caught an exception: divide by zero
0
1
3
Hello, Alice!
Good morning, Bob!
Before function call
Hello, Alice!
After function call
0
1
1
2
3
5
8
hello
world
name = Alice
age = 25
25
Caught an exception: divide by zero
This block is always executed
x is not an integer
Hello, Alice!
Entering the context
Inside the context
Exiting the context
object <function> is of type function
Unsupported Language Features
The following features are not supported:
ascii_var = ascii("Hello World")
not_implemented_var = NotImplemented
slice_var = slice(1, 3)
list_var[slice_var]