On the pc, this runs well. On the board, however, importing the module makes available only some of the units, classes, and functions. I think it varies per session which members are available or not, but I cannot confirm this yet for sure. The module I am importing is listed at the end of this question.
After importing he unit in Python on the pc and running
for v in [m for m in sorted(dir(platformSpecific)) if not m.startswith('_')]:
print("%-20s : %s" % (v, getattr(platformSpecific, v)))
I get this result, which is what I expect:
Code: Select all
AutoNumber : <enum 'AutoNumber'>
EMBEDDED : False
Enum : <enum 'Enum'>
getDeltaTime : <function getDeltaTime at 0x02782F60>
getTicks_ms : <function getTicks_ms at 0x02732588>
initializestatic : <function initializestatic at 0x02793930>
json : <module 'json' from 'C:\\Program Files (x86)\\python\\Anaconda3\\lib\\json\\__init__.py'>
machine : isoetes
os : <module 'os' from 'C:\\Program Files (x86)\\python\\Anaconda3\\lib\\os.py'>
platform : <module 'platform' from 'C:\\Program Files (x86)\\python\\Anaconda3\\lib\\platform.py'>
programmingLanguage : cpython
sockets : <module 'socket' from 'C:\\Program Files (x86)\\python\\Anaconda3\\lib\\socket.py'>
sys : <module 'sys' (built-in)>
sysname : nt
time : <module 'time' (built-in)>
Code: Select all
AutoNumber : <class 'AutoNumber'>
EMBEDDED : True
SortOfEnum : <class 'SortOfEnum'>
json : <module 'ujson'>
machine : ESP32 module with ESP32
os : <module 'uos'>
programmingLanguage : micropython
sockets : <module 'usocket'>
sys : <module 'sys'>
sysname : esp32
time : <module 'utime'>
For a test, I forced the EMBEDDED variable to true (and made a few minor changes to avoid non-existing units and functions). Then the get-functions are exported as expected:
Code: Select all
AutoNumber : <class 'platformSpecific.SortOfEnum'>
EMBEDDED : True
SortOfEnum : <class 'platformSpecific.SortOfEnum'>
getDeltaTime : <function getDeltaTime at 0x027C2150>
getTicks_ms : <function getTicks_ms at 0x02595150>
initializestatic : <function initializestatic at 0x027D77C8>
json : <module 'json' from 'C:\\Program Files (x86)\\python\\Anaconda3\\lib\\json\\__init__.py'>
machine : N/A
os : <module 'os' from 'C:\\Program Files (x86)\\python\\Anaconda3\\lib\\os.py'>
programmingLanguage : cpython
sockets : <module 'socket' from 'C:\\Program Files (x86)\\python\\Anaconda3\\lib\\socket.py'>
sys : <module 'sys' (built-in)>
sysname : N/A
time : <module 'time' (built-in)>
What is going on here?
Follows the code of the module. I have tried this as file in the root-directory and as __init__.py in a folder of lib:
Code: Select all
import sys
import os
programmingLanguage = sys.implementation.name
EMBEDDED =(programmingLanguage == "micropython")
if not EMBEDDED:
import platform
import time
from enum import Enum
import socket as sockets # @UnusedImport
import json # @UnusedImport
sysname = os.name
machine = platform.node()
def getTicks_ms():
return 1000 * time.time()
def getDeltaTime(t2, t1):
return t2-t1
def initializestatic(cls):
# no need to call cls.initclass()
return cls
class AutoNumber(Enum):
def __new__(cls, doc):
value = len(cls.__members__)
obj = object.__new__(cls)
obj._value_ = value
obj.doc = doc
return obj
else:
import utime as time # @UnresolvedImport @UnusedImport @Reimport
import usocket as sockets # @UnresolvedImport @UnusedImport @Reimport
import ujson as json # @UnusedImport @UnusedImport @Reimport
sysname, _, _, _, machine = os.uname() # @UndefinedVariable
def getTicks_ms():
return time.ticks_ms()
def getDeltaTime(t2, t1):
return time.ticks_diff(t2, t1)
def initializestatic(cls):
cls.initclass()
return cls
class SortOfEnum():
class _EnumElement: ### (= SimpleNamespace)
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __str__(self):
keys = sorted(self.__dict__)
items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
return "{}({})".format(type(self).__name__, ", ".join(items))
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __new__(cls, value):
return cls.index[value]
@classmethod
def initclass(cls):
items = [(cmd, getattr(cls, cmd)) for cmd in dir(cls) if not cmd.startswith('_')]
cls.index = []
for item in enumerate(items):
item = cls._EnumElement(value=item[0], name=item[1][0], doc=item[1][1])
if item.name != "initclass":
# print(item)
setattr(cls, item.name, item)
cls.index.append(item)
AutoNumber = SortOfEnum