Skip to content Skip to sidebar Skip to footer

Seek A Better Design Suggestion For A Trial-and-error Mechanism In Python?

See below data matrix get from sensors, just INT numbers, nothing specical. A B C D E F G H I J K 1 25 0 25 66 41 47 40 12 69 76 1 2 17 23

Solution 1:

If I understand what you're asking correctly, I probably wouldn't even venture down the Numbpy path as I don't think given your description that it's really required. Here's a sample implementation of how I might go about solving the specific issue that you presented:

l = [\
        {'a':25, 'b':0, 'c':25, 'd':66, 'e':41, 'f':47, 'g':40, 'h':12, 'i':69, 'j':76, 'k':1},\
        {'a':25, 'b':0, 'c':25, 'd':66, 'e':41, 'f':47, 'g':40, 'h':12, 'i':69, 'j':76, 'k':1}\
]
r = ['a=g=i', 'a=b', 'a=c']
res = []

# test all given rulesfor n inrange(0, len(r)):
        # i'm assuming equality here - you'd have to change this to accept other operators if needed
        c = r[n].split('=')
        vals = []
        # build up a list of values given our current rulefor e in c:
                vals.append(l[0][e])
        # using len(set(v)) gives us the number of distinct values
        res.append({'rangeID': 0, 'ruleID':n, 'violation':'Y'iflen(set(vals)) == 1else'N'})

print res

Output:

[{'violation': 'N', 'ruleID': 0, 'rangeID': 0}, {'violation': 'N', 'ruleID': 1, 'rangeID': 0}, {'violation': 'Y', 'ruleID': 2, 'rangeID': 0}]

http://ideone.com/zbTZr

There are a few assumptions made here (such as equality being the only operator in use in your rules) and some functionality left out (such as parsing your input to the list of dicts I used, but I'm hopeful that you can figure that out on your own.

Of course, there could be a Numpy-based solution that's simpler than this that I'm just not thinking of at the moment (it's late and I'm going to bed now ;)), but hopefully this helps you out anyway.

Edit:

Woops, missed something else (forgot to add it in prior to posting) - I only test the first element in l (the given range).. You'd just want to stick that in another for loop rather than using that hard-coded 0 index.

Solution 2:

You want to look at Numpy matrix for data structures like matrix etc. It exposes a list of functions that work on matrix manipulation.

As for rule / range generator I am afraid you will have to build your own domain specific language to achieve that.

Post a Comment for "Seek A Better Design Suggestion For A Trial-and-error Mechanism In Python?"