pycharm哪个版本可以用jdk7
更新时间:2023-12-07 12:12
最满意答案
C语言有__LINE__来表示源代码的当前行号,经常在记录日志时使用。Python如何获取源代码的当前行号? The C Language has the __LINE__ macro, which is wildly used in logging, presenting the current line of the source file. And how to get the current line of a Python source file? exception输出的函数调用栈就是个典型的应用: A typical example is the output of function call stack when an exception: python代码 File "D:\workspace\Python\src\lang\lineno.py", line 19, inafunc() File "D:\workspace\Python\src\lang\lineno.py", line 15, in afunc errmsg = 1/0 ZeroDivisionError: integer division or modulo by zero 那么我们就从错误栈的输出入手,traceback模块中: Now that, Let's begin with the output of an exception call stack, in the traceback module: python代码 def print_stack(f=None, limit=None, file=None): """Print a stack trace from its invocation point. The optional 'f' argument can be used to specify an alternate stack frame at which to start. The optional 'limit' and 'file' arguments have the same meaning as for print_exception(). """ if f is None: try: raise ZeroDivisionError except ZeroDivisionError: f = sys.exc_info()[2].tb_frame.f_back print_list(extract_stack(f, limit), file) def print_list(extracted_list, file=None): """Print the list of tuples as returned by extract_tb() or extract_stack() as a formatted stack trace to the given file.""" if file is None: file = sys.stderr for filename, lineno, name, line in extracted_list: _print(file, ' File "%s", line %d, in %s' % (filename,lineno,name)) if line: _print(file, ' %s' % line.strip()) traceback模块构造一个ZeroDivisionError,并通过sys模块的exc_info()来获取运行时上下文。我们看到,所有的秘密都在tb_frame中,这是函数调用栈中的一个帧。 traceback constructs an ZeroDivisionError, and then call the exc_info() of the sys module to get runtime context. There, all the secrets hide in the tb_frame, this is a frame of the function call stack. 对,就是这么简单!只要我们能找到调用栈frame对象即可获取到行号!因此,我们可以用同样的方法来达到目的,我们自定义一个lineno函数: Yes, It's so easy! If only a frame object we get, we can get the line number! So we can have a similar implemetation to get what we want, defining a function named lineno: python代码 import sys def lineno(): frame = None try: raise ZeroDivisionError except ZeroDivisionError: frame = sys.exc_info()[2].tb_frame.f_back return frame.f_lineno def afunc(): # if error print "I have a problem! And here is at Line: %s"%lineno() 是否有更方便的方法获取到frame对象?当然有! Is there any other way, perhaps more convinient, to get a frame object? Of course YES! python代码 def afunc(): # if error print "I have a proble! And here is at Line: %s"%sys._getframe().f_lineno 类似地,通过frame对象,我们还可以获取到 当前文件、当前函数等信息,就像C语音的 __FILE__与__FUNCTION__一样。其实现方式,留给你们自己去发现。 Thanks to the frame object, similarly, we can also get current file and current function name, just like the __FILE__ and __FUNCTION__ macros in C. Debug the frame object, you will get the solutions.
相关问答
更多-
python 设置脚本持续运行[2024-02-25]
使用time模块的sleep -
关于Python中的一段为Python脚本添加行号脚本[2022-01-27]
C语言有__LINE__来表示源代码的当前行号,经常在记录日志时使用。Python如何获取源代码的当前行号? The C Language has the __LINE__ macro, which is wildly used in logging, presenting the current line of the source file. And how to get the current line of a Python source file? exception输出的函数调用栈就是个典型的应 ... -
Python 如何写脚本?[2023-09-27]
1)如果是控制台的,用py后缀,否则用pyw后缀 2)如果已经注册了py,pyw后缀是有python处理的,直接在cmd中敲入python的脚本文件名。否则采用 >python 脚本文件 这种方式,注意python如果不在环境变量path中,就要用全路径。 另外,那个shebang行 #!/usr/bin/python 是在linux/unix下才有效的,windows下没作用。pyc是第一次执行的时候会做一个编译,生成pyc文件,以提高后继加载速度。 -
python给一段代码加上for循环[2019-05-11]
定界符倒只有空格 不过你可以试试选中所有要缩进的代码 然后按TAB键 就能一次性缩进所有代码,大部分编辑器支持这个功能 -
我执行一段python脚本报错了,怎么解决[2022-03-20]
在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因。在操作系统提供的调用中,返回错误码非常常见。比如打开文件的函数open(),成功时返回文件描述符(就是一个整数),出错时返回-1。 用错误码来表示是否出错十分不便,因为函数本身应该返回的正常结果和错误码混在一起,造成调用者必须用大量的代码来判断是否出错: 复制代码代码如下: def foo(): r = some_function() if r==(-1): return (-1) # do some ... -
我执行一段python脚本报错了,怎么解决[2022-04-13]
这个要看具体的错误,一般来说python脚本错误,如果是命令行的,一般是捕获ctrl-c事件吧。 import signal import sys def signal_handler(signal, frame): print('You pressed Ctrl+C!') sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print('Press Ctrl+C') signal.pause() -
给定代码可能很难实现。 该线 file.write(line.split("(")[1].split(")")[0].replace(", ", "\n").replace(" 0", ",\n").replace(" ", ",")) 单线不可读。 它至少应该被重构 content = line.split("(")[1].split(")")[0] rows = [y.replace(" ", ",") for x in content.split(', ') ...
-
inspect.currentframe().f_back.f_lineno 从ActiveState代码 。 inspect.currentframe().f_back.f_lineno From ActiveState Code.
-
您正在尝试部署云端功能,但据我所知,您无法使用Python编写Google云端功能。 Javascript是目前唯一受支持的语言。 例如,请参阅https://cloud.google.com/functions/docs/writing/ 如果你想部署Python代码,你将需要一些其他的方法; 也许AppEngine,它看起来像你链接的代码正在使用。 You are trying to deploy a cloud function, but as far as I know, you can't wri ...
-
为什么这个python多处理脚本在一段时间后会变慢?(Why does this python multiprocessing script slow down after a while?)[2023-09-07]
散列是一项相对简单的任务,与旋转磁盘的速度相比,现代CPU速度非常快。 i7上的快速基础测试显示,它可以使用MD5散列大约450 MB / s,使用SHA-1散布大约290 MB / s。 相比之下,旋转盘具有约70-150MB / s的典型(顺序原始读取)速度。 这意味着,即使忽略文件系统的开销和最终的磁盘搜索,CPU也可以将文件散列大约比磁盘读取速度快3倍。 处理第一个文件时可能会提高性能,因为操作系统会将第一个文件缓存在内存中,因此不会发生磁盘I / O. 这可以通过以下任一方式确认: 重新启动服务器 ...