0

I want to send instructions to a method as a pickleable object, then have that make changes. (Think track changes).

Current method:

  1. I define a set of instructions:

    instructions = {
        0: {
            'function': 'do_something_func',
            'kwargs': {
                'a':22,
                'b':'hello,  
            }
        }
    }
    
  2. Inside my class I run the following:

    for step, transformation in self.instructions.items():
        eval('self.' + transformation['function'])(**transformation['kwargs'])
    

Generally, whenever eval is used, it's unsafe and wrong. This also feels like a problem people would have solved.

So, what is the "right" way to do this? Rough criteria:

  • Allow me to track and store a set of instructions (so I can reverse if required)
  • Doesn't expose vulnerabilities.
  • Scalable to ~100 transformations max.
  • Human readable instructions if possible / easy to develop.
  • I'm quite flexible on how these instructions are stored, I have initialy thought of using a yaml file, but gets more tricky whne using python specific arguments - e.g. numpy.nan
GooJ
  • 257
  • 3
  • 13
  • 2
    `getattr(self, transformation['function'])(**transformation['kwargs'])`…?! – deceze Aug 28 '22 at 09:04
  • @deceze Beautiful - thank you! Happy to delete this or keep it up as closed for searchability reasons? Also +1 for finding a duplicate from 12 years ago :D – GooJ Aug 28 '22 at 09:07

0 Answers0