eval

python重要函数eval

梦想的初衷 提交于 2020-01-06 14:12:25
1.参数会作为一个 Python 表达式(从技术上说是一个条件列表)被解析并求值 >>> x = 1 >>> eval('x+1') 2 2.去除字符串两边的引号 >>> a='"srting"' >>> print(a) "srting" >>> b=eval(a) >>> print(b) srting 也可以用 >>> a.strip('"') 'srting' 3.字符串转字典 >>> a= "{'name':'linux','age':18}" >>> type(a) <type 'str'> >>> b=eval(a) >>> b {'age': 18, 'name': 'linux'} >>> type(b) <type 'dict'> 4.传递全局变量 >>> a= "{'name':'linux','age':age}" >>> b=eval(a,{"age":1822}) >>> b {'age': 1822, 'name': 'linux'} >>> type(b) <type 'dict'> 5.传递本地变量 >>> a= "{'name':'linux','age':age}" >>> age=18 >>> b=eval(a,{"age":1822},locals()) >>> b {'age': 18, 'name': 'linux'} 来源:

Is it safe to use eval in javascript when it only executes server-side data?

强颜欢笑 提交于 2020-01-06 14:07:00
问题 Yes, it's a kinda duplicate of this question, but the answer given by @apsillers is a super-answer as it points a new aspect of the problem that have not been told in the previous question. Another helpful SO question that helped me to undestand better what exactly are the eval() security concerns is that question I use eval() in my javascript code and I'm wondering if I should let it down. Principe : I have an ajax call which initialize the first connexion between a browser client and the

why JavaScript is not displaying parsed json data?

那年仲夏 提交于 2020-01-06 07:05:03
问题 This is format of JSON data: [{"options":"smart_exp"},{"options":"user_int"},{"options":"blahblah"}] that I receive through getjson from server. I need to append json with user input. I am trying to do it in this way: 1st convert it into javascript object, append it with user input, again convert to json object & send it back to server for database update. I have converted json to javaScript object using eval(). Now not able to manipulate javascript object. If I convert javascript object back

Print expression and also echo it

大憨熊 提交于 2020-01-06 06:04:32
问题 I mean to define a function print_echo that replaces print , such that in addition to printing the result of an expression it prints the expression itself. If I simply pass the expression as a string and use eval inside print_echo , it will not know any variable local to the caller function. My current code is def print_echo( expr ) : result = eval( expr ) print( expr + ' => ' + str( result ) + ' ' + str( type( result ) ) ) return But when using def my_func( params ) : a = 2 print_echo( "a" )

ImplementIon of eval() parser and 2d array in Java

随声附和 提交于 2020-01-06 03:22:08
问题 I have really stuck on two Java related issues. One simple and one more difficult. Regarding the creation of a 2D array, I initialize a table like this: private String [][] table_of_classifiers = null; and then, within a function, I fill its contents like this: String [][] table_of_classifiers = { {"x1","x","x","x","x"}, {"x2","x","x","x","x"}, {"x3","x","x","x","x"}, {"x4","x","x","x","x"}, {"x5","x","x","x","x"}, {"x6","x","x","x","x"}, }; But as you can guess the second table overwrites

Can an inserted <script> tag be run multiple times without using eval in JavaScript?

左心房为你撑大大i 提交于 2020-01-06 02:32:10
问题 I have to use a plugin which bundles js files inside html files (gadgets). For one use case I need to drop and re-instantiate a gadget to run updated code. So say I have foo.html : <html> <head> <script src="foo.js"></script> </head> <body>...</body> </html> and foo.js which is the file being injected into my actual document's head: alert("hello"); Problem is I can only cachebust the html file dynamically and declare my gadget as foo.html?x=123 , but the JS file I'm after will still be foo.js

快速使用Jscex改进JavaScript异步编程体验代码

。_饼干妹妹 提交于 2020-01-05 02:59:26
JavaScript是互联网时代编程语言的霸主,统领浏览器至今已有许多年头,而这股风潮很可能随着HTML 5的兴起而愈演愈烈。如今JavaScript更是在Node.js的帮助下进军服务器编程领域。“单线程”和“无阻塞”是JavaScript的天性, 因此任何需要“耗时”的操作,例如等待、网络通信、磁盘IO都只能提供“异步”的编程接口。尽管这对服务器的伸缩性和客户端的响应能力都大有脾益,但是异 步接口在使用上要比传统的线性编程困难许多,因此也诞生了如jQuery Deferred这样的辅助类库。Jscex的主要目的也是简化异步编程,但它使用了一种与传统辅助类库截然不同的方式,尽可能地将异步编程体验带领到新的高度。 JavaScript编程几乎总是伴随着异步操作,传统的异步操作会在操作完成之后,使用回调函数传回结果,而回调函数中则包含了后续的工作。这也 是造成异步编程困难的主要原因:我们一直习惯于“线性”地编写代码逻辑,但是大量异步操作所带来的回调函数,会把我们的算法分解地支离破碎。此时我们不能 用if来实现逻辑分支,也不能用while/for/do来实现循环,更不用提异步操作之间的组合、错误处理以及取消操作 pdf 了。 快速入门:排序动画 我们先来看一个简单的例子。“冒泡排序”是最常见的排序算法之一,它的JavaScript实现如下: view source print ? 01

js 实现call和apply方法,超详细思路分析

北城以北 提交于 2020-01-05 00:36:52
壹 ❀ 引 我在 五种绑定策略彻底弄懂this 一文中,我们提到 call,apply,bind 属于显示绑定,这三个方法都能直接修改this指向。其中call与apply比较特殊,它们在修改this的同时还会直接执行方法,而bind只是返回一个修改完this的 boundFunction 并未执行,那么今天我们来讲讲如果通过JavaScript模拟实现call与apply方法。 贰 ❀ 关于call与apply1 贰 ✿ 壹 call与apply区别 除了都能改变this指向并执行函数,call与apply唯一区别在于参数不同,具体如下: var fn = function (arg1, arg2) { // do something }; fn.call(this, arg1, arg2); // 参数散列 fn.apply(this, [arg1, arg2]) // 参数使用数组包裹 call第一参数为this指向,后续散列参数均为函数调用所需形参,而在apply中这些参数被包裹在一个数组中。 贰 ✿ 贰 使用场景 call与apply在日常开发中非常实用,我们在此列举几个实用的例子。 检验数据类型: function type(obj) { var regexp = /\s(\w+)\]/; var result = regexp.exec(Object

how to avoid eval in python for string conversion?

给你一囗甜甜゛ 提交于 2020-01-04 14:11:35
问题 I'm confronted with an "eval" problem and I would like your advice, I know eval is wrong and I wish to avoid it but I don't know how... I have some python objects stored in a database, they are stored as strings and I transform them into actual objects with eval. As an example, my objects could be some simple strings like "[2,1,3,4]" or "[[None,None],None]" -- they correspond to different types of object, they don't follow a common pattern. I have to store them as human-readable objects as

How to eval a string containing an equal symbol?

≯℡__Kan透↙ 提交于 2020-01-04 12:37:25
问题 I have some issues with the eval function. I have a list like, for example, list1 = [('a',1), ('b',2), ('c',3)] and I would like to assign each value of a tuple to the first element: for el in list1 : eval(el[0]) = el[1] How can I do this? 回答1: You could do this: exec('%s = %s' % el) But don't. Really, don't. You don't need dynamic local variables, you need a dictionary: my_dict = dict(list1) 回答2: You don't need eval for that. You can access local environment directly by calling the vars