模块¶
Generated Wed 03 Jan 2024 12:07:50 UTC
数组¶
查找为实现整数¶
参考代码
import array
print(1 in array.array("B", b"12"))
CPy 输出 |
uPy 输出 |
False
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
检测为实现数组¶
参考代码
import array
a = array.array("b", (1, 2, 3))
del a[1]
print(a)
CPy 输出 |
uPy 输出 |
array('b', [1, 3])
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
Subscript with step != 1 is not yet implemented¶
参考代码
import array
a = array.array("b", (1, 2, 3))
print(a[3:2:2])
CPy 输出 |
uPy 输出 |
array('b')
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
内置¶
未实现第二个参数 next()¶
Cause: MicroPython is optimised for code space.
Workaround: Instead of val = next(it, deflt)
use:
try:
val = next(it)
except StopIteration:
val = deflt
参考代码
print(next(iter(range(0)), 42))
CPy 输出 |
uPy 输出 |
42
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
队列¶
队列为实现¶
Workaround: Use regular lists. micropython-lib has implementation of collections.deque.
参考代码
import collections
D = collections.deque()
print(D)
CPy 输出 |
uPy 输出 |
deque([])
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
json¶
对象序列化时,JSON模块不会抛出异常¶
参考代码
import json
a = bytes(x for x in range(256))
try:
z = json.dumps(a)
x = json.loads(z)
print("Should not get here")
except TypeError:
print("TypeError")
CPy 输出 |
uPy 输出 |
TypeError
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
os¶
environ
属性未实现¶
Workaround: Use getenv
, putenv
and unsetenv
参考代码
import os
try:
print(os.environ.get("NEW_VARIABLE"))
os.environ["NEW_VARIABLE"] = "VALUE"
print(os.environ["NEW_VARIABLE"])
except AttributeError:
print("should not get here")
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))
CPy 输出 |
uPy 输出 |
None
VALUE
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
getenv
returns actual value instead of cached value¶
Cause: The environ
attribute is not implemented
参考代码
import os
print(os.getenv("NEW_VARIABLE"))
os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))
CPy 输出 |
uPy 输出 |
None
None
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
getenv
only allows one argument¶
Workaround: Test that the return value is None
参考代码
import os
try:
print(os.getenv("NEW_VARIABLE", "DEFAULT"))
except TypeError:
print("should not get here")
# this assumes NEW_VARIABLE is never an empty variable
print(os.getenv("NEW_VARIABLE") or "DEFAULT")
CPy 输出 |
uPy 输出 |
DEFAULT
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
struct结构¶
Struct pack with too few args, not checked by uPy¶
参考代码
import struct
try:
print(struct.pack("bb", 1))
print("Should not get here")
except:
print("struct.error")
CPy 输出 |
uPy 输出 |
struct.error
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
Struct pack with too many args, not checked by uPy¶
参考代码
import struct
try:
print(struct.pack("bb", 1, 2, 3))
print("Should not get here")
except:
print("struct.error")
CPy 输出 |
uPy 输出 |
struct.error
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|
sys¶
Overriding sys.stdin, sys.stdout and sys.stderr not possible¶
Cause: They are stored in read-only memory.
参考代码
import sys
sys.stdin = None
print(sys.stdin)
CPy 输出 |
uPy 输出 |
None
|
/bin/sh: ../ports/unix/micropython: No such file or directory
|