putting a string in Mac Address form

kyle.tk kyle.tk at gmail.com
Sun Oct 30 21:24:18 EST 2005


I came up with this. Is there a better (more pythonic) way to do it?

import string
def mac_to_norm(mac):
	def bad_char(char):
		if char not in string.hexdigits:
			return False
		return True
	mac = filter(bad_char,mac)
	if len(mac) is not 12: return None
	new_mac = ''
	c = 0
	while len(new_mac) < 16:
		new_mac += mac[c:c+2] + ':'
		c=c+2
	return new_mac[:-1].upper()
print mac_to_norm('0012.ab23.b2cd')
#shows: 00:12:AB:23:B2:CD

The part I think is bad is the while loop part. Could it be better?




More information about the Python-list mailing list