string-evaluation

How to convert a string to a function in python?

大城市里の小女人 提交于 2019-12-27 19:17:33
问题 For example, if I have a function called add like def add(x,y): return x+y and I want the ability to convert a string or an input to direct to that function like w=raw_input('Please input the function you want to use') or w='add' Is there any way to use w to refer to the function add? 回答1: Since you are taking user input, the safest way is to define exactly what is valid input: dispatcher={'add':add} w='add' try: function=dispatcher[w] except KeyError: raise ValueError('invalid input') If you

How to convert a string to a function in python?

一笑奈何 提交于 2019-12-27 19:12:06
问题 For example, if I have a function called add like def add(x,y): return x+y and I want the ability to convert a string or an input to direct to that function like w=raw_input('Please input the function you want to use') or w='add' Is there any way to use w to refer to the function add? 回答1: Since you are taking user input, the safest way is to define exactly what is valid input: dispatcher={'add':add} w='add' try: function=dispatcher[w] except KeyError: raise ValueError('invalid input') If you

Bash - Running multiple commands using string evaluation

泄露秘密 提交于 2019-12-11 06:42:43
问题 I'm trying to create a shell script that concatenates multiple commands to a string and then execute the commands. The following succeeds (Creates the 'y' and 'z' files and prints ls output): $ /bin/touch /z && ls && /bin/touch /y But the following fails (Creates the 'y' and 'z' files, but also a '&&' and 'ls' files ) $ A="/bin/touch /z && ls && /bin/touch /y" $ $A It seems that by executing the line using $A the binary touch gets the rest of the string as parameters and the && operator does

mysql is array in multiple columns

一世执手 提交于 2019-11-28 11:35:21
I have a string in the form $string = 'London,Paris,Birmingham' and I want to search multiple columns for occurences of these values. For example WHERE events.name, events.cities, events.counties IN (".($string).") Can someone recommend me a simple and short way of doing something like this. Use the FIND_IN_SET function : WHERE ( FIND_IN_SET(events.name, mysql_real_escape_string($string)) > 0 OR FIND_IN_SET(events.cities, mysql_real_escape_string($string)) > 0 OR FIND_IN_SET(events.counties, mysql_real_escape_string($string)) > 0) 来源: https://stackoverflow.com/questions/3971622/mysql-is-array

mysql is array in multiple columns

谁说胖子不能爱 提交于 2019-11-27 06:21:14
问题 I have a string in the form $string = 'London,Paris,Birmingham' and I want to search multiple columns for occurences of these values. For example WHERE events.name, events.cities, events.counties IN (".($string).") Can someone recommend me a simple and short way of doing something like this. 回答1: Use the FIND_IN_SET function: WHERE ( FIND_IN_SET(events.name, mysql_real_escape_string($string)) > 0 OR FIND_IN_SET(events.cities, mysql_real_escape_string($string)) > 0 OR FIND_IN_SET(events

How to convert a string to a function in python?

安稳与你 提交于 2019-11-27 03:21:19
For example, if I have a function called add like def add(x,y): return x+y and I want the ability to convert a string or an input to direct to that function like w=raw_input('Please input the function you want to use') or w='add' Is there any way to use w to refer to the function add? Since you are taking user input, the safest way is to define exactly what is valid input: dispatcher={'add':add} w='add' try: function=dispatcher[w] except KeyError: raise ValueError('invalid input') If you want to evaluate strings like 'add(3,4)' , you could use safe eval : eval('add(3,4)',{'__builtins__':None}