mercredi 14 mars 2018

Create if condition from string

i have a graphical interface where a user could enter a datafilter as a string, like:

>= 10 <= 100

I like to create an if-condition from this string.

The code i currently got splits the string into a list:

import re

def filter_data(string_filter)
     # \s actually not necessary, just for optical reason
     s_filter = r'(\s|>=|>|<=|<|=|OR|AND)'

     splitted_filter = re.split(s_filter, string_filter)
     splitted_filter = list(filter(lambda x: not (x.isspace()) and x, splitted_filter))

     print(splitted_filter)

with the given filter string above, the output would be:

['>=', '10', '<=', '100']

I now like to use this to create the if-condition of it.

My current idea would be to create nested if-statements.

Do you see a better solution?

Thanks!

Aucun commentaire:

Enregistrer un commentaire