bitflags for both python 2 and python 3.
First, you have to define your bits, starts from bit 0, one by one:
>>> from bitflags import BitFlags
>>> class TextStyle(BitFlags):
... names = (
... 'bold',
... 'italic',
... 'underline',
... )
>>>
Then, you've got all bit masks, with same name but in uppercase:
>>> TextStyle.BOLD
TextStyle(bits=1)
>>> TextStyle.ITALIC
TextStyle(bits=2)
>>> TextStyle.UNDERLINE
TextStyle(bits=4)
You can combinate different masks to make what you want:
>> my_style = TextStyle.BOLD | TextStyle.UNDERLINE
>>> my_style
TextStyle(bits=5)
You can tell whether specific bit is set:
>>> my_style.is_bold
True
>>> my_style.is_italic
False
>>> my_style.is_underline
True
Also, you can get value of given bit:
>>> my_style.bold
TextStyle(bits=1)
>>> my_style.italic
TextStyle(bits=0)
>>> my_style.underline
TextStyle(bits=4)
You can get all set bits:
>>> my_style.flags
['bold', 'underline']
You can use bit operator to set another bit:
>>> my_style |= TextStyle.ITALIC
>>> my_style
TextStyle(bits=7)
>>> my_style.is_italic
True
>>> my_style.italic
TextStyle(bits=2)