def __init__(self, obj, defaultflags=None):
        """
        :param obj, (int, dict):
            either the decimal form of the bitwise array, or
            a dictionary (complete or otherwise) of the form
            {flag : bool, flag : bool, ...}
        :param defaultflags, dict:
            a dictionary representing the default for
            one or more flags.  Only applicable
            when a dictionary is passed to obj.  It's
            ignored when obj is an integer.
        """

        # calculate an msb-like number, which is actually
        # the msb * 2 to get one more digit than
        # the number of flags

        self.msb = 2 ** (len(BitFlag.flags) + 1)

        # if an integer was passed...
        # convert it to a boolean array
        if isinstance(obj, int):
            self.val = obj % self.msb
            tmp = self.val + self.msb

            self.bools = []
            while tmp != 0:
                self.bools.append(tmp % 2 == 1)
                tmp >>= 1
            self.bools = self.bools[:-1]

            for i, key in enumerate(BitFlag.flags):
                setattr(self, key, self.bools[i])

        # a dict of the form {flags : bool, } was passed...
        # convert it to the boolean array just the same.
        elif isinstance(obj, (dict, list)):
            if isinstance(obj, list):
                obj = dict(zip(obj, [True] * len(obj)))
            # if there are defaultflags, use them, otherwise assume all flags
            # are unset
            if defaultflags:
                defaults = defaultflags
            else:
                defaults = zip(BitFlag.flags, [False] * len(BitFlag.flags))
                defaults = ODict(defaults)

            self.bools = []
            self.val = 0
            for i, key in enumerate(BitFlag.flags):
                if key in obj:
                    val = obj[key]
                else:
                    val = defaults[key]
                setattr(self, key, val)
                self.bools.append(val)
                if val:
                    self.val += 2 ** i
def __init__ ( self , obj , defaultflags = None ) : self . msb = 2 ** ( len ( BitFlag . flags ) + 1 ) if isinstance ( obj , int ) : self . val = obj % self . msb tmp = self . val + self . msb self . bools = [ ] while tmp != 0 : self . bools . append ( tmp % 2 == 1 ) tmp >>= 1 self . bools = self . bools [ : - 1 ] for i , key in enumerate ( BitFlag . flags ) : setattr ( self , key , self . bools [ i ] ) elif isinstance ( obj , ( dict , list ) ) : if isinstance ( obj , list ) : obj = dict ( zip ( obj , [ True ] * len ( obj ) ) ) if defaultflags : defaults = defaultflags else : defaults = zip ( BitFlag . flags , [ False ] * len ( BitFlag . flags ) ) defaults = ODict ( defaults ) self . bools = [ ] self . val = 0 for i , key in enumerate ( BitFlag . flags ) : if key in obj : val = obj [ key ] else : val = defaults [ key ] setattr ( self , key , val ) self . bools . append ( val ) if val : self . val += 2 ** i
:param obj, (int, dict):
            either the decimal form of the bitwise array, or
            a dictionary (complete or otherwise) of the form
            {flag : bool, flag : bool, ...}
        :param defaultflags, dict:
            a dictionary representing the default for
            one or more flags.  Only applicable
            when a dictionary is passed to obj.  It's
            ignored when obj is an integer.