top of page
Search
  • Akmod

Coding Conventions in POP: A Revolutionary Approach

In the realm of software development, conventions are more than just rules for writing code. They're the structure that aids collaboration, mitigates confusion, and reduces the time spent on understanding how different pieces fit together. Among the various programming paradigms and philosophies, the Plugin Oriented Programming (POP) introduced by Thomas Hatch offers a unique approach to organize code and manage dependencies.


In POP, everything is loaded onto the 'hub'. This fundamental rule eliminates the need for recursive dependencies and imports, even with Python libraries. As an example, consider the following:


# create the hub
import pop.hub
hub = pop.hub.Hub()

# Create a subsystem on the hub for hosting python libraries
hub.pop.sub.add(dyne_name='lib')

# Add python libraries to hub.lib
hub.pop.sub.add(python_import="pathlib", sub=hub.lib)

# use the python library
path = hub.lib.pathlib.Path("/")

POP doesn't need classes. Classes are used only for types. Instead of classes, we use 'plugins', files that hold specific functions. These plugins allow you to seamlessly merge large and complex apps, and modify or extend the functionality of another app without having to modify its code explicitly.


POP's plugin architecture is designed to alleviate one of the biggest pain points of maintaining an open source project: the constant need to say "no". With POP, you don't need a maintainer's permission to get the features you want. You simply add the functionality to your project with your own plugin and contracts, modifying the behavior of another POP project as required.


A practical example of this is the project idem-ai. This project only includes contracts that modify "idem", a POP project. Here, large dependencies such as AI models are isolated into its own project. It also allows it to modify the output of idem state functions without requiring explicit modification of the main idem project's code.


To successfully work with POP, there are some key concepts and functions to be aware of:


__virtual__: This function is used to determine if a plugin should be loaded. If there are two plugins with the same name, __virtual__ makes it deterministic which one wins. Consider the following example from an idem-posix plugin:

import os
def __virtual__(hub):
    if os.name == "posix":
        return True
    else:
        return False

__contracts__: This list can be defined in any plugin to enforce a specific contract. It’s a powerful tool for enforcing standards and maintaining consistency across different plugins. In idem-aws, for instance, you will often find __contracts__ defined as follows:


__contracts__ = ["resource"]

This particular contract is defined in idem itself and enforces that a plugin contains a present, absent, and describe function with specific parameters. It also has a post contract that fires an event with the results of an individual state.

__init__: This function is used for setting up the hub for your project or all the hub structures needed for a specific function. This can be used in any plugin, but you will primarily see it in the very first "init" plugin that get's loaded by a run.py entrypoint.


__virtualname__: This provides a virtual name to a plugin if the plugin name clashes with a Python keyword or another Python import.


POP projects like "idem" benefit immensely from the use of plugins. They allow for the main project to remain simple while complex testing and requirements of all its plugins can be isolated and maintained by dedicated teams.


This philosophy makes development with POP convenient. With the conventions in place, idem remains easy to maintain, and users can develop plugins without having to negotiate with the core team for each feature they want.


For developers new to POP, I recommend checking out the POP documentation on pop.readthedocs.io, tuning into my TikTok and YouTube channel, and keeping an eye on this blog. Get started with POP, and you'll find that you can build powerful, maintainable software with a new level of freedom and flexibility.

30 views0 comments

Recent Posts

See All

Comments


bottom of page