Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 400
dust Avatar asked Aug 13 '26 20:08

dust


1 Answers

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.

like image 148
ssundarraj Avatar answered Aug 16 '26 09:08

ssundarraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!