
NAME, GROUP, PRIORITY, HOME, MOBILE, OFFICE, SPEED, EMAIL, MELODY, SNAPSHOT, CDATE, MDATE = range(12)
record = 'name', 'group', 'prority', 'home', 'mobile', 'office', 'speed', 'email', 'melody', 'snapshot', 'cdate', 'mdate'

class NamedListMetaClass(type):
	def __init__(cls, cls_name, bases, attrs):
		members = attrs['members']
		member_list = members.split()
		setattr(cls, '__slots__', ())
		def property_factory(index):
			def property_get(self):
				return self[index]
			def property_set(self, v):
				self[index] = v
			return property_get, property_set
		for i, member in enumerate(member_list):
			setattr(cls, member, property(*property_factory(i)))
		super(NamedListMetaClass, cls).__init__(cls_name, bases, attrs)
	def __call__(self, *args):
		# self: class
		instance = super(NamedListMetaClass, self).__call__(args)
		return instance

class PhoneItem(list):
	__metaclass__ = NamedListMetaClass
	members = 'name group prority home mobile office speed email melody snapshot cdate mdate'
	def __init__(self, args):
		if not args:
			list.__init__(self, [''] * len(self.members.split()))
		elif len(args) == 1:
			list.__init__(self, *args)
		else:
			list.__init__(self, args)
	def is_valid(self):
		return self[0] or self[1] or self[2] or self[3] or self[4]




print 'drop table phonedb;'
print 'create table phonedb ( NAME, _GROUP, PRIORITY, HOME, MOBILE, OFFICE, SPEED, EMAIL, MELODY, SNAPSHOT, CDATE, MDATE );'
for line in file('phonenumber.txt'):
        item = line[:-1].split('\t')
        PhoneItem(item)
        print 'insert into phonedb values ( "' + item[NAME] + '", "' + item[GROUP] + '", "' + item[PRIORITY] + '", "' + item[HOME] + '", "' + item[MOBILE] + '", "' + item[OFFICE] + '", "' + item[SPEED] + '", "' + item[EMAIL] + '", "' + item[MELODY] + '", "' + item[SNAPSHOT] + '", "' + item[CDATE] + '", "' + item[MDATE] + '");'



