# -*- coding: utf-8 -*-
	
class BitFlags(object):
	"""
		v = BitFlags(5, ['race', 'sex', 'alive']) #  v.race is True, v.sex is False, v.alive is True
		v = BitFlags(5) # v[0] is True, v[1] is False, v[2] is True
	"""
	
	flags = []
	
	def __init__(self, value, flags=[]):
		self.bitmask = value
		self.flags = flags
		
	def __getitem__(self, key):
		assert isinstance(key, int) and key >= 0, "key must be positive integer"
		bit = 1
		bit <<= key
		return bool(self.bitmask & bit)
	
	def __setitem__(self, key, value):
		assert isinstance(key, int) and key >= 0, "key must be positive integer"
		bit = 1
		bit <<= key
		if value:
			self.bitmask |= bit
		else:
			self.bitmask &= ~bit
	
	def __getattr__(self, name):
		if name in self.flags:
			return self[self.flags.index(name)]
		raise AttributeError
		
	def __setattr__(self, name, value):
		if name in self.flags:
			self[self.flags.index(name)] = value
		super(BitFlags, self).__setattr__(name, value)
		
	def __int__(self):
		return self.bitmask
	
	def __repr__(self):
		return '<%s: %s>' % (self.__class__.__name__, int(self))
	
	# introspection support:
	__members__ = property(lambda self: self.__dir__())

	def __dir__(self):
		result = self.__dict__.keys()
		result.extend(self.flags)
		return result

@@ -1,8 +1,27 @@
+#!/usr/bin/python%0D%0A
 # -*- co
@@ -37,17 +37,16 @@
 -8 -*-%0D%0A
-%09
 %0D%0Aclass 
@@ -340,16 +340,100 @@
 flags%0D%0A%09
+%0D%0A%09def __repr__(self):%0D%0A%09%09return '%3C%25s: %25s%3E' %25 (self.__class__.__name__, int(self))%0D%0A
 %09%0D%0A%09def 
@@ -932,19 +932,18 @@
 Error%0D%0A%09
-%09
 %0D%0A
+
 %09def __s
@@ -1090,19 +1090,18 @@
 alue)%0D%0A%09
-%09
 %0D%0A
+
 %09def __i
@@ -1143,93 +1143,8 @@
 %0A%09%0D%0A
-%09def __repr__(self):%0D%0A%09%09return '%3C%25s: %25s%3E' %25 (self.__class__.__name__, int(self))%0D%0A%09%0D%0A
 %09# i
@@ -1305,16 +1305,16 @@
 flags)%0D%0A
-
 %09%09return
@@ -1322,8 +1322,132 @@
 result%0D%0A
+%09%0D%0A%09%0D%0A%09def dict(self):%0D%0A%09%09%22%22%22 Convert the BitFlags to a dict %22%22%22%0D%0A%09%09return dict((k, getattr(self, k)) for k in self.flags)%0D%0A

Implement BitFlags.dict()