BinaryFlags is a tiny Python library that can handle binary flags.
Example
from binflags import BinaryFlags
# Define the possible flags.
access_flags = BinaryFlags('>H', {
'acc_public': 0x0001,
'acc_final': 0x0010,
'acc_super': 0x0020,
'acc_interface': 0x0200,
'acc_abstract': 0x0400,
'acc_synthetic': 0x1000,
'acc_annotation': 0x2000,
'acc_enum': 0x4000
})
# Toggle some flags.
print(b.value) # 0
b.acc_enum = 1
print(hex(b.value)) # 0x4000
b.acc_enum = False
print(hex(b.value)) # 0x0
b.acc_interface = True
b.acc_annotation = 1
print(hex(b.value)) # 0x2200
# Pack to a string
print(b.pack())
# And unpack from a string
b.unpack(io.read(2))
Product's homepage
Requirements:
· Python