0

I have got the following condition in string format over the network:

"value1+value2>value3"

I am getting value1, value2 and value3 from certain registers say

value1 = readRegister(100), 
value2 = readRegister(200), 
value3 = readRegister(300) 

So I have created follwing dictonary by reading the register values

test_dict = { value1:120, value2: 200, value3: 500}

If condition mentioned at starting is true then I have to perform certain operations.

As I mentioned at starting the condition is in string format, so I need to fetch and fetch the value of string mentioned in condition from the dictionary that I have created (i.e. test_dict) and then need to perform the arithematic operation mentioned in condition.

I am not sure how solve this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chets Goms
  • 19
  • 2
  • So basically you need to evaluate the expression that comes in string from using the values from yoru dict? – Padraic Cunningham Sep 01 '15 at 18:27
  • Where is `readRegister` defined? What is the type of each key in your dict? – Kevin Sep 01 '15 at 18:27
  • question is very unclear. – taesu Sep 01 '15 at 18:28
  • Take a look at [my answer to a similar question](https://stackoverflow.com/a/32022459/892383); it should get you started. – Cyphase Sep 01 '15 at 18:33
  • I love @NedBatchelders post: [Eval really is dangerous](http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html). – Peter Wood Sep 01 '15 at 18:40
  • @PeterWood, who said anything about `eval()`? – Cyphase Sep 01 '15 at 22:09
  • @Cyphase Not sure I understand. I posted a link about `eval`. Nobody else has mentioned it. – Peter Wood Sep 01 '15 at 22:48
  • @PeterWood, sorry, it just seemed a bit out of place because no one had mentioned `eval()` :). Though obviously this is something for which someone _might_ suggest `eval()`. – Cyphase Sep 01 '15 at 22:50
  • @Kevin readRegister() is just function to read value from registers. I have not given its implementation here as I don't find it necessary. I am more concerned about the the values i am getting from these register. The dict I have created is just a value it read from the specific register. – Chets Goms Sep 02 '15 at 07:48
  • @PadraicCunningham I want exactly what you have said. I am pretty novice in python. Is there any simple solution to get this working? – Chets Goms Sep 02 '15 at 07:51

1 Answers1

0

You could use eval.

First, make sure the input string is absolutely trustworthy. If a malicious user can specify its contents, they can erase your hard drive and send impolite emails to your mother. See the link in Peter Wood's comment for more information.

Then, call eval, passing d as the globals dict.

>>> d = {"value1": 120, "value2": 200, "value3": 500}
>>> s = "value1+value2>value3"
>>> eval(s, d)
False
>>> d = {"value1": 120, "value2": 200, "value3": 300}
>>> eval(s, d)
True

This is assuming that your dictionary has string literals as keys. It wasn't clear from your question; I'm assuming you just forgot to write the quote marks.

Kevin
  • 74,910
  • 12
  • 133
  • 166