I have a main script with a static class, that imports other scripts and stores them in a dict in said class. The purpose of the class is to act like an interface between scripts, allowing them to use the class to add functionality to the program. In this class, depending on some events, it calls some functions in the stored scripts, but in these scripts, they use some other functions/variables in the class from main script. I thought that being a static class, it would share it's attributes values through all of the scripts, but it doesn't. Trying to get i.e Interface.scripts.get('foo').a_function() will raise an AttributeError because there is no 'foo' in scripts dict.

I have already accomplish my objective having no static attributes neither static methods in the class and passing the only instance of it to the scripts functions as argument, but(here is the why Im doing this) i would like to simplify code for me, and for everyone who wants to write the scripts and by just using a simple from mainscript import Interface to allow me to use all of the Interface class with no problems.


Here is a resume of the code:

mainscript.py

import os
import importlib
class Interface:
    scripts = {}

    @staticmethod
    def init():
        """This function gets called when mainscript is run in terminal:
           python mainscript.py
        """
        for file in os.listdir("./scripts/"):
            if file.endswith(".py"):
                Interface.scripts[file[:-3]] = importlib.import_module(file)

    @staticmethod
    def some_event():
        Interface.scripts.get('bar').do_stuff()

    @staticmethod
    def print_stuff(some_arg):
        print('hello', some_arg)

    ...more code...

if __name__ == '__main__':
    Interface.init()
    Interface.run()  # some loop that handles events, so 'some_event' will be called eventually

bar.py

from mainscript import Interface

def do_stuff():
    Interface.print_stuff('me')
    Interface.scripts.get('foo').a_function()  # AttributeError, foo not in scripts

Same error would happen with the same situation on other scripts. Notice that the problem here is sharing the memory for the stored values in the attributes of Interface class (say scripts or any other variable).

So, what workarounds can I do?, is it possible to have such static variable (thinking like in c#)?, do I better go back with what I had before, without using a static class?

This is my first question here, so I hope I could explain the situation. Thanks

2 Answers

0
Community On Best Solutions

I just solved this problem, to anyone that wish to know:

I moved the Interface class to another script interface.py and from mainscript.py I import the interface class, like so:

mainscript.py

from interface import Interface

if __name__ == "__main__":
    Interface.init()
    Interface.run()

And in bar.py and others scripts, everything keeps the same.

This way i guarantee that the interface module is imported in mainscript only once, so scripts like bar.py can import Interface without reseting Interface's class variables (or whatever was happening)

0
hir12111 On

Note that what you are saving as a key in the scripts dictionary includes the .py extension, just strip it.

Interface.scripts[file[:-3]] = importlib.import_module(file)