Search This Blog

Thursday, July 31, 2014

Python is interpreted or compiled?

Code written in any high level language is either converted into the machine code by compiler tool chain (preprocess -> compile -> link) that can be executed on the CPU or can be converted into an intermediate code that can be executed on the CPU by an interpreter(VM).

In python, the .py file is converted into bytecode (.pyc) before it is executed by the interpreter. So, working with python involves a stage of compilation before the interpreting is done.

Compiler Vs Interpreter


Compilers

Interpreters

Conversion of source files to executable is done at one and for all files. So, in this environment, generating the executable code takes more time than interpreter environment. But once the executable is generated, the execution will be much faster compared to interpreter

Conversion will be done statement by statement at the execution time. So, the execution takes more time.

Development cycle is:

Code -> Preprocess -> compile -> Link->execute

Development cycle is:

Code -> Load-> execute

When the programs are large, the time taken create the executable will be more. So, there is longer waiting time before any testing is started

Waiting time to start testing is less.

 

Sunday, July 20, 2014

Understanding Scope of variables in python

C:\Work\MyWork\Python\Programs\local_global.py.html # Understanding scope of variables in Python

# Consider the following class:
# Here constructor initializes variable_1 and variable_2. Both to 0
class localGlobal():
    def __init__(self):
        self.variable_1 = 0
        variable_2 = 0
    def setVar(self, var):
        #set variable_1
        self.variable_1 = var
        variable_2 = var + 1
        print "variable_2", variable_2


# Let us create a object of this class
obj = localGlobal()

#Now, variable_1 being a class variable (or class attribute),
#we can access it as follows
obj.variable_1

#Same is not true for variable_2. It will result in AttributeError.
#obj.variable_2
#Traceback (most recent call last):
#  File "", line 1, in
#AttributeError: localGlobal instance has no attribute 'variable_2'

#The reason for the error is self explanatory. The 'variable_2' is
#not defined as a class attribute (not defined with self).
#it is just a local variable inside the 'init' and 'setVar'. If it is tried to access outside
# the class, it results in a NameError as shown below

variable_2
#Traceback (most recent call last):
#  File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
#    exec codeObject in __main__.__dict__
#  File "C:\Work\MyWork\Python\Programs\local_global.py", line 36, in
#    variable_2
#NameError: name 'variable_2' is not defined

#Now let us consider one more variable 'variable_3'
class localGlobal():
    def __init__(self):
        self.variable_1 = 0
        variable_2 = 0
    def setVar(self, var):
        #set variable_1
        self.variable_1 = var
        variable_2 = var + 1
        print "variable_2", variable_2
        self.variable_3 = var + 2

# Let us create a object of this class
obj = localGlobal()

# If we try to access the 'variable_3', we'll see an AttributeError
obj.variable_3
##Traceback (most recent call last):
##  File "", line 1, in
##AttributeError: localGlobal instance has no attribute 'variable_3'

##The reason for this error is that, the attribute 'variable_3' is not created yet.
##This variable is getting created in 'setVar' method. So, once the 'setVar' method
##is executed, 'variable_3' will be available
#call setVar method
obj.setVar(10)

#Now try to access the 'variable_3'. We'll get no error
obj.variable_3

#Now let us consider one more variable 'variable_4'
class localGlobal():
    def __init__(self):
        self.variable_1 = 0
        variable_2 = 0
    def setVar(self, var):
        #set variable_1
        self.variable_1 = var
        variable_2 = var + 1
        print "variable_2", variable_2
        self.variable_3 = var + 2
        variable_4 = var + 3
    def getVar4(self):
        print 'variable_4', variable_4

# Let us create a object of this class
obj = localGlobal()

##if we try to access the 'variable_4'(through 'getVar4') we will get the NameError.
##You may state the reason as 'variable_4' is not created yet because
##'setVar' is not executed. Let us try to execute 'setVar' and then access
##'variable_4'
obj.setVar(10)

obj.getVar4()
##We'll still see a name error as below. Becase, the name 'variable_4'
##is not a global name. In python, scope of all names defined inside a method
##is limited to that method.
##variable_4Traceback (most recent call last):
##  File "", line 1, in
##  File "C:\Work\MyWork\Python\Programs\local_global.py", line 87, in getVar4
##    print 'variable_4', variable_4
##NameError: global name 'variable_4' is not defined


##Consider the below example. One 'variable_4' is defined outside class and another
##inside 'setVar' function. When this block of code is executed we see that different
##values of 'variable_4' are printed out.
##variable_4(1) 13
##variable_4(2) 12
##'variable_4' accessed inside 'setVar' is its local.
##'variable_4' accessed in 'getVar4' is the 'variable_4' defined outside class.

variable_4 = 12

class localGlobal():
    def __init__(self):
        self.variable_1 = 0
        variable_2 = 0
    def setVar(self, var):
        #set variable_1
        self.variable_1 = var
        variable_2 = var + 1
        print "variable_2", variable_2
        self.variable_3 = var + 2
        variable_4 = var + 3
        print 'variable_4(1)', variable_4
    def getVar4(self):
        global variable_4
        print 'variable_4(2)', variable_4

obj = localGlobal()
obj.setVar(10)
obj.getVar4()

   


Back to Python Home Page

Sunday, July 6, 2014

Perl Scripting Tutorial 1_page

The perl was developed by Larry Wall in 1987 as scripting language for unix. It was originally developed for text manipulation.
Following are few highlighted features of perl:
  • Highlevel
  • General purpose
  • Interpreted
  • Object Oriented (Class based)
About the versions of perl:
  • Perl 1.0 : Original version of perl. Released in 1987.
  • Perl 2 : Released in 1988 with better regular expression engine
  • Perl 3: Released in 1989 with binary data streams support
  • Perl 4: Relesed in 1991 (“Camel Book”)
  • Perl 5: Released in 1994 with completely re-write interpreter which supported features like Objects,  references, lexical (my) variables, modules
  • Perl 6: Is the current version
Following are typical applications of perl:
  • Text manipulation
  • Graphics programming
  • System Administration
  • Network programming
Enough of histroy!!!. Let's start programming.
Before jumping into programming, it's required to know how to install and run perl on windows. (On linux machines, perl is included in the standard distribution). There are many flavors of perl available. The Active perl is what I'm using. The ActivePerl can be downloaded from its official website: http://www.activestate.com/activeperl The installation is straight forward. If you need any help, please follow this setup guide: TBD
The beginners to perl usually face following issues during execution:

Saturday, July 5, 2014

Reading Windows Logs from Event Viewer in python

c:\Work\MyWork\Python\Scripts\Doc\evreading.txt.html Reading 'Windows Logs' in Windows 'Event Viewer' in python using win32evtlog package can be really confusing. It took some time for me to understand and comeup with sequence of python statements to read the required windows log records.

1) Sequence to read the most recent event record
#set flags
flags = win32evtlog.EVENTLOG_FORWARDS_READ|win32evtlog.EVENTLOG_SEEK_READ
#get the total number of records in the log
totalNumOfRecords  = win32evtlog.GetNumberOfEventLogRecords(hand)
# read the event log
events = win32evtlog.ReadEventLog(hand, flags,totalNumOfRecords)


2) Sequence to read n most recent event records
#set flags
flags = win32evtlog.EVENTLOG_FORWARDS_READ|win32evtlog.EVENTLOG_SEEK_READ
#get the total number of records in the log
totalNumOfRecords  = win32evtlog.GetNumberOfEventLogRecords(hand)
# read the event log
events = win32evtlog.ReadEventLog(hand, flags,(totalNumOfRecords - n))

Note that the records will be populated in the 'events' list in chronological order (i.e. oldest first)

While reading windows logs it is important to correctly visualize the arrangment of records in the internal data structures. In the list of records, the newest one is at the last index. The last index in this case is same as the total number of records in the log. That is the reason we're passing 'totalNumOfRecords' as offset to ReadEventLog function in the sequence 1.


The complete program for logging new event records as they occur in "system" log is listed below
C:\Work\MyWork\Python\Scripts\Event_Viewer.py.html import win32evtlog # requires pywin32 pre-installed
from win32event import *
import threading

#to create your own thread,
# 1. define a new subclass of Thread class.
# 2. implement the thread functionality in run method
# 3. Create a thread object of subclass defined in step 1
# 4. start the thread with start() method
class evThread(threading.Thread):
    def __init__(self, server, logtype):
        threading.Thread.__init__(self)
        self.server = server
        self.logtype = logtype

    def run(self):
        self.hand = win32evtlog.OpenEventLog(self.server,self.logtype)
        flags = win32evtlog.EVENTLOG_FORWARDS_READ|win32evtlog.EVENTLOG_SEEK_READ
        prevNumOfRecs = win32evtlog.GetNumberOfEventLogRecords(self.hand)
        evnthndl = CreateEvent(None, True, False, None)
        win32evtlog.NotifyChangeEventLog(self.hand, evnthndl)
        logfile = open("C:\Work\MyWork\Python\Programs\log.txt", "w")
        while True:
            print 'hi'
            evntsgnld = WaitForSingleObject(evnthndl, 10000)
            if evntsgnld == WAIT_OBJECT_0:
                print 'New event'
                numOfRec = win32evtlog.GetNumberOfEventLogRecords(self.hand)
                NumOfNewRecs = numOfRec - prevNumOfRecs
                offset = numOfRec - NumOfNewRecs
                events = win32evtlog.ReadEventLog(self.hand, flags, offset)
                for event in events:
                    print >> logfile, 'Event Category:', event.EventCategory
                    print >> logfile, 'Time Generated:', event.TimeGenerated
                    print >> logfile, 'Source Name:', event.SourceName
                    print >> logfile, 'Event ID:', event.EventID
                    print >> logfile, 'Event Type:', event.EventType
                    logfile.flush()
                ResetEvent(evnthndl)
                prevNumOfRecs = numOfRec
               
                data = event.StringInserts
                if data:
                    print 'Event Data:'
                    for msg in data:
                        print msg
                print


# Creating a thread object of subclass evThread
systemEventViewer = evThread('localhost', 'System')

#start the thread
systemEventViewer.start()

Inheritance in Python

C:\Vim\vim72\Untitled.html A subclass in python is created like below

class <subclass name> (<super class name>):
    def __init__(self):
        #call the super class constructor
        <super class name>.__init__()

Example:
#example starts
#subclass of thread class is creted
class evThread(threading.Thread):
    #defining the constructor for the subclass
    def __init__(self, server, logtype):
        #super class constructor must be called. Otherwise, python will give a rruntime error: "RuntimeError("thread.__init__() not called")"
        threading.Thread.__init__(self)
        #define new attributes for subclass
        self.server = server
        self.logtype = logtype

    #overide the superclass run method
    def run(self):
        self.hand = win32evtlog.OpenEventLog(self.server,self.logtype)
        flags = win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ
        total = win32evtlog.GetNumberOfEventLogRecords(self.hand)
        print 'Total Events: ', total

# Creating a thread object of subclass evThread
systemEventViewer = evThread('localhost', 'System')

#start the thread
systemEventViewer.start()

#example ends


Back to Python Home Page

Sunday, March 23, 2014

Using Perl Interpreter Interactively

Perl interpreter is very handy when you want to try some quick Perl lines, especially when you are beginner to perl. In this blog I would like to show how perl interpreter can be used effectively.

Perl can be started in interpreter mode using following command:
             perl -de 1

Perl when started in interpreter mode (This is also the debugger mode of perl) would look something like below:

Now Perl statements can be executed interactively as shown in the video below: