eval

Evaluating a mathematical expression without eval() on Python3

倾然丶 夕夏残阳落幕 提交于 2021-02-07 14:14:56
问题 I'm working on a "copy-paste calculator" that detects any mathematical expressions copied to the system clipboard, evaluates them and copies the answer to the clipboard ready to be pasted. However, while the code uses the eval()-function, I'm not terribly concerned considering the user normally knows what they are copying. That being said, I want to find a better way without giving the calculations a handicap (= eg. removing the ability to calculate multiplications or exponents). Here's the

evaluate boolean values in Java

微笑、不失礼 提交于 2021-02-07 13:39:43
问题 I am trying to evaluate the following from a string boolean value = evaluate("false || true && true && false || true"); I need to get a boolean value of true for this one. Any ideas on how to solve this problem in the most efficient way? 回答1: String value = ("false || true && true && false || true"); boolean result = false; for (String conj : value.split("\\|\\|")) { boolean b = true; for (String litteral : conj.split("&&")) b &= Boolean.parseBoolean(litteral.trim()); result |= b; } System

evaluate boolean values in Java

醉酒当歌 提交于 2021-02-07 13:38:40
问题 I am trying to evaluate the following from a string boolean value = evaluate("false || true && true && false || true"); I need to get a boolean value of true for this one. Any ideas on how to solve this problem in the most efficient way? 回答1: String value = ("false || true && true && false || true"); boolean result = false; for (String conj : value.split("\\|\\|")) { boolean b = true; for (String litteral : conj.split("&&")) b &= Boolean.parseBoolean(litteral.trim()); result |= b; } System

How to pass parameters to an eval based function inJavascript

两盒软妹~` 提交于 2021-02-07 07:11:27
问题 I am storing function body in string with function name. function fnRandom(lim){ var data=[]; for(var i=0;i<lim;i++) { data=data.concat(Math.floor((Math.random() * 100) + 1)); } return data; } After selecting the functionName from a drop down I use eval to execute function body. JSON.stringify(eval(this.selectedFunction.body)); I want to pass 'lim' to this execution or can I use functionName as initiating point for execution somehow? 回答1: Use Function constructor var body = "console.log

clojure resolving function from string name

*爱你&永不变心* 提交于 2021-02-07 05:22:06
问题 In clojure 1.2RC1, I wish to obtain a function based on its name as string and evaluate it. Function definition (ns my-ns) (defn mycar [x] (first x)) The following worked: ((ns-resolve *ns* (symbol "mycar")) '(3 4)) ((intern *ns* (symbol "mycar")) '(3 4)) ((eval (symbol "mycar")) '(3 4)) but they seem ugly. Is there a better way? If not, which of the above is the most idiomatic? 回答1: This worked for me without using eval: user> (defn mycar [x] (first x)) #'user/mycar user> ((resolve (symbol

eval(): can't assign to function call

点点圈 提交于 2021-02-05 08:37:09
问题 Yet another question for my cookie clicker... Here is the code I made that produces an error: cps=cps+bcps[buych] c=c-bprice[buych] eval(buych)=eval(buych)+1 cps is a variable, c is a variable, b1,b2,b3,b4,b5 are variables buych is a string (Which I get from an input() command) bcps and bprice are maps (shown below) bcps = {"b1":'1', "b2":'5', "b3":'10', "b4":'20', "b5":'25'} bprice = {"b1":'10', "b2":'20', "b3":'30', "b4":'40', "b5":'50'} So, what I'm trying to achieve is: -Take the input

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases Calls: lm -> lm.fit

十年热恋 提交于 2021-01-29 06:13:05
问题 I am trying to run an R application, but I receive the following first error : Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Calls: lm -> lm.fit The code which generates the error is : pppb = lm(Exchange.rate.change ~ Inflation.difference) I am new to R and is really hard for me to find the mistake, so any help it is really appreciated. This is a minimal data set: Country Inflation.difference Exchange.rate.change Developed Australia -1.235100000e

R - Checking if a string is a valid mathematical expression using non-standard evaluation

爱⌒轻易说出口 提交于 2021-01-28 12:16:02
问题 I would like to check if the strings below are valid mathematical expressions: s1 = 'sin(x)' s2 = 'sin(x*m)' s3 = 'sin' s4 = 'sin(xm)' By 'valid', I mean the expression is a combination of operators (must be used in conjunction with variables or constants) variables x and/or m constants. By this definition s1 and s2 are valid while s3 and s4 are not. To identify if a string is valid, I wrote a function checkFxn that first attempts to convert the string into a call or one of its parts. If

Parse and Evaluate Column of String Expressions in R?

丶灬走出姿态 提交于 2021-01-27 11:39:58
问题 How can I parse and evaluate a column of string expressions in R as part of a pipeline? In the example below, I produce my desired column, evaluated . But I know this isn't the right approach. I tried taking a tidyverse approach. But I'm just very confused. library(tidyverse) df <- tibble(name = LETTERS[1:3], to_evaluate = c("1-1+1", "iter+iter", "4*iter-1"), evaluated = NA) iter = 1 for (i in 1:nrow(df)) { df[i,"evaluated"] <- eval(parse(text=df$to_evaluate[[i]])) } print(df) # # A tibble: 3

python中eval与json.loads对json的处理

*爱你&永不变心* 提交于 2021-01-22 08:48:24
JSON有两种结构: “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。 eval与json.loads都可以将 大部分 的JSON串转成 Python 对象,但也有区别: >>> import json >>> s = '{"one":1,"two":2}' >>> json.loads(s) {u'two': 2, u'one': 1} >>> eval(s) {'two': 2, 'one': 1} json.loads与eval都能将s转成python中的对象,json.loads将json中的字符串转成unicode(types.UnicodeType),eval转成了str(types.StringType)。在读文件时如果有中文如 此时用json.loads会报错如下: 这时使用eval不会报错,强烈推荐,这个问题困扰了我一晚上。 对于普通的数据类型,json