-1

I'm attempting to write a dynamic python script where i am storing the if statement in a database. EX:

op='=='
stat1='4'
stat=int(stat1)
if stat==4: # Works
if stat + op + stat: # Does not Work

Is there a solution to format op so that it is readable by python?

  • 2
    I'm calling an `x/y problem` here. What problem are you actually trying to solve? You're trying to cross the boundary between source code and data, building a self-modifying program. This is dangerous, hard to debug, and stains your karma. – Prune Nov 22 '19 at 22:38
  • 1
    Look at the `operator` library. – cs95 Nov 22 '19 at 22:40
  • @Prune many interfaces between users and backend data involve users writing "pseudo queries" that one must parse, validate and then execute. If you've ever used a boolean query in a search engine, there's a parser on the backend. – Him Nov 22 '19 at 22:45
  • 1
    @Scott, sure, but if the author of that parser is sane, it isn't implemented by generating code in the host language to be directly interpreted, but instead an AST to evaluate. Building an interpreter isn't inherently evil, but concatenating strings to generate code (pretty much) *is*. – Charles Duffy Nov 22 '19 at 22:46
  • @Scott I'm well aware of that; I've written several parsers. In most cases, a posting with presentation such as this one, is trying to solve a problem that is better handled another way. If OP truly needs a parser, then the actual problem requires a deeper solution. – Prune Nov 22 '19 at 22:48
  • If you need to parse queries of any substantial complexity, you might want to use a [grammar parsing library](https://dexterritory.online/posts/parsing-context-free-grammars-using-lark). They make parsing arbitrarily large queries easy. – Him Nov 22 '19 at 22:48
  • @CharlesDuffy The OP did not ask for a way to concatenate strings to generate code. Possibly you've misinterpreted the problem. – Him Nov 22 '19 at 22:50
  • @Scott, it's not what they *asked for* (and I don't believe I ever claimed otherwise), but it *is* what they were *attempting* (`stat + op + stat`). And someone to whom that approach would even occur is not someone who needs to be building an interpreter, at least without retraining on the scale of a typical university-level compiler design course first. – Charles Duffy Nov 22 '19 at 23:23

1 Answers1

2

Use the operator module to get the corresponding functions for binary operations:

import operator

conversions = {
    '==': operator.eq,
    '+': operator.add
    ...
}

You would evaluate them like so:

op = conversions['==']
if op(stat, stat1):
    ...

Another method which is not as recommended is to use the underlying special method names to perform your operation:

conversions = {
    '==': '__eq__',
    '!=': '__ne__',
    '>=': '__ge__',
    ...
}

Now when evaluating your code:

op = conversions['==']
if getattr(stat, op)(stat1):
    ...

To create a full conversions list, use this website to get the method names of binary operations - arithmetic ones like addition and logical ones like greater than.

N Chauhan
  • 3,407
  • 2
  • 7
  • 21