Monday, September 27, 2004

Python NMS Libraries

Python NMS Libraries: "The pyNMS package is a collection of Python (and some C) modules for use in network management applications. It is also useful for testing and other types of applications."

Monday, September 20, 2004

Having problems putting some source in your blog?

Especially if you can't use any html headers and stuph. Of want to add some form of binary data along with it: use this.. . . .
Stuph it in you python ide and run.
It will result in a file, once called, will create a compressed, base64, blogspot ready of any file you give it.

here'se the source (cut and paste ought to work i guess):

import base64,zlib;f=file("fileToBloggablePython.py",'wb');f.write(zlib.decompress(base64.decodestring("""eNqtUU1LwzAYvg/2H0LKSMJqQRBBRyc4hwiyg8OTSsi6t1ugTUKSMeevN+36MRl4Wg45vHk+30TRJc5wEEWovp4X7+j1ZTZfLOdP9WCmzcHKzdaj1QG9QZlp9KjBlqLjXCbAcCBLo22wEQ5ub+KfQq5i7RIj/LZ+zRHnSpTAeZpizkshFef4fjhA4Ti9sxnksgCUIiv2XCqz85T08wfCjlAP3z6t1JNMl8aCc1QbULSHxsSuCEssiDVl8V3D0zt/rt8MO/GQUmnfYptwf9lNp8SZQvoTV/Zxdf01JolwmZSJOZDOtiJVCRuNmOxbO2Ol8mg6DS8xwhifr3CSpxWH4pHDgRh6TfJkb6UHWu9gDd0Wjqx6sgbng/SGjhxjFSMrtAPKggUaIdrX6ur9Vyo+x5Og9KnIuPEEdeJZfRAbVwjSwvsvaJP8Asg+yOs=""")));f.close()

Python Locking sollution

Somewhere on a python cookbook, someone wrote something very nice. . . See the comments for the locking sollution that works cross platform, cross machine type, even over networks, can lock inter machine, inter process and interthread. Maybe even more.

This is a nice sollution to a common problem. Note though that it is easy enough to patch the code for your own requirements, as it's really simple. But effective - the Python way! :]

"""Usage example:
#test locking
import glock
file='lock.txt'
l=glock.lock(file)

#do something
time.sleep(60)
#extend the lock
l=glock.lock(file)

##do something again
time.sleep(60)

#now unlock
l.unlock()
###glock
"""
import os,time

class lock:
'''cross-platform locking.
Locking will raise exceptions.
Unlocking won't. So, unlock all you want'''
def __init__(self,nameInValidDirectory,wait=300):
"""Params::
- nameInValidDirectory: is a filename (doesn't need to exist though)
for wich the directory this file is in, is
writable for the current process, and exists on the
medium.
- wait: default period for waiting for a lock to become free: in seconds.
"""
name=nameInValidDirectory
self.wait = wait
self.d=name+'_lock/'
self.d2=name+'_lock2/'
self.locked={}
lock_successful=0
if self.locked.has_key(self.d2):
try:
#you got a lock, it is yours?
if os.stat(self.d2)[8]== self.locked.get(self.d2):
#try to extend lock before loss
#ATOMIC operation, extend may
#fail, presence of self.d will
#prevent loss of lock
#just by the act of extending
os.rmdir(self.d2)
os.mkdir(self.d2)
os.rmdir(self.d)
os.mkdir(self.d)
self.locked[self.d2]= os.stat(self.d2)[8]
lock_successful=1
if self.locked.has_key(self.d2):
del(self.locked[self.d2])
result='Fail: lost lock'
except:
if self.locked.has_key(self.d2):
del(self.locked[self.d2])
result='Fail: lost lock'
else:
for t in range(0,self.wait): #try 10 times
#not locked yet, try to lock it
try:
os.mkdir(self.d) #ATOMIC
os.mkdir(self.d2) #ATOMIC
self.locked[self.d2]= os.stat(self.d2)[8]
lock_successful=1
break
except Exception,error:
result=error
## print 'locking??',error
#mkdir probably failed
#lock already there,
#try to delete old lock
m_dir2=0;m_dir=0
if os.path.exists(self.d):
try:
m_dir2=os.stat(self.d2)[8]
except:
pass
try:
m_dir=os.stat(self.d)[8]
except:
pass
cur_tm=int(time.time())
#presence of either directory
#can stop you from taking lock
if cur_tm>m_dir+self.wait and cur_tm>m_dir2+self.wait:
#delete old locks
#ATOMIC here
os.rmdir(self.d2)
os.mkdir(self.d2)
os.rmdir(self.d)
os.mkdir(self.d)
self.locked[self.d2]= os.stat(self.d2)[8]
lock_successful=1
break
time.sleep(1)
#made it thru the loop, so we got no lock
if not lock_successful:
raise result

def unlock(self):
'''does not raise an exception,
safe to unlock as often as you want
it may just do nothing'''
if self.locked.has_key(self.d2):
#we're the ones that unlocked it,
#if time matched
if self.locked[self.d2]== os.stat(self.d2)[8]:
try:
del(self.locked[self.d2])
os.rmdir(self.d2)
os.rmdir(self.d)
return 1
except:
return 0
else:
del(self.locked[self.d2])
return 0
else:
return 0

if __name__ == "__main__":
print 'testing lock'
file='fred.txt'
l=lock(file, wait=20)
## f=open(file,'w')
## f.write('test')
## f.close()
raw_input("Locked '%s', presse enter to unlock" % (file))
l.unlock()



Saturday, September 18, 2004

Python Packages Index: ctypes 0.9.1

Python Packages Index: ctypes 0.9.1: "ctypes is a Python package to create and manipulate C data types in Python, and to call functions in dynamic link libraries/shared dlls. It allows wrapping these libraries in pure Python."

Tuesday, September 14, 2004

pypcap

pypcap: "pypcap
simplified object-oriented Python extension module for libpcap - the current tcpdump.org version, the legacy version shipping with some of the BSD operating systems, and the WinPcap-3.x port for Windows."

Durus

Durus: "This is Durus, a persistent object system for applications written in the Python programming language. Durus offers an easy way to use and maintain a consistent collection of object instances used by one or more processes. Access and change of a persistent instances is managed through a cached Connection instance which includes commit() and abort() methods so that changes are transactional. Durus is best suited to collections of less than a million instances with relatively stable state."

Tuesday, September 07, 2004

QuirksMode - for all your browser quirks

QuirksMode - for all your browser quirks
Javascript, HTML, CSS, Browsers etc. Hoe en wanneer gebruik je welke?!

Monday, September 06, 2004

ImageMagick - Convert, Edit, and Compose Images

ImageMagick - Convert, Edit, and Compose Images: "ImageMagickTM 5.5.4 is a robust collection of tools and libraries offered under a usage license to read, write, and manipulate an image in many image formats (over 87 major formats) including popular formats like TIFF, JPEG, PNG, PDF, PhotoCD, and GIF. With ImageMagick you can create images dynamically, making it suitable for Web applications. You can also resize, rotate, sharpen, color reduce, or add special effects to an image or image sequence and save your completed work in the same or differing image format. Image processing operations are available from the command line, as well as through C, C , Perl, or Java programming interfaces."

Sunday, September 05, 2004

NoMachine NX - Get NX software from NoMachine

NoMachine NX - Get NX software from NoMachine: " Download the NX Client for your Operating System and enjoy the speed and power of NX products. You can even apply for a free evaluation version of NX server software."

VNC, RDP en (free)NX terminal client. Hiermee moet het mogelijk zijn om ook de FreeNX server te bekijken/gebruiken - en die is meegeinstalleerd onder Knoppix.