Friday, March 14, 2014

Python libraries


Long time no c ... :)  I know.
lets get back to business.
I have had interesting issues with my python scripting . thought of sharing one of them.
I was asked to create a plot for application performance comparison across multiple platforms. this turned out to be a tricky one as I hardly worked on graphs.

To my surprise, i found an easy and interesting library for this.

matplotlib.pyplot

amazing tool. very user friendly. here is the link to the same with few examples.

http://matplotlib.org/index.html

happy scripting :)




Monday, April 9, 2012

perl with python

I am not sure how many times i have come across to a point where I had to use a perl script with python. And I realized that this could be a case for few of you guys too. thought why not put a post for this. here is a simple example of how to do it.


import os
os.system("perl ~perlfile~")

you could use sys.exit() to see if the execution was a success. This is the way i am using it. could solve problems at least for a few of you guys.

Friday, April 6, 2012

write/read to serial port

Well, when you have pyserial installed on your pc writing to serial port is fairly simple. here is the how you do it..

import serial
ser = serial.Serial(, ,timeout=1)
print ser.portstr
ser.setWriteTimeout(1)
ser.write("hello" + "\n")
ser.read(9999)

the above sample code best describes how to write or read from a serial port using python - pyserial library. This is not the best of the solutions available out there but def simple (probably the simplest :) ). you could do more digging into the serial library too if you would like.

Like I always say happy scripting !!!

pyserial - usb serial port scan

good to be back after a break. I have been using pyserial for performing serial automation on hardware. I faced a peculiar problem . Thought it will be a good idea to post it. I have usb-serial cable connected to pc which is bluetooth capable. Now, when i do a port scan , it also lists the serial ports from bluetooth too. this was causing problems in my script execution. so I had to modify the port scan script to take care of this problem . the script is below ..

print "Checking for the Serial Port....."

for i in range(256):
try:
ser = serial.Serial(i, 115200,timeout=1)
print ser.portstr
ser.setWriteTimeout(1)
ser.write("hello" + "\n")
data = ser.read(9999)
print data
ser.close()
except serial.SerialException:
print "exception",sys.exc_info()[1]
pass

Just copy this code and use it. keep in mind , the true validation can be based on the data in my script or the time it takes to respond to such a request over serial.

i have also added the exception print info which could turn in handy .

Happy Scripting

Wednesday, December 28, 2011

pyserial port scan

if you are using pyserial module to read the communication from your device on UART , you will have to know which port the is your system connected to.

the following is a simple code to scan through all the COM ports and find out which com port is in opened. at the end of the program , you will see the serial uart that is in use being printed

import pyserial
for i in range(256):
try:
s = serial.Serial(i)
available.append( (i, s.portstr))
port = i
s.close()
except serial.SerialException:
pass

ser = serial.Serial(port, 115200,timeout=1)
print ser.portstr


happy scripting!

Tuesday, December 27, 2011

Process kill

In python if we need to kill a running process we need to run the following command.

import os
os.system("taskkill /im "process_name" /f")

just give the name of the process that is running and it should kill it. we need this kind of setup when we try to kill all the existing process before launching a one.

Thursday, December 22, 2011

Python File Operations

Alright ,it is time for python file operation related examples. there could be a variety of things that you would like to do with a file. I am sharing a simple example below.

old_file= open ("file_Path","r")
new_file = open ("file_Path","w")

for line in old_file:
if "" in line:
new_file.write(line.replace("oldtext","newtext"))
else:
new_file.write(line)

In the above example we are opening a file in read and write mode and performing some basic operations.

Based on a condition we are trying to replace some content in the file and writing the new thing in a different file.

this code works as is , you just have to give the appropriate values. sys is the only module you will be needing.