eval

JSON.parse与eval的区别

时光总嘲笑我的痴心妄想 提交于 2019-11-30 16:06:43
JSON.parse与eval和能将一个字符串解析成一个JSON对象,但还是有挺大区别。 测试代码 var A = "{ a: 1 , b : 'hello' }"; var B = "{ 'a': 1 , 'b' : 'hello' }"; var C = "{'a':1,'b':'hello'}"; var D = '{"a":1,"b":"hello"}'; var E = '{ "a" : 1 , "b" : "hello" }'; var F = '{ "a" : 1 ,\n "b" : "hello" }'; var G = '{ "a" : 1 , "b" : window.location.href="https://www.baidu.com" }'; JSON.parse执行: 例:JSON.parse(A); A、B、C、G都不可转,D、E、F都可以。 eval执行: 例:eval("("+A+")"); A到G都可以转,特别到G时,页面还跳转到百度了。 JSON.parse 上面的演示例子可以看出,这方法只能解析属性名是双引号包裹的字符串对象,并会忽略换行和空格(值外面)。 但是,从 MDN 对JSON的描述,能解析的JSON字符串的条件完整如下: JavaScript类型 JSON与之区别 对象和数组 属性名称必须用双引号包裹; 最后一个属性后面不能有逗号

linux环境变量展开

大憨熊 提交于 2019-11-30 14:27:05
#!/bin/bash expand_env(){ to_eval=$1 final_value=$(eval echo $to_eval) until [ "$final_value" = "$to_eval" ] do to_eval=$final_value final_value=$(eval echo $to_eval) done echo $final_value } 来源: https://my.oschina.net/guyongquan/blog/3111785

How safe is SymPy's sympify(<string>).evalf()?

梦想的初衷 提交于 2019-11-30 13:58:28
We know Python's eval() is evil http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html and threads throughout StackOverflow suggest to use SymPy's evalf() . As a Python newbie, I can't really convince myself that evalf() is safe as I lack the skills. Can anyone elaborate on what evalf() does (different)? There is nothing common between python eval and sympy evalf (the latter is for calculating the numeric value of sympy expression trees and it has nothing to do with parsing, while eval is all about parsing a string and evaluating it as if it is code). On the other hand, sympify is

Unexpected $end in eval()'d code

若如初见. 提交于 2019-11-30 12:50:45
I hate to ask such a specific question, but I'm getting an error I can't figure out. This is in a cron job which runs on the hour. I'm creating an array of tasks, each of which has a date check which is supposed to be eval()'d. $todo = array(); $todo[] = array( "date('z')%3 == 0", "Task 1" ); $todo[] = array( "date('N') == 1", "Task 2" ); foreach( $todo as $task ) { if( eval($task[0]) ) { echo $task[1]; } } For some reason the eval() line is giving me this error. Note that I am getting this error for both tasks. Parse error: syntax error, unexpected $end in /file.php(21) : eval()'d code on

20190919-6 四则运算试题生成

▼魔方 西西 提交于 2019-11-30 12:31:22
此作业要求参见: https://edu.cnblogs.com/campus/nenu/2019fall/homework/7631 Git地址: https://e.coding.net/thiking/si_ze_yun_suan.git 结对队友:董亚辉 功能1. 四则运算 1.1功能描述 支持出题4个数的四则运算题目,所有题目要求作者有能力正确回答 (提示:1/3 != 0.33333333333333333333333333333333,而是无限长)为了快出成果,你快速造个控制台的版本,包括以后改版成更优秀界面的核心功能,并考虑到扩展。你在老师面前作如下表演 (数字你用excel验算过了,UI的卖萌部分你还没有想好) 。 1.2重难点   (1)如何随机产生计算符号,   (2)如何将式子进行拼凑,   (3)如何将产生的式子进行计算,   (4)如果计算结果产生的是小数如何转化为分数 1.3收获: 学习了eval函数给完成作业带来了很大的便捷。 1.4 代码片段: def creat_equation(): #功能一生成随机式子 ops = ['+', '-', '*', '/'] num1 = r(1, 9) #产生随机数 num2 = r(1, 9) num3 = r(1, 9) num4 = r(1, 9) ops1 = r(0, 2) ops2 = r(0,

Parsing a string which represents a list of tuples

落花浮王杯 提交于 2019-11-30 11:27:11
I have strings which look like this one: "(8, 12.25), (13, 15), (16.75, 18.5)" and I would like to convert each of them into a python data structure. Preferably a list (or tuple) of tuples containing a pair of float values. I could do that with eval("(8, 12.25), (13, 15), (16.75, 18.5)") which gives me a tuple of tuples, but I don't think naively evaluating external information would be a wise decision. So I wondered what an elegant pythonic solution might look like. >>> import ast >>> print ast.literal_eval("(8, 12.25), (13, 15), (16.75, 18.5)") ((8, 12.25), (13, 15), (16.75, 18.5)) def parse

Best Technique for Multiple Eval Fields in Gridview ItemTemplate?

拜拜、爱过 提交于 2019-11-30 11:26:20
问题 What is the best way to use multiple EVAL fields in a GridView ItemTemplate? Looking to have some control over formatting for appearance as well as setting up hyperlinks/javascript etc. 回答1: Even clearer, IMO, is: <%# String.Format("{0} - {1}", Eval("Name1"), Eval("Name2")) %> 回答2: I had previously used this (bad, I know): <%# Eval("Name1", "{0} - ")%> <%#Eval("Name2")%> Result = 'John - Smith' But just discovered that I can also put TWO (or more) Evals in the same data-bound group: <%#Eval(

JS教程(6)-- JS 严格模式、this关键字、JS调试、JS代码约定

对着背影说爱祢 提交于 2019-11-30 11:12:47
2019.9.24: 学习内容:JS 严格模式、this关键词 一、JS 严格模式: "use strict" "use strict" 是 JavaScript 1.8.5 中的新指令(ECMAScript version 5)。 声明严格模式 通过在脚本或函数的开头添加 "use strict"; 来声明严格模式。 在脚本开头进行声明,拥有全局作用域(脚本中的所有代码均以严格模式来执行) 为什么使用严格模式? 严格模式使我们更容易编写“安全的” JavaScript。 严格模式把之前可接受的“坏语法”转变为真实的错误。 举例来说, 在普通的 JavaScript 中,错打变量名会创建新的全局变量。在严格模式中,此举将抛出错误,这样就不可能意外创建全局变量。 在普通 JavaScript 中,如果向不可写属性赋值,开发者不会得到任何错误反馈。 在严格模式中,向不可写的、只能读取的、不存在的属性赋值,或者向不存在的变量或对象赋值,将抛出错误。 严格模式中不允许的事项: 1、在不声明变量的情况下使用变量。 2、对象也是变量,在不声明对象的情况下使用对象也是不允许的。 3、删除变量(或对象)是不允许的。(delete不能用了) 4、删除函数是不允许的。 5、重复参数名是不允许的。 6、八进制数值文本是不允许的。(例如: 010) 7、转义字符是不允许的。(例如:/010) 8

powershell: how to evaluate a string read from a file

雨燕双飞 提交于 2019-11-30 11:11:41
file a.txt is: delete from test_$suffix $a=get-content a.txt $suffix="tableA" how to manipulate the variable to set it as delete from test_tableA $a=get-content a.txt $suffix="tableA" $ExecutionContext.InvokeCommand.ExpandString($a) Stephan Unrau Invoke-Expression is the equivalent. $strExpression = "5 + 5 -eq 10" Invoke-Expression $strExpression True See http://technet.microsoft.com/en-us/library/ee176880.aspx for more information. Here's one way. Variables in a double-quoted here-string get substituted automatically. Just be sure your input file conforms to the PS rules for here-strings.

scope of eval function in python

拥有回忆 提交于 2019-11-30 11:04:34
Consider the following example: i=7 j=8 k=10 def test(): i=1 j=2 k=3 return dict((name,eval(name)) for name in ['i','j','k']) It returns: >>> test() {'i': 7, 'k': 10, 'j': 8} Why eval does not take into consideration the variables defined inside the function? From the documentation, optionally you can pass a globals and a locals dictionary. What does it means?Finally, how can I modify this small case to make it work? Generators are implemented as function scopes : The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this