Writing a Python module that can tell when it's hit with Spyder's UMD (user module deleter)

StackOverflow https://stackoverflow.com/questions/20291743

Вопрос

Spyder's UMD is usually great for me but periodically I trip myself when writing a module that I don't want to delete and reload. I know I can control the UMD via Tools > Preferences > Console > Advanced settings > User Module Deleter. But I would also like to be able to mark certain modules I write as non-UMD friendly in the code of the module itself.

In a perfect world I would just write something like

assert_no_umd()

which would throw an exception if the module is hit by the UMD. It would be fine if the code was tripped by any reloading of the module, whether by UMD or otherwise.

Note that this is different from Method that gets called on module deletion in Python because that question is about cleaning up a database connection which only needs to be done once, and therefore can be done with atexit.

Это было полезно?

Решение

(Spyder dev here) If I understand you correctly, this would be my assert_no_umd function:

import os

def assert_no_umd():
    mod = __file__
    if os.environ.get("UMD_ENABLED", "").lower() == "true":
        namelist = os.environ.get("UMD_NAMELIST", None)
        if namelist is not None:
            namelist = namelist.split(',')
            if mod not in namelist:
                 raise ValueError('UMD active!!')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top