Category: Python

Nov 06

Python Exec Linux Process

While I am writing a curses based recording application in Python I thought it a good idea to jot down what I did to call a process and get the pid, then run for a set number of minutes and then kill the pid.

def doit_func():
output = subprocess.check_output(["/usr/bin/v4l2-ctl","--device=/dev/" + cfg_dict['source'],"--set-ctrl=video_bitrate="
 +  cfg_dict['bitrate']])

    tsStream = open(cfg_dict['target'],"wb")

    catProc = subprocess.Popen(["/bin/cat","/dev/video1","&"], stdout=tsStream)
    pid = str(catProc.pid) 

    start_time = time.time()
    elapsed_mins = 0

    while elapsed_mins != mins:
      counter = counter + 1
      elapsed_mins = int(time.time() - start_time) / 60
      draw_dict("recording for " + str(elapsed_mins) + " mins")

    output = subprocess.check_output(["/bin/kill","-9",pid])

Comments Off on Python Exec Linux Process
comments

Nov 06

Multidimensional array in python

#!/usr/bin/python 
rows=5;cols=2 
players=[[0]*cols for _ in xrange(rows)] 
print "####### Print Original Array ###################" 
print players 
print "\n" 

print "####### Direct Access ###################" 
print "going to set [0][0]=S9 and [3][1]=D3" 
players[0][0]='S9' 
players[3][1]='D3' 
print players 
print "\n" 

print "####### Append Col ###################" 
print "going to add to [2] value C7, and add to [4] value S4" 
players[2].append('C7') 
players[4].append('S4') 
print players 
print "\n" 

print "####### Append Row ###################" 
print "going to add row [5]" 
players.extend([[0]*cols]) 
print players 
print "\n" 

print "####### Print Complete Rows ###################" 
for row in range(len(players)): 
    print players[row] 
print "\n" 

print "####### Print item for item, by Column by Row ######" 
for row in range(len(players)): 
    for col in range(len(players[row])): 
        print str(players[row][col]).rjust(10), 
    print

REF: http://stackoverflow.com/questions/261006/multidimensional-array-python

Comments Off on Multidimensional array in python
comments