Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन स्टेटमेंट एक्ज़ीक्यूशन को ट्रेस या ट्रैक करना (trace मॉड्यूल)

पायथन लाइब्रेरी में 'trace' मॉड्यूल के फ़ंक्शन प्रोग्राम एक्ज़ीक्यूशन का ट्रेस जनरेट करते हैं और एनोटेटेड स्टेटमेंट कवरेज प्रदान करते हैं। इसमें रन के दौरान कॉल किए गए फ़ंक्शन्स को सूचीबद्ध करने के लिए कॉलर रिलेशनशिप जनरेट करने के फ़ंक्शन भी होते हैं।

उदाहरण स्क्रिप्ट्स

trace मॉड्यूल की विशेषताओं को प्रदर्शित करने के लिए निम्न दो पायथन स्क्रिप्ट्स का उपयोग किया जाता है:

# 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' मॉड्यूल का एक कमांड लाइन इंटरफ़ेस है। मॉड्यूल के सभी फ़ंक्शन्स को कमांड लाइन स्विच का उपयोग करके कॉल किया जा सकता है। सबसे महत्वपूर्ण विकल्प --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(7): return 1
factorial= 120

--count विकल्प

--count विकल्प उपयोग में प्रत्येक मॉड्यूल के लिए .cover एक्सटेंशन वाली फ़ाइल जनरेट करता है।

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.cover

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

--summary विकल्प

--summary विकल्प --count विकल्प के साथ उपयोग किए जाने पर एक संक्षिप्त सारांश प्रदर्शित करता है।

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)

--file विकल्प

--file विकल्प उस फ़ाइल का नाम निर्दिष्ट करता है जिसमें कई ट्रेसिंग रन पर काउंट जमा होता है।

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 विकल्प

--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: 
filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: area
filename: E:\python37\myfunctions.py, modulename: myfunctions, funcname: factorial
filename: mymain.py, modulename: mymain, funcname: 
filename: mymain.py, modulename: mymain, funcname: main

--trackcalls विकल्प

--trackcalls विकल्प का उपयोग --listfuncs विकल्प के साथ किया जाता है। यह कॉलिंग रिलेशनशिप जनरेट करता है।

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.
*** E:\python37\myfunctions.py ***
myfunctions.factorial -> myfunctions.factorial
*** mymain.py ***
mymain. -> 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
  1. - पायथन प्रोग्राम के निष्पादन का समय कैसे प्राप्त करें?

    किसी प्रोग्राम के निष्पादन समय को मापने के लिए, या तो time.clock() या time.time() फंक्शन्स का उपयोग करें। पायथन डॉक्स कहते हैं कि इस फंक्शन का उपयोग बेंचमार्किंग उद्देश्यों के लिए किया जाना चाहिए। उदाहरण import time t0 = time.clock() print(Hello) t1 = time.clock() - t0 print(Time elapsed: , t1 - t

  1. - पायथन का उपयोग करके स्क्रीन पर कैसे प्रिंट करें?

    स्क्रीन पर आउटपुट करने का बुनियादी तरीका print स्टेटमेंट का उपयोग करना है। >>> print Hello, world Hello, world एक ही लाइन पर स्पेस से अलग कई चीजें प्रिंट करने के लिए, उनके बीच कॉमा का उपयोग करें। उदाहरण के लिए: >>> print Hello,, World Hello, World जबकि किसी भी स्ट्रिंग में स्पेस न

  1. - पायथन में assert स्टेटमेंट का उपयोग क्या है?

    assert स्टेटमेंट का निम्न सिंटैक्स होता है: assert , उपरोक्त लाइन को इस प्रकार पढ़ा जाता है: यदि फाल्स मूल्यांकन करता है, तो एक एक्सेप्शन उठाया जाता है और आउटपुट होगा। यदि हम किसी कोड ब्लॉक या एक्सप्रेशन का परीक्षण करना चाहते हैं तो हम इसे assert कीवर्ड के बाद रखते हैं। यदि टेस्ट पास होता है या ए