पायथन लाइब्रेरी में 'ट्रेस' मॉड्यूल में फ़ंक्शन प्रोग्राम निष्पादन का पता लगाता है, और एनोटेट स्टेटमेंट कवरेज। इसमें कॉलर संबंध उत्पन्न करके रन के दौरान बुलाए गए कार्यों को सूचीबद्ध करने के लिए कार्य भी हैं।
ट्रेस मॉड्यूल की विशेषताओं को प्रदर्शित करने के लिए एक उदाहरण के रूप में निम्नलिखित दो पायथन लिपियों का उपयोग किया जाता है।
#myfunctions.py
import math
def area(x):
a = math.pi*math.pow(x,2)
return a
def factorial(x):
if x==1:
return 1
else:
return x*factorial(x-1) #mymain.py
import myfunctions
def main():
x = 5
print ('area=',myfunctions.area(x))
print ('factorial=',myfunctions.factorial(x))
if __name__=='__main__':
main() 'ट्रेस' मॉड्यूल में कमांड लाइन इंटरफेस है। मॉड्यूल में सभी कार्यों को कमांड लाइन स्विच का उपयोग करके बुलाया जा सकता है। सबसे महत्वपूर्ण विकल्प है --trace जो प्रोग्राम लाइनों को निष्पादित करते समय प्रदर्शित करता है। निम्नलिखित उदाहरण में एक अन्य विकल्प --ignore-dir प्रयोग किया जाता है। यह ट्रेस जनरेट करते समय निर्दिष्ट निर्देशिकाओं की उपेक्षा करता है।
E:\python37>python -m trace --ignore-dir=../lib --trace mymain.py
आउटपुट
mymain.py(2): def main():
mymain.py(7): if __name__=='__main__':
mymain.py(8): main()
--- modulename: mymain, funcname: main
mymain.py(3): x=5
mymain.py(4): print ('area=',myfunctions.area(x))
--- modulename: myfunctions, funcname: area
myfunctions.py(3): a=math.pi*math.pow(x,2)
myfunctions.py(4): return a
area= 78.53981633974483
mymain.py(5): print ('factorial=',myfunctions.factorial(x))
--- modulename: myfunctions, funcname: factorial
myfunctions.py(6): if x==1:
myfunctions.py(9): return x*factorial(x-1)
--- modulename: myfunctions, funcname: factorial
myfunctions.py(6): if x==1:
myfunctions.py(9): return x*factorial(x-1)
--- modulename: myfunctions, funcname: factorial
myfunctions.py(6): if x==1:
myfunctions.py(9): return x*factorial(x-1)
--- modulename: myfunctions, funcname: factorial
myfunctions.py(6): if x==1:
myfunctions.py(9): return x*factorial(x-1)
--- modulename: myfunctions, funcname: factorial
myfunctions.py(6): if x==1:
myfunctions.py(7): return 1
factorial= 120 --गिनती विकल्प कवर एक्सटेंशन के साथ उपयोग में आने वाले प्रत्येक मॉड्यूल के लिए एक फ़ाइल बनाता है।
E:\python37>python -m trace --count mymain.py area= 78.53981633974483 factorial = 120
myfunctions.cover
1: import math 1: def area(x): 1: a = math.pi*math.pow(x,2) 1: return a 1: def factorial(x): 5: if x==1: 1: return 1 else: 4: return x*factorial(x-1)
mymain.कवर
1: import myfunctions
1: def main():
1: x = 5
1: print ('area=',myfunctions.area(x))
1: print ('factorial=',myfunctions.factorial(x))
1: if __name__=='__main__':
1: main() --सारांश विकल्प एक संक्षिप्त सारांश प्रदर्शित करता है यदि -गिनती विकल्प का भी उपयोग किया जाता है।
E:\python37>python -m trace --count --summary mymain.py area = 78.53981633974483 factorial = 120 lines cov% module (path) 8 100% myfunctions (E:\python37\myfunctions.py) 7 100% mymain (mymain.py)
--फ़ाइल विकल्प फ़ाइल का नाम निर्दिष्ट करता है जिसमें कई ट्रेसिंग रन पर गिनती जमा होती है।
E:\python37>python -m trace --count --file report.txt mymain.py area = 78.53981633974483 factorial = 120 Skipping counts file 'report.txt': [Errno 2] No such file or directory: 'report.txt' E:\python37>python -m trace --count --file report.txt mymain.py area= 78.53981633974483 factorial= 120
--listfuncs विकल्प कार्यक्रम के निष्पादन के दौरान बुलाए गए कार्यों को प्रदर्शित करता है।
E:\python37>python -m trace --listfunc mymain.py | findstr -v importlib area= 78.53981633974483 factorial= 120 functions called: filename: E:\python37\lib\encodings\cp1252.py, modulename: cp1252, funcname: IncrementalEncoder.encode filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: <module> filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: area filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: factorial filename: mymain.py, modulename: mymain, funcname: <module> filename: mymain.py, modulename: mymain, funcname: main
--trackcalls विकल्प का उपयोग -सूची funcs विकल्प के साथ किया जाता है। यह कॉलिंग संबंध उत्पन्न करता है।
E:\python37>python -m trace --listfunc --trackcalls mymain.py | findstr -v importlib area= 78.53981633974483 factorial= 120 calling relationships: --> E:\python37\myfunctions.py *** E:\python37\lib\trace.py *** --> mymain.py trace.Trace.runctx -> mymain.<module> *** E:\python37\myfunctions.py *** myfunctions.factorial -> myfunctions.factorial *** mymain.py *** mymain.<module> -> mymain.main --> E:\python37\lib\encodings\cp1252.py mymain.main -> cp1252.IncrementalEncoder.encode --> E:\python37\myfunctions.py mymain.main -> myfunctions.area mymain.main -> myfunctions.factorial