eval

Running Python code contained in a string

走远了吗. 提交于 2019-12-17 11:53:17
问题 I'm writing a game engine using pygame and box2d, and in the character builder, I want to be able to write the code that will be executed on keydown events. My plan was to have a text editor in the character builder that let you write code similar to: if key == K_a: ## Move left pass elif key == K_d: ## Move right pass I will retrieve the contents of the text editor as a string, and I want the code to be run in a method in this method of Character: def keydown(self, key): ## Run code from

Dirt-simple PHP templates… can this work without `eval`?

喜夏-厌秋 提交于 2019-12-17 10:42:34
问题 Update- Thanks for all the responses. This Q is getting kind of messy, so I started a sequel if anyone's interested. I was throwing together a quick script for a friend and stumbled across a really simple way of doing templating in PHP. Basically, the idea is to parse the html document as a heredoc string, so variables inside of it will be expanded by PHP. A passthrough function allows for expression evaluation and function and static method calls within the string: function passthrough($s)

Python: How to pass arguments to the __code__ of a function?

霸气de小男生 提交于 2019-12-17 10:40:06
问题 The following works: def spam(): print "spam" exec(spam.__code__) spam But what if spam takes arguments? def spam(eggs): print "spam and", eggs exec(spam.__code__) TypeError: spam() takes exactly 1 argument (0 given) Given, that I don't have access to the function itself but only to a code object, how can I pass arguments to the code object when executing it? Is it possible with eval? Edit: Since most readers tend not to believe in the usefulness of this, see the following use case: I want to

js 对象 / json / jsonb / jsonp 区别

你。 提交于 2019-12-17 09:47:00
一、JSON vs JS 对象 1、区别 区别 Javascript 对象 Json 含义 对象的实例 一种数据格式(序列化格式) 传输 不能传输 可以 跨平台 传输,轻量级 格式 1.键不加引号、加单引号、双引号都行 2.值可以是函数、对象、字符串、数字、boolean 等 1. 键必须得加双引号 2. 值不能为函数/undefined/NaN 注: 序列化格式 :早期有 XML,后来基于 javascript 诞生了更轻量的 JSON,再后来还有YAML。 针对于科学使用的大量数据集合,例如气候,海洋模型和卫星数据,开发了特定的二进制序列化标准,例如HDF,netCDF和较旧的GRIB。 2、序列化 (1)序列化操作 正序列化 - JSON.stringify() 反序列化 - JSON.parse() 、(不推荐)eval() // 初始化 JS 对象 var obj_origin = { a: 1, b: "2", 'c': 3 } console.log(obj_origin) // { a: 1, b: '2', c: 3 } // 正序列化 var objStr = JSON.stringify(obj_origin) console.log(objStr) // {"a":1,"b":"2","c":3} // 反序列化 var obj = JSON.parse

JQuery getJSON - ajax parseerror

最后都变了- 提交于 2019-12-17 09:41:40
问题 I've tried to parse the following json response with both the JQuery getJSON and ajax: [{"iId":"1","heading":"Management Services","body":"<h1>Program Overview</h1><h1>January 29, 2009</h1>"}] I've also tried it escaping the "/" characters like this: [{"iId":"1","heading":"Management Services","body":"<h1>Program Overview <\/h1><h1>January 29, 2009<\/h1>"}] When I use the getJSON it dose not execute the callback. So, I tried it with JQuery ajax as follows: $.ajax({ url: jURL, contentType:

Assign to a bash array variable indirectly, by dynamically constructed variable name

笑着哭i 提交于 2019-12-17 06:49:11
问题 Bash script to create multiple arrays from csv with unknown columns. I am trying to write a script to compare two csv files with similar columns. I need it to locate the matching column from the other csv and compare any differences. The kicker is I would like the script to be dynamic to allow any number of columns to be entered and it still be able to function. I thought I had a good plan to solve this but turns out I'm running into syntax errors. Here is a sample of a csv I need to compare.

Why the open quote and bracket for eval('(' + jsonString+ ')') when parsing json string

删除回忆录丶 提交于 2019-12-17 05:07:10
问题 Can you please tell me the reason for this specific syntax structure eval('(' + jsonString+ ')') When parsing json text. Crockford says " The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax ." here. What does that mean? Can we avoid it? 回答1: The syntax ambiguity to which Crockford refers is that if an open curly brace is not found on expression context, it will be recognized like a block, and not like the start of an object literal. For example: {"foo":

Is it possible to execute a string in MySQL?

只愿长相守 提交于 2019-12-17 04:07:32
问题 I have to convert a MSSQL stored proc that passes a varchar that is a query: INSERT INTO Results EXEC (@Expresion); This isn't working. I'm pretty sure that EXEC and EXECUTE aren't MySQL commands, but CALL doesn't work either. Does anyone know if it's even possible to have something like JavaScript's eval function for MySQL? 回答1: EXECUTE is a valid command in MySQL. MySQL reference manual 回答2: I think you're looking for something like this: SET @queryString = ( SELECT CONCAT('INSERT INTO user

instantiate a class from a variable in PHP?

微笑、不失礼 提交于 2019-12-17 02:33:45
问题 I know this question sounds rather vague so I will make it more clear with an example: $var = 'bar'; $bar = new {$var}Class('var for __construct()'); //$bar = new barClass('var for __construct()'); This is what I want to do. How would you do it? I could off course use eval() like this: $var = 'bar'; eval('$bar = new '.$var.'Class(\'var for __construct()\');'); But I'd rather stay away from eval(). Is there any way to do this without eval()? 回答1: Put the classname into a variable first:

When (if ever) is eval NOT evil?

冷暖自知 提交于 2019-12-17 02:22:57
问题 I've heard many places that PHP's eval function is often not the answer. In light of PHP 5.3's LSB and closures we're running out of reasons to depend on eval or create_function . Are there any conceivable cases where eval is the best (only?) answer in PHP 5.3? This question is not about whether eval is evil in general, as it obviously is not. Summary of Answers: Evaluating numerical expressions (or other "safe" subsets of PHP) Unit testing Interactive PHP "shell" Deserialization of trusted