Archive for October, 2010

 

Basic threading in python ..

Oct 20, 2010 in Python

The “main” script:

# ttest.py
import threading
import someclass

csc = someclass.SomeClass()
csc_thread = threading.Thread(target=csc.run,args=())
csc_thread.start()

The class used to run in a thread:


# someclass.py
class SomeClass:
    def __init__(self):
        print "init"

    def run(self):
        # import pdb
        # pdb.set_trace() # trigger debugger
        for ix in range(1,10):
            print "run %d" % ix

To run it:

python [-m pdb] ttest.py

rpm verify package file ..

Oct 20, 2010 in Linux

http://www.rpm.org/max-rpm/ch-rpm-checksig.html

rpm -K | –checksig file.rpm

Python dynamic class loading ..

Oct 16, 2010 in Python

File “mod_loader.py” :

# file: mod_loader.py

mod_name = "pkg.mod_loadee"
# Get module object (class is stored in a module)
mod_obj = __import__(mod_name, globals(), locals(), [''])

print ">> %s" % mod_obj

# Get class
class_obj = mod_obj.getClass()

print ">> %s" % class_obj

# Create instance from class object
class_inst = class_obj()

# invoke method
class_inst.testme()

in folder “pkg”, make file “mod_loadee.py” :


class Loadee:
    def __init__(self):
        print "Loadee.init"
        
    def testme(self):
        print "Loadee.testme"

def getClass():
    return Loadee

run it:

python mod_loader.py

do not forget the “./pkg/__init__.py” to make the folder a package .

.