Advice on optimizing a Python data driven rules engine

Michael Selik michael.selik at gmail.com
Thu Aug 11 11:38:30 EDT 2016


On Thu, Aug 11, 2016 at 10:57 AM Malcolm Greene <python at bdurham.com> wrote:

> Background: I'm building a  rules engine for transforming rows of data
> being returned by csv DictReader, eg. each row of data is a dict of column
> name to value mappings. My rules are a list of rule objects whose
> attributes get referenced by rule specific methods. Each rule has an
> associated method which gets called based on rule name.
>

Could you share the API you've designed for this tool? An example of usage
will clarify what you intend.

One technique for applying a transformation function to each record
returned by a csv DictReader:

import csv
import math
from io import StringIO

f = StringIO('''\
a,b,c
1,2,3
4,5,6
''')

def pass_(value):
    return value

transforms = {
    'a': lambda v: math.sin(int(v)),
    'b': lambda v: math.cos(int(v)),
}

for row in csv.DictReader(f):
    print({k: transforms.get(k, pass_)(v) for k, v in row.items()})



Using __missing__ for your transform dict might be more elegant than .get
with a pass_ as default.



More information about the Python-list mailing list