Search code examples
pythonrulesbusiness-rules

How to implement a ruleset in Python


I process a large dataset where based on column A I want to process some other columns in particular ways.

If column A has "processLastNameOnly", then I only process LastName. If column A has "processMiddleAsFirst", then I process Middle Name as First Name.

Etc.

These rules are plenty and complex, and I want to maintain them in a separate file in some preferably standard "ruleset" format.

Is there a commonly used format for such rules? How do I use it from within Python code?


Solution

  • The cool thing about Python is that everything is an object, including functions. So you can create a dictionary that maps the string (in columnA) to a function.

    def processLastNameOnly(...):
        pass  # process data here
    def processMiddleAsFirst(...):
        pass  # process data here
    
    ruleset = {'processLastNameOnly': processLastNameOnly, 
               'processMiddleAsFirst': processMiddleAsFirst}
    
    
    # Call the function from the dict with the appropriate args
    ruleset[columnA_value](...)
    

    You can store the ruleset and functions in a separate file and import that file as you would do so with any Python object/function.

    You can check this Stack Exchange link for an in-depth discussion.