Posts

[Level 2] gtk shell script for gcc/vala

1. get vala syntax from gnome [gcc.sh] #!/bin/bash -vx output=`echo $1 | sed -e 's/\.c$//'` gcc -Wall $1 -o $output `pkg-config --cflags --libs gtk+-2.0` $output $ alias gcc='./gcc.sh ' $ gcc ./1.helloworld.c [valac.sh] #!/bin/bash output=`echo $1 | sed -e 's/\.vala$//'` /usr/bin/valac --pkg gtk+-2.0 $1 $output $ alias valac='./valac.sh ' $ valac ./3.helloworld.vala Wish this helps. regards, Stanley Huang

[Level 2] How to verify instance has attribute (variables in global/local) or not.

$ cat ./test1.py #!/bin/env python class myClass: def __init__(self): self.attr1 = 'a' self.attr2 = 'b' def checkAttr(self, sAttr): return hasattr(self, sAttr) mc = myClass() print hasattr(mc, 'attr1') print hasattr(mc, 'attr2') print hasattr(mc, 'attr3') print mc.checkAttr('attr1') print mc.checkAttr('attr2') print mc.checkAttr('attr3') $ ./test1.py True True False True True False $ cat ./test2.py #!/bin/env python ## cannot extract test locals() in another method! ## def testExistInGlobal(): # print globals() # print locals() print 'a in globals(): %s' % ('Ture' if 'a' in globals() else 'False') print 'b in globals(): %s' % ('Ture' if 'b' in globals() else 'False') # print 'a in locals(): %s' % ('Ture' if 'a' in locals() else 'False') # print 'b in locals(): %s' % ...

[Level 2] get broadcast by Python

I wrote a python script to get broadcast by giving ip and netmask. And I use "lambda" and "map" function to shorten my code, so it's also a good sample for you to study map function. More information please refer here . The sample code is as the following: #!/bin/env python def getBroadcast(self, ip=None, netmask=None): if not ip or not netmask: return None ip = ip.split('.') netmask = netmask.split('.') for n in range(0,4): if netmask[n] == '0': break bc = (map(lambda a, b: str(int(a, 10)&int(b,10)), ip[0:n]+['255']*(4-n), netmask[0:n]+['255']*(4-n))) if n > 1: bc[n-1] = str(int(bc[n-1]) + (255 - int(netmask[n-1]))) return '.'.join(bc) print getBroadcast('192.168.10.1','255.255.254.0') Result: 192.168.11.255 Wish this helps. regards, Stanley Huang

[Level 2] Get ether list by Python in Ubuntu.

#!/bin/env python def getEtherList(): return [x.strip() for x in \ os.popen("ifconfig -a | \ grep Link | grep '^[^ ]' | \ grep -v lo | awk '{print($1)}'")\ .readlines()] print getEtherList() Wish this helps. regards, Stanley Huang

[Level 2] iif in Python

If you want to implement iif in Python, please refer to the following sample code: #!/bin/env python def iif(b): return 'It\'s True' if b else 'It\'s False' print iif(True) print iif(False) And the following sample, guess what will you get! #!/bin/env python b = True print 'b: %s' % 'Ture' if b else 'False' b = False print 'b: %s' % 'Ture' if b else 'False' Result: $ ./iif.py b: Ture False The second result is "False", not "b: False". Why??? Because the prededence of string concatenation is higher than if. Therefore you need to use () to make it correct. #!/bin/env python b = True print 'b: %s' % 'Ture' if b else 'False' b = False print 'b: %s' % ('Ture' if b else 'False') Wish this helps. regards, Stanley Huang

[Level 2] Implement prefix and netmask transfer with Python.

Sometimes, you want to transfer between netmask and prefix setting. You can use the following sample script which is written by Python. #!/bin/env python def transferPrefix2Netmask(self, prefix): netmask = '' nLeadingFF = prefix / 8 netmask += '.'.join(['255']*nLeadingFF) if nLeadingFF != 4: netmask += '.' + str(256-2**(8-prefix%8)) nSuffix00 = 3 - nLeadingFF if nLeadingFF < 3: netmask += '.' + '.'.join(['00']*nSuffix00) return netmask def transferNetmask2Prefix(self, netmask): prefix = 0 left = 0 for i in netmask.split('.'): if i == '255': prefix += 8 else: left = int(i) break if left != 0: for n in range(7, 0, -1): prefix += 1 if left - 2 ** n == 0: break ...

[Level 2] How to reset interface seq. no in Ubuntu

If you want to reset the seq. no of interface. You can remove the record in /etc/udev/rules.d/70-persistent-net.rules. Wish this helps. regards, Stanley Huang