I would like to figure out how to monkey-patch modules to help with this, but literally assigning to modules just leads to e.g.
Code: Select all
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== from ucollections import namedtuple
=== _struct_time = namedtuple("struct_time", ("tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"))
=== def struct_time(tm_year, tm_mon, tm_mday, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=-1, tm_yday=-1, tm_isdst=-1):
=== return _struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
===
>>> import time
>>> setattr(time, "struct_time", struct_time)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'struct_time'
Seems like a compatibility layer is mostly feasible, but of course there is a huge value to this being seamless and not requiring a specialised bundle to be distributed with tweaked modules. The experiment is to see if people can be enabled to use the regular module source instead, but relying on extra attributes having been inserted beforehand.
To achieve this, I am wondering what I can do to insert functions into system modules where necessary (this would be done before importing libraries depending on them). For example if an implementation of struct_time could be made available to a module like https://github.com/adafruit/Adafruit_Ci ... uit_gps.py then it should be able to run OK, but I can't control how the module imports struct_time without modifying the module, (which is what I'm trying to avoid).
I acknowledge that even if a wrapper approach could be found, then this runtime monkey-patching approach eats away at limited RAM, (although this could end up being academic in the case of 4Meg PSRAM ESP32 modules which are on their way into our ecosystem).