Introduction
In the Plugin Oriented Programming (POP) paradigm, your __init__ function is more than just a traditional constructor. It's the backbone of your project, the command center that defines its very structure. In this blog post, we'll dig deep into the role of __init__ in POP, examining how it can serve as the architectural blueprint for your entire project.
A New Way to Think
POP introduces plugins instead of classes and objects, requiring a paradigm shift in how you approach programming. The __init__ function is sort of like your plugin's "constructor", where you lay out the architectural blueprint of your project. It is where we initialize variables, import modules, and orchestrate how everything interacts on the hub.
How to do it
We have a very simple entrypoint to start a POP project. Here is a run.py that initializes a hub, then adds a module to it and calls a "main" function on that module:
# run.py
import pop.hub
# initialize the hub, the heart of any POP project
hub = pop.hub.Hub()
# Add a base plugin to the hub; this triggers its __init__ function
hub.pop.sub.add(dyne_name="my_app")
# Call the "main" function, the entrypoint of your project
hub.my_app.init.main()
Now that we have identified a clear entrypoint for our project, let's look at what a very basic plugin might look like for our app:
# my_app/init.py
def __init__(hub):
# Add any other subs or dynes onto the hub that our plugin needs
# Recursively add subdirectories of modules that extend your namespace
hub.pop.sub.load_subdirs(hub.my_app, recurse=True)
# Store a simple variable on the hub for later use
hub.my_app.VAR = "string"
# Create a 'lib' namespace on the hub to keep imported modules
hub.pop.sub.add(subname="lib")
# Add Python's 'random' library under the 'lib' namespace
hub.pop.sub.add(python_import="random", sub=hub.lib)
# Import 'uuid' library and alias it as 'UUID' under hub.lib
hub.pop.sub.add(python_import="uuid", subname="UUID", sub=hub.lib)
# Import a submodule and give it a concise alias
# Classes are omitted by default, ensure they get added
hub.pop.sub.add(python_import="dict_tools.data", subname="ddata", sub=hub.lib, omit_class=False)
def main(hub):
print(hub.lib.random.randint(0, 10))
print(hub.lib.UUID.uuid4())
my_dict = hub.lib.ddata.NamespaceDict()
print(hub.my_app.VAR)
Final Thoughts
In the context of Plugin Oriented Programming (POP), the __init__ function is the control center that brings structure and organization to your project. It's where all your imports, modules, and variables come together cohesively. This centralized architecture not only streamlines dependency management but also makes your project easier to navigate and debug.
Simply put, your __init__ function in POP is a necessity, not an option. It's the backbone that keeps everything upright and functional.
Comments