首页 \ 问答 \ pycharm哪个版本可以用jdk7

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, in 

 
afunc()
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.

相关问答

更多
  • 使用time模块的sleep
  • 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输出的函数调用栈就是个典型的应 ...
  • 1)如果是控制台的,用py后缀,否则用pyw后缀 2)如果已经注册了py,pyw后缀是有python处理的,直接在cmd中敲入python的脚本文件名。否则采用 >python 脚本文件 这种方式,注意python如果不在环境变量path中,就要用全路径。 另外,那个shebang行 #!/usr/bin/python 是在linux/unix下才有效的,windows下没作用。pyc是第一次执行的时候会做一个编译,生成pyc文件,以提高后继加载速度。
  • 定界符倒只有空格 不过你可以试试选中所有要缩进的代码 然后按TAB键 就能一次性缩进所有代码,大部分编辑器支持这个功能
  • 在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因。在操作系统提供的调用中,返回错误码非常常见。比如打开文件的函数open(),成功时返回文件描述符(就是一个整数),出错时返回-1。 用错误码来表示是否出错十分不便,因为函数本身应该返回的正常结果和错误码混在一起,造成调用者必须用大量的代码来判断是否出错: 复制代码代码如下: def foo(): r = some_function() if r==(-1): return (-1) # do some ...
  • 这个要看具体的错误,一般来说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 ...
  • 散列是一项相对简单的任务,与旋转磁盘的速度相比,现代CPU速度非常快。 i7上的快速基础测试显示,它可以使用MD5散列大约450 MB / s,使用SHA-1散布大约290 MB / s。 相比之下,旋转盘具有约70-150MB / s的典型(顺序原始读取)速度。 这意味着,即使忽略文件系统的开销和最终的磁盘搜索,CPU也可以将文件散列大约比磁盘读取速度快3倍。 处理第一个文件时可能会提高性能,因为操作系统会将第一个文件缓存在内存中,因此不会发生磁盘I / O. 这可以通过以下任一方式确认: 重新启动服务器 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)