eval

Python variable variables without eval?

会有一股神秘感。 提交于 2019-12-02 02:46:59
问题 Is there is a way to access variables using a variable string in python? For instance, I would like a neater way than using eval for the following: def toggleListButtons (self): buttons = ["flip", "remove", "removeAll", "delete", "deleteAll", "loadDirectory"] for button in buttons: eval("self." + button + "Button.setEnabled(!self." + button + "Button.isEnabled())") 回答1: What you're looking for is the getattr() built-in function. There is also hasattr() and setattr(). button = getattr(self,

Using eval method to get class from string in Firefox

你。 提交于 2019-12-02 02:13:38
What I tried (which works in chrome) var class_str = "class Test {};"; var a = eval(class_str); console.log(new a()); Raises following error in Firefox 46: TypeError: a is not a constructor a is undefined and using new A() returns ReferenceError: A is not defined . What is different on Firefox? Putting the whole class string in parentheses works. Fixed code: var class_str = "(class Test {})"; var a = eval(class_str); console.log(new a()); I tried another method that works just as using parentheses and parentheses seems much simpler as it doesn't pollute global names. result = eval(`(class a{})

Expression eval(“function(x) { return x*x}”) breaks node.js console

本秂侑毒 提交于 2019-12-02 01:41:13
When I type something like this in node.js console: var f = eval("function(x) { return x*x}"); It doesn't evaluate the expression and waits for me to type something else. Why is this happening? Why can't I type another expression after this one? function(x) { return x*x} is an error (type it in the console to check that), so Node's REPL waits for more. If you want to build and assign the function, you must eval an expression, that is a statement returning a value. The usual solution is to close the function expression with parenthesis. You may write var f = eval("(function(x) { return x*x})");

Getting nested obj value

試著忘記壹切 提交于 2019-12-02 01:10:54
Given the following obj: var inputMapping = { nonNestedItem: "someItem here", sections: { general: "Some general section information" } }; I'm writing a function to get that data by passing in a string "nonNestedItem" or in the nested case "sections.general" . I'm having to use an eval and I was wondering if there was maybe a better way to do this. Here is what I have so far and it works okay. But improve! function getNode(name) { var n = name.split("."); if (n.length === 1) { n = name[0]; } else { var isValid = true, evalStr = 'inputMapping'; for (var i=0;i<n.length;i++) { evalStr += '["'+ n

Python variable variables without eval?

会有一股神秘感。 提交于 2019-12-02 01:05:38
Is there is a way to access variables using a variable string in python? For instance, I would like a neater way than using eval for the following: def toggleListButtons (self): buttons = ["flip", "remove", "removeAll", "delete", "deleteAll", "loadDirectory"] for button in buttons: eval("self." + button + "Button.setEnabled(!self." + button + "Button.isEnabled())") What you're looking for is the getattr() built-in function. There is also hasattr() and setattr() . button = getattr(self, 'flipButton') button.setEnabled(True) 来源: https://stackoverflow.com/questions/12111358/python-variable

what are the issues javascript eval can pose

不问归期 提交于 2019-12-02 01:00:57
i tried googling but didnt get a very specific answer.. then again, i might be not using the right keywords.. can someone point out the "security" issues javascript eval can cause? with examples with be very nice. will also do if you can point to an existing web resource which does the same. Edit : I only need the security implications for eval. eval() may be a sign of poor design. For instance, sometimes people use it to access object properties because they don't know you can use the [] notation, i.e., eval('obj.' + prop_name). It's also a source of XSS holes if you eval() user content,

代码不规范,同事皮锤现(下)

£可爱£侵袭症+ 提交于 2019-12-02 00:20:25
这篇是代码规范系列的最后一篇了,前两篇说了一些注意的点,最后一篇再来补充一下子,开工 参数验证    关于参数验证这一点我想大家都不陌生了,React,Vue都可以对父组件传进来的参数进行验证,而函数里面通常也可以对传进来的参数进行验证,当然,不加验证只要传参正确也是没有问题的,但是假设我们要对传入的参数类型进行限制,还是需要把参数验证加上的,起码能让我们的程序写起来更加规范,与同事之间进行迭代开发也能更快的上手。 主动抛错    啥叫主动抛错呢,跟主动承认错误是一个意思吗,做错了事情赶紧承认,在代码中我们可以通过throw来主动抛出错误,这里可能有的同学要问,当代码出错,浏览器上不是会自动抛出错误吗?为何还要脱了裤子放屁多此一举呢,其实不然,浏览器的错误可能没有我们自己定义的详细,毕竟你自己抛出的错误,想抛出什么就抛出什么,这样后期代码中一旦抛出错误,就能快速的定位到问题的原因。   我们常用throw和try catch来抛出错误,而常见的错误类型也不外乎以下七种   Error:所有错误的基本类型   ReferenceError: 期望的对象不存在时发生,比如变量未定义直接使用   TypeError: 变量不是期望的类型时抛出,例如a是一个字符串,调用a()   EvalError:eval函数执行发生错误时抛出,现在已经非常不提倡用eval了   SyntaxError

日常学习 ‘sort()与sorted()’区别 及 ‘eval()与exec()’区别

删除回忆录丶 提交于 2019-12-01 23:24:17
日常新学习知识点总结 sort与sorted的区别(面试中可能被问到) 先一句话总结: sort是应用在列表(list)上的方法,可以列表进行排序处理,注:是永久性的处理,如下实例: lis = [1, 3, 45, 34, 23, 55, 18] lis.sort() # 永久性的改变顺序 print(lis.sort()) # None #为了避免人们误解,python设计者用返回值为None的方法告诉我们,原来的列表已经被改变了。 print(lis) #[1, 3, 18, 23, 34, 45, 55] 验证 sort() 是永久性改变顺序 sorted 是 可以对所有的可迭代对象进行排序操作 lis = [1, 3, 45, 34, 23, 55, 18] lis1 = sorted(lis) print(lis1) #[1, 3, 18, 23, 34, 45, 55] 进行了排序,临时性的 print(lis) #[1, 3, 45, 34, 23, 55, 18] 验证 sorted 临时排序 aaa=sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) print(aaa) #[1, 2, 3, 4, 5] 这里没看懂 list 的 sort 方法返回的是对已经存在的列表进行操作(返回值是None)

Binding Eval with an ImageURL in ASP.NET

不问归期 提交于 2019-12-01 22:59:53
I'm trying to bind an image using Eval() with VB.NET and ASP.NET, but am running into issues: Code snippet <bri:ThumbViewer Id="Th1" runat="server" ImageUrl='<%# Eval("Name", "~/SiteImages/ram/3/{0}") %>' Height="100px" Width="100px" /> I set strImagePath in the code-behind as: strImagePath ="~/SiteImages/ram/3/" How can I replace: ~/SiteImages/ram/3/{0} with the variable strImagePath ? simply use <asp:Image id="abc" ImageUrl =<%# string.Format("~/SiteImages/ram/3/{0}",Eval("imagepath"))%> imagepath could be from datatable or cs I personally prefer to do these things in the codebehind directly

Convert String to List in Python Without Using Eval?

淺唱寂寞╮ 提交于 2019-12-01 22:49:51
问题 I have a string, something like this: "[['Cheese', 72], ['Milk', 45], ['Bread', 22]]" . I want to convert this to a list. I know I can use eval(string) to get the list, but eval scares me because of its potential for catastrophe (and because I can get a non-list as valid output). Is there another saner/safer way to turn this string into a list? I know it's a list and anything that isn't a list is invalid data (and should be checked for and/or throw an error). 回答1: If you insist on doing it