2

So I have a design/architectural question. I want to develop a pattern in a programming language that is able to allow a app command-line shell to send commands to a running application to create objects that a developer maybe working on while the application is running.

For example:

CLI> createObjCmd someClass arg1 arg2 arg3

The application will locate the class that the developer just worked on and create the object from that class and add the execution path of that object to the main loop of the application and run it under some interface definition for that object. I am thinking such a pattern can only be developed in an interpretable language such as python.

How can I do this?

3
  • oh bad question? Would be useful to understand why. The part I was most interested in maybe the most relevant to this SE was being able to code and as well be able to load that code dynamically which I don't know if that even exists out there. The first thing that comes to mind is a plugin design however...
    – LeanMan
    May 19 at 13:43
  • What is "the class that the developer just worked on"?
    – JacquesB
    May 19 at 14:17
  • A piece of interpretable code was not present at the time the python script started that represents a class definition and can be instantiated into an object and holds state as soon as the script is commanded to create the object.
    – LeanMan
    May 19 at 20:43
2

The general name for your search terms is a "plugin architecture" supporting "hot reload".

You may be surprised to lean that it's not specific to interpreted languages. You can do this in C, C#, Java, Lisp, all sorts of languages. Your command line example reminded me of rundll32. Here's someone describing their C hot reload architecture.

locate the class that the developer just worked on and create the object from that class and add the execution path of that object to the main loop of the application and run it under some interface definition for that object.

Yes, that's pretty much the set of operations you need to define. The one thing I would add is that you need to define unload as well. And consider what it means to add a class that's already there. Normally you'd want to do that as unload-reload, destroying all instances of the old one before loading the new one. You can do it more finely but that is much more fragile.

Probably also want to consider exception handling (don't want a newly loaded module to take out the whole thing) and help for modules that wish to communicate with each other (you should avoid letting them do so directly if you ever want unload to work reliably).

For python you may want to look at importlib and familiarise yourself with exactly how import works.

1
  • awesome answer! Thanks!
    – LeanMan
    May 24 at 16:05
1

It is not clear what you have tried and what your actual problem is.

In Python, you can just exec() a string, which may have been read from a file. This may contain anything, including class definitions.

There are numerous examples and tutorials on how to do this, so it's weird that you didn't find and try even one.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.