我的目标是为此激发应用程序的序列图我需要在运行时有关调用者和被调用者类名的信息.我可以成功检索调用函数但无法获取调用者类名称?
#Scenario caller.py:
import inspect
class A:
def Apple(self):
print "Hello"
b=B()
b.Bad()
class B:
def Bad(self):
print"dude"
print inspect.stack()
a=A()
a.Apple()
Run Code Online (Sandbox Code Playgroud)
当我打印堆栈时,没有关于调用者类的信息.那么可以在运行时检索调用者类吗?
我目前有一个表t1,'\t'在我的FIELD TERMINATED子句中设置了一个值.
现在我想在表的 结构中更改该特定子句t1.
创作后ALTER的FIELD TERMINATED条款有什么办法吗?
如何在执行期间导入Python文件?
我创建了3个文件a.py,b.py并c.py在路径中C:\Users\qksr\Desktop\Samples
这些文件包含如下所示的代码:
a.py
from c import MyGlobals
def func2():
print MyGlobals.x
MyGlobals.x = 2
Run Code Online (Sandbox Code Playgroud)
b.py
import a
from c import MyGlobals
def func1():
MyGlobals.x = 1
if __name__ == "__main__":
print MyGlobals.x
func1()
print MyGlobals.x
a.func2()
print MyGlobals.x
Run Code Online (Sandbox Code Playgroud)
c.py
class MyGlobals(object):
x = 0
Run Code Online (Sandbox Code Playgroud)
当我执行代码b.py时,抛出以下错误:
ImportError: No module named a
Run Code Online (Sandbox Code Playgroud)
我相信我的工作目录是默认的,所有文件a,b,c都是我在samples文件夹中创建的.
如何在Python中导入python文件?
假设类A的方法m在类B上调用方法n,但类B的源代码不包含n,因为n是从B的一个超类继承的.
序列图如何显示?
通过方法n在A和B之间是否存在消息交互,或者它将与A和B的超类通过方法n进行消息交互
场景:
>>> a=' Hello world'
index = 3
Run Code Online (Sandbox Code Playgroud)
在这种情况下,“ H”索引为“ 3”。但是我需要一个更通用的方法,这样对于任何字符串变量'a'我都需要知道第一个字符的索引?
替代方案:
>>> a='\tHello world'
index = 1
Run Code Online (Sandbox Code Playgroud) 我想从文本文件中删除重复的单词.
我有一些文本文件包含如下所示:
None_None
ConfigHandler_56663624
ConfigHandler_56663624
ConfigHandler_56663624
ConfigHandler_56663624
None_None
ColumnConverter_56963312
ColumnConverter_56963312
PredicatesFactory_56963424
PredicatesFactory_56963424
PredicateConverter_56963648
PredicateConverter_56963648
ConfigHandler_80134888
ConfigHandler_80134888
ConfigHandler_80134888
ConfigHandler_80134888
Run Code Online (Sandbox Code Playgroud)
结果输出需要是:
None_None
ConfigHandler_56663624
ColumnConverter_56963312
PredicatesFactory_56963424
PredicateConverter_56963648
ConfigHandler_80134888
Run Code Online (Sandbox Code Playgroud)
我只使用了这个命令:en = set(open('file.txt')但它不起作用.
任何人都可以帮助我如何从文件中只提取唯一的集合
谢谢
我目前正在编写一个python脚本来显示包中所有python文件的所有名称,以及文件包含的类的所有名称.
脚本
#A.py
class apple:
.
.
class banana:
.
.
# Extracting_FilesName_className.py
f=open("c:/package/A.py','r')
className=[]
"Here i need to use some command to get all the class name within the file A.py and append it to className'
print file_name,className
Run Code Online (Sandbox Code Playgroud)
把A.py,苹果,香蕉放出来
我可以使用旧的约定方式,我可以检查每一行是否存在"class"字符串并检索className.但我想知道是否有更好的方法来检索所有类名?
我的目标是在 python 中找到循环语句开始和结束的行号。
示例场景
#A.py
Line1: a=0
Line2: while a<5:
Line3: print a
Line4: a=a+1
Desired output:
Start of a loop Line2
End of a loop Line4
Run Code Online (Sandbox Code Playgroud)
当前解析器代码
#parser.py
with open(a) as f:
tree = ast.parse(f.read())
taskline=[]
for node in ast.walk(tree):
if isinstance(node, (ast.For)) or isinstance(node,(ast.While)):
print node.lineno-1 <-- This give line number on for the start of a loop
Run Code Online (Sandbox Code Playgroud)
我想实现上述输出。我使用 AST 来解析给定的文件并确定循环的发生。通过 AST 解析,我能够找到循环开始的行号,但循环结束的行号尚未确定。有什么办法可以解析整个循环语句并确定其开始和结束行号吗?
我有一个关于装饰器工作的问题.我想用一个例子来解释我的问题
我实现的代码来理解装饰器
import sys
import inspect
def entryExit(f):
def new_f(self,*args, **kwargs):
print "Entering", f.__name__,self.__class__.__name__,inspect.getargspec(f).args[1:]
f(self,*args)
print "Exited", f.__name__,self.__class__.__name__,inspect.getargspec(f).args[1:]
return new_f
class A:
@entryExit
def move(self,g,h):
print "hello"
print g,h
@entryExit
def move1(self,m,n):
print "hello"
print m,n
return m
a=A()
a.move(5,7)
h=a.move1(3,4)
print h
Run Code Online (Sandbox Code Playgroud)
这段代码的输出是
Entering move A ['g', 'h']
hello
5 7
Exited move A ['g', 'h']
Entering move1 A ['m', 'n']
hello
3 4
Exited move1 A ['m', 'n']
None
Run Code Online (Sandbox Code Playgroud)
输出的最后一行显示None.但是使用装饰器可以改变方法的实际含义.方法中的return语句move1未执行.我需要的实际输出是
Entering move A ['g', …Run Code Online (Sandbox Code Playgroud)