首页 \ 问答 \ 我如何让python函数返回异常回溯或结果?(How do I have a python function return an exception traceback, or the result?)

我如何让python函数返回异常回溯或结果?(How do I have a python function return an exception traceback, or the result?)

让我们说我有一个功能:

def ReadFile():
    with open("/etc/passwd") as file:
        data = file.read()

这个函数可能会成功,在这种情况下它需要返回一个结果,否则它可能会失败,在这种情况下我想返回一个异常的回溯,它将通过电子邮件发送给我,所以我知道我的程序中有一些失败,并且确切地说什么失败了。

要做到这一点,我可以这样做:

import traceback

def ReadFile():
    try:
        with open("/etc/passwd") as file:
            data = file.read()
    except IOError:
        return traceback.format_exc()
    return data

如果能够成功读取文件,则返回文件的内容。 否则,它返回异常的回溯。

traceback.format_exc()返回一个字符串。 如果ReadFile()应该返回一个列表,或者如果成功则返回元组或整数,那么事情很容易 - 当你调用ReadFile()时,如果返回的结果是一个字符串,你知道它失败了你可以运行代码发送电子邮件你错了,如果结果是你期望的类型(int,tuple,list或w / e),那么你知道它有效。

如果ReadFile()应该返回一个字符串,就像在我的例子中那样,而不是确定ReadFile()是成功还是失败变得非常困难,因为你需要解析字符串以确定它是否看起来像一个回溯或你期望的结果。

有一个更好的方法吗? 也许某些方法让traceback返回某种类型的对象,其中包含与traceback.format_exc()包含的信息相同的信息,这样就可以更容易地判断ReadFile()是成功还是失败?


Lets say I have a function:

def ReadFile():
    with open("/etc/passwd") as file:
        data = file.read()

This function might succeed, in which case it needs to return a result, or it might fail, in which case I want to return a traceback of the exception, which will get emailed to me so I know something failed in my program, and exactly what failed.

To do this, I can do something like:

import traceback

def ReadFile():
    try:
        with open("/etc/passwd") as file:
            data = file.read()
    except IOError:
        return traceback.format_exc()
    return data

If it is able to read the file successfully, it returns the contents of the file. Otherwise, it returns the traceback of the exception.

traceback.format_exc() returns a string. If ReadFile() is supposed to return a list, or tuple or integer if it succeeds, than things are easy - when you call ReadFile(), if the result returned is a string, you know it failed and you can run code that emails you the error, and if the result is the type you expected (int, tuple, list or w/e), than you know it worked.

If ReadFile() is supposed to return a string, as it does in my example, than figuring out if ReadFile() succeeded or failed becomes significantly more difficult, as you need to parse the string to figure out if it looks like a traceback or the result you expected.

Is there a better way to do this? Perhaps some way to have traceback return some sort of object with the same information as traceback.format_exc() contains, so that it's easier to figure out if ReadFile() succeeded or failed?


原文:https://stackoverflow.com/questions/35953283
更新时间:2022-02-09 15:02

最满意答案

您将需要Javascript来执行此操作。 使用window.close()

close();

注意:当前选项卡是隐含的。 这是相当的

window.close();

或者您可以指定一个不同的窗口。

所以:

function close_window() {
  if (confirm("Close Window?")) {
    close();
  }
}

与HTML:

<a href="javascript:close_window();">close</a>

要么:

<a href="#" onclick="close_window();return false;">close</a>

您在此return false以防止事件的默认行为。 否则浏览器将尝试去该URL(这显然不是)。

现在, window.confirm()对话框中的选项将OK,取消(不是Yes和No)。 如果你真的想要是和否,你需要创建一些模态的Javascript对话框。

注意:与上述有特定的浏览器差异。 如果用Javascript打开窗口(通过window.open() ),那么可以使用javascript关闭窗口。 Firefox不允许您关闭其他窗口。 我相信IE会要求用户确认。 其他浏览器可能有所不同。


You will need Javascript to do this. Use window.close():

close();

Note: the current tab is implied. This is equivalent:

window.close();

or you can specify a different window.

So:

function close_window() {
  if (confirm("Close Window?")) {
    close();
  }
}

with HTML:

<a href="javascript:close_window();">close</a>

or:

<a href="#" onclick="close_window();return false;">close</a>

You return false here to prevent the default behavior for the event. Otherwise the browser will attempt to go to that URL (which it obviously isn't).

Now the options on the window.confirm() dialog box will be OK and Cancel (not Yes and No). If you really want Yes and No you'll need to create some kind of modal Javascript dialog box.

Note: there is browser-specific differences with the above. If you opened the window with Javascript (via window.open()) then you are allowed to close the window with javascript. Firefox disallows you from closing other windows. I believe IE will ask the user for confirmation. Other browsers may vary.

相关问答

更多
  • 试试这个,我测试了它,它的工作原理: $IE->Quit(); Try this I have tested it and it works: $IE->Quit();
  • 您将需要Javascript来执行此操作。 使用window.close() : close(); 注意:当前选项卡是隐含的。 这是相当的 window.close(); 或者您可以指定一个不同的窗口。 所以: function close_window() { if (confirm("Close Window?")) { close(); } } 与HTML: close 要么:
    在循环中,我创建了URL来命中控制器并使用它设置参数,然后简单地使用以下语句。 echo ''; 在$ url变量中,我创建了URL并在其中发布数据。 这是如何,新标签将打开,点击网址,结果将是我完整的HTML。 In loop, I've created the URL to hit the controller and set the parameters with ...
  • 不,但是您可以在第一个标签中运行一个Javascript脚本,每隔10秒钟对您的服务器进行一次ping,以检查帐户是否已被确认。 如果是,那么该页面会自动重定向到新用户的仪表板(或其他“主要帐户页面”)。 setInterval(function(){ $.get('/api/is_confirmed.php').then( function(response){ if (response.status_code == '200') ...
  • var pop_window = window.open(...); //to close: pop_window.close(); 因此,Gmail可能会做类似(这是伪代码): window.onunload = function(){ if(pop_window){ //display questionare if({yes}) //close all windows else //keep all windows open ...
  • CODEPEN 尝试一下: