The mission of this blog, along with associated youtube channel, is to deliver top quality embedded systems education for free, to all. My vision is to build an embedded systems university where learning doesn't cost
Search This Blog
Thursday, July 31, 2014
Python is interpreted or compiled?
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
# 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 "
#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 "
##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 "
## 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
Following are few highlighted features of perl:
- Highlevel
- General purpose
- Interpreted
- Object Oriented (Class based)
- 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
- Text manipulation
- Graphics programming
- System Administration
- Network 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
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
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
# Creating a thread object of subclass evThread
systemEventViewer = evThread('localhost', 'System')
#start the thread
systemEventViewer.start()
Inheritance in Python
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 can be started in interpreter mode using following command: