eval

22.json&pickle

走远了吗. 提交于 2019-12-06 06:22:16
json 之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了,所以eval的重点还是通常用来执行一个字符串表达式,并返回表达式的值。 import json x="[null,true,false,1]" print(eval(x)) print(json.loads(x)) 什么是序列化? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling,在其他语言中也被称之为serialization,marshalling,flattening等等,都是一个意思。 序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上。 反过来,把变量内容从序列化的对象重新读到内存里称之为反序列化,即unpickling。 json 如果我们要在不同的编程语言之间传递对象,就必须把对象序列化为标准格式,比如XML,但更好的方法是序列化为JSON,因为JSON表示出来就是一个字符串,可以被所有语言读取,也可以方便地存储到磁盘或者通过网络传输。JSON不仅是标准格式,并且比XML更快,而且可以直接在Web页面中读取,非常方便。 JSON表示的对象就是标准的JavaScript语言的对象

eval in anchor tag href

ぐ巨炮叔叔 提交于 2019-12-06 06:21:21
问题 What I am trying to achieve is to use an Eval as parameter in my anchor tag's href. The anchor is nested inside a repeater, why I cannot use the code behind to achieve this. I have tried a few things without any luck. <a href="http://MyWebsite/ActiveUsers?ID=InsertEvalHere"><%# Eval("Name")%></a> The following code below is what I have tried to do: <a href="<% "http://MyWebsite/ActiveUsers?ID=" + DataBinder.Eval(Container.DataItem("ID"))%>"><%# Eval("Name")%></a> <a href="<% "http://MyWebsite

Can I strictly evaluate a boolean expression stored as a string in Java?

不想你离开。 提交于 2019-12-06 05:16:37
问题 I would like to be able to evaluate an boolean expression stored as a string, like the following: "hello" == "goodbye" && 100 < 101 I know that there are tons of questions like this on SO already, but I'm asking this one because I've tried the most common answer to this question, BeanShell, and it allows for the evaluation of statements like this one "hello" == 100 with no trouble at all. Does anyone know of a FOSS parser that throws errors for things like operand mismatch? Or is there a

Convert flattened key->value pairs to a nested object

℡╲_俬逩灬. 提交于 2019-12-06 04:38:56
What would be the easiest way to convert the following key->value object "array" to a proper "JSON" style object? Example below would be converting input into graph. var input = { "graph.default.seriesColor" : ["#cccccc", "#3c3c3c"], "graph.default.stackSeries" : false, "graph.default.title.text" : "Hello!", "graph.default.title.show" : false, "graph.default.axesDefaults.show" : true, "graph.default.axesDefaults.min" : 17, "graph.default.axesDefaults.max" : 20, }; var graph = { default: { seriesColor: ["#cccccc", "#3c3c3c"], stackSeries: false, title: { text: "Hello!", show: false },

eval(call) different from typing the expression into the console

﹥>﹥吖頭↗ 提交于 2019-12-06 04:34:10
问题 given the call below, why does eval(call) yield results different from simply typing the expression right into the console x <- list(Vect=seq(3), Mat=matrix(seq(9), ncol=3)) ## This call came from the source to `as.data.table.list()` theCall <- as.call(c(expression(data.frame), x)) theCall # data.frame(Vect = 1:3, Mat = 1:9) data.frame(Vect=1:3, Mat=1:9) # Vect Mat # 1 1 1 # 2 2 2 # 3 3 3 # 4 1 4 # 5 2 5 # 6 3 6 # 7 1 7 # 8 2 8 # 9 3 9 eval(theCall) # Vect Mat.1 Mat.2 Mat.3 # 1 1 1 4 7 # 2 2

Accessing Global Vars with Window

扶醉桌前 提交于 2019-12-06 04:22:47
Why doesn't window.x print out 10 ? eval("var x = 10;"); console.log(window.x); // undefined console.log(x); // 10 http://jsfiddle.net/kzd4z/1/ You have selected onLoad in the side panel, which wraps everything in an anonymous function. If you pick "No wrap" it works. Demo: http://jsfiddle.net/kzd4z/2/ You can see this by viewing source: //<![CDATA[ window.onload=function(){ eval("var x = 10;"); console.log(window.x); // undefined console.log(x); // 10 }//]]> brbcoding Expanding on @Dennis' answer, because using "No Wrap" wraps your entire function within an anonymous function, x is now part

Perl: safe eval?

百般思念 提交于 2019-12-06 04:15:59
问题 I'm curious if there is any good information on performing restricted evals. Looking at the docs, there is a use Safe that has a reval method, but I'm not sure how safe that is. What I want to do is to be able to pass various conditional statements as a string to a function w/o the source abusing the eval. For instance: sub foo { my $stmt = shift; my $a = 3; say eval($stmt)?"correct":"wrong") , "($stmt)"; } foo( q{1 == $a} ); foo( q{$a =~ /3/ ); foo( q{(sub {return 3})->() eq 3} ); Would use

Caching includes in PHP for iterated reuse

╄→尐↘猪︶ㄣ 提交于 2019-12-06 04:14:23
Is there a way to cache a PHP include effectively for reuse, without APC, et al? Simple ( albeit stupid ) example: // rand.php return rand(0, 999); // index.php $file = 'rand.php'; while($i++ < 1000){ echo include($file); } Again, while ridiculous, this pair of scripts dumps 1000 random numbers. However, for every iteration, PHP has to hit the filesystem ( Correct? There is no inherit caching functionality I've missed, is there? ) Basically, how can I prevent the previous scenario from resulting in 1000 hits to the filesystem? The only consideration I've come to so far is a goofy one, and it

PHP's create_function() versus just using eval()

烈酒焚心 提交于 2019-12-06 04:12:58
问题 In PHP you have the create_function() function which creates a unique named lambda function like this: $myFunction = create_function('$foo', 'return $foo;'); $myFunction('bar'); //Returns bar Is this actually any better (apart from being more easy) then just doing: do{ $myFunction = 'createdFunction_'.rand(); } while(function_exists($myFunction)); eval("function $myFunction(\$foo) { return \$foo; }"); $myFunction('bar'); //Returns bar Is create_function really better? (apart from the fact

Ruby eval behaves differently in irb versus in a file

天涯浪子 提交于 2019-12-06 04:06:22
问题 This code works in irb: irb(main):037:0> eval <<-EOS irb(main):038:0" #{attribute} = "host" irb(main):039:0" puts machine irb(main):040:0" EOS host => nil irb(main):041:0> puts machine host => nil irb(main):042:0> puts attribute machine => nil irb(main):043:0> however when I try to execute the same code as a ruby script I get the following error: ../autosys/convert_jil_to_zapp.rb:40: undefined local variable or method `machine' for main:Object (NameError) from ../autosys/convert_jil_to_zapp