我使用JEXL库来计算具有不同参数的数学表达式(例如,y = 2x + a ^ 2-4*a*x其中(x = 1&a = 3),(x = 5&a = -15)等).它在简单的表达式上运行良好,但是当我开始使用更多的硬表达式时 - 它不起作用.这是代码运作良好:
JexlEngine jexl = new JexlEngine();
Expression func = jexl.createExpression("x1+x2");
MapContext mc = new MapContext();
mc.set("x1", 2);
mc.set("x2", 1);
System.out.println(func.evaluate(mc)); // prints "3" - GOOD ANSWER!
Run Code Online (Sandbox Code Playgroud)
但这一个打印错误答案:
JexlEngine jexl = new JexlEngine();
Expression func = jexl.createExpression("(x1-2)^4+(x1-2*x2)^2");
MapContext mc = new MapContext();
mc.set("x1", 2);
mc.set("x2", 1);
System.out.println(func.evaluate(mc)); // prints "6" - WRONG ANSWER!
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我要观点:
所以,我在MainWindowView类中有组合框.然后我创建ActionListener并将其绑定到这个组合框.此ActionListener的actionPerfomed()方法尝试将ScanOptimisationView面板添加到主窗口框架.这是代码:
package ru.belaventcev.view;
import java.awt.Container;
public class MainWindowView extends JFrame{
private int frmHeight = 525;
private int frmWidth = 650;
public Container frmContainer;
public static JButton btnCalc;
public static JComboBox cbMethods;
public MainWindowView(){
setPreferredSize(new Dimension(frmWidth, frmHeight));
setSize(frmWidth, frmHeight);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
frmContainer = getContentPane();
frmContainer.setLayout(new MigLayout("", "[grow,center]", "[::30px,grow,center][grow,center][::500px,grow,center][::25px,grow,center]"));
cbMethods = new JComboBox();
cbMethods.setModel(new DefaultComboBoxModel(new JPanel[] {new ScanOptimisationView()}));
cbMethods.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JPanel temp = (JPanel) cbMethods.getSelectedItem();
frmContainer.add(temp, "cell 0 1,span");
}
}); …Run Code Online (Sandbox Code Playgroud) 我有一个 SwingWorker,它在后台执行一些计算(这些操作位于重写的doInBackground()方法中)。所以,我也用这个execute()方法来开始计算。当这些计算完成后我怎样才能得到结果?
我想shutil.copy()将文件复制到另一个目录.我尝试执行以下代码:
copy(open("/home/dizpers/pytest/testfile1.txt", "r"), "/home/dizpers/pytest")
Run Code Online (Sandbox Code Playgroud)
但python shell显示错误消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/shutil.py", line 116, in copy
dst = os.path.join(dst, os.path.basename(src))
File "/usr/lib/python2.7/posixpath.py", line 112, in basename
i = p.rfind('/') + 1
AttributeError: 'file' object has no attribute 'rfind'
Run Code Online (Sandbox Code Playgroud)
所以,我理解为什么会出现这个问题.我用open()函数打开文件.而且我认为我也应该打开这样的目录.我怎样才能做到这一点?
提前致谢!
我搜索答案为什么a is None比工作更快a == None.我使用这段代码测量时间:
>>> timeit.timeit("1 is None", number=10000000)
0.4035069934390217
>>> timeit.timeit("1 == None", number=10000000)
0.8190256083633187
Run Code Online (Sandbox Code Playgroud)
该文件说,a is b具有功能equialent is_(a, b)和a == b具有同等功能eq(a, b).那么,为什么is_功能更快eq?
我读了一些is_()只比较对象标识符的文章,并进行了eq()"深度比较".但我在文档中找不到这些信息.这些信息是否正确?我可以在哪里阅读更多相关信息?
有人可以向我解释以下内容。让我们来看看代码:
if __name__ == '__main__':
try:
while 1:
x = 2+2
except KeyboardInterrupt:
print('yo')
Run Code Online (Sandbox Code Playgroud)
如果我运行这个,稍等片刻,然后按Ctrl+ C,将处理异常并yo打印消息。
如果我们更改代码以捕获像这样的广泛异常:
if __name__ == '__main__':
try:
while 1:
x = 2+2
except Exception, e:
print('yo')
print(e)
Run Code Online (Sandbox Code Playgroud)
运行它,稍等片刻,按Ctrl+ C,不会捕获到 KeyboardInterrupt 异常。
根据Python 文档:
Python 默认安装少量信号处理程序:忽略 SIGPIPE(因此管道和套接字上的写入错误可以报告为普通 Python 异常)并且SIGINT 被转换为 KeyboardInterrupt 异常。所有这些都可以被覆盖。
那么,为什么在第二种情况下这个异常没有被捕获甚至引发?
这是我的行动:
>>> def show(d):
print d
...
>>> test = {"result": True}
>>> show(test)
{'result': True}
>>> show(test["info"]="Some info")
File "<console>", line 1
SyntaxError: keyword can't be an expression
Run Code Online (Sandbox Code Playgroud)
为什么我不能将表达式作为参数传递给函数?
我的任务是定期向服务器发出请求并将该请求的时间写入数据库。我使用 Python 2.7.3、cURL 和 pycurl 作为 cURL 的包装器。我确实这样要求:
import pycurl
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
c.perform()
Run Code Online (Sandbox Code Playgroud)
但我如何确定这个请求的时间呢?
UPD1
好的,我明白,我应该采用 2 个时间戳,它们之间的差异将是请求的持续时间。但我面临一些问题:
当我执行时:
import pycurl, time
c = pycurl.Curl()
c.setopt(c.URL, "http://google.com/")
c.setopt(c.VERBOSE, True) # to see request details
ts = time.clock()
c.perform()
print time.clock() - ts
Run Code Online (Sandbox Code Playgroud)
我得到 0.0 o(有时)0.01。这是错误的 diff,因为我在 python shell 中执行此命令,并且在执行之后ts = time.clock()和执行之前还剩下一些时间print time.clock() - ts,所以 diff 应该约为 3 秒。
我在 Virtualbox 上安装的 Ubuntu 服务器 12.04 上得到此输出。Virtualbox 安装在 …
我有以下情况.我有一个图像(标签)和标题(标签).我想在图像上标题.我该怎么做?
我试图使用负边距http://jsfiddle.net/58H7c/1/但在这种情况下图像位于标题上.我尝试使用z-index - 没有效果.
也许你知道更好的解决方案
TIA!
PS
另一个想法是使用图像作为背景.但在这种情况下,当我尝试为#imgPlace容器填充时 - 背景图像保持原位(当移动div容器时).
我有通过 AJAX 预先上传文件的 HTML 表单。所以在 django 后端我有下面的 View 代码,它处理这个 AJAX 调用:
@csrf_exempt
def book_upload(request):
if request.method == 'POST':
log.info('received POST to main book_upload view')
if request.FILES is None:
return HttpResponseBadRequest('Must have files attached!')
log.info('request has FILES')
file_types = (u'file_pdf', u'file_djvu', u'file_doc', u'file_epub', u'file_djvu', u'file_fb2', u'file_txt', u'file_chm', u'file_other');
file = None
file_type = None
for ft in file_types:
if ft in request.FILES:
file = request.FILES[ft]
file_type = ft
break
if file is None:
return HttpResponseBadRequest('Bad file type')
file_path = file.temporary_file_path()
result …Run Code Online (Sandbox Code Playgroud)