Package: bitflags
Bit flags implementation using a C Union. This library removes the need to use ctypes and helps you quickly access what
bits are toggled.This class is built off of the Bit Manipulation guide found athttps://wiki.python.org/moin/BitManipulationunder the
Bit fields section.This library includes a class based approach to bit flags (BitFlags) and a one time dynamic bit flags object (bitflags).The individual bits can always be accessed with ‘flag.bit_0’, ‘flag.bit_1’, ‘flag.bit_2’, …Example - BitFlagsThis is the class based approach.frombitflagsimportBitFlagsclassMyFlags(BitFlags):options={0:"flag1",1:"flag2",2:"flag3",3:"flag4",4:"Something Happened"}f=MyFlags(0)assertf.value==0assertint(f)==0f.value=0b101# 5 - bin(5) shows the bit values (0b101)assertf.value==0b101# You can always access the bit value with 'bit_X'# Access all of the bits (The number of bits can be changed by setting the class attribute nbits or nbytesprint(f.bit_7,f.bit_6,f.bit_5,f.bit_4,f.bit_3,f.bit_2,f.bit_1,f.bit_0)# 0 0 0 0 0 1 0 1# Access the custom flags as attributesassertf.flag1==1assertf.flag2==0assertf.flag3==1assertf.flag4==0assertf.something_happened==0# Get a list of flag optionsassertf.get_flags()==['flag1','flag3']# Convert to use the data typesassertstr(f)=='flag1, flag3'assertint(f)==5assertbytes(f)==b'\x05'This class was made to be flexible if you want the attributes to be different from the display options.frombitflagsimportBitFlagsclassMyFlags(BitFlags):options={0:"Failure",1:"Warning",2:"System 2% Overloaded"}f=MyFlags(0b111)asserthasattr(f,'failure')asserthasattr(f,'warning')asserthasattr(f,'system_2_overloaded')assertf.get_flags()==['Failure','Warning','System 2% Overloaded']classSpecialFlags(BitFlags):options={0:"2% System Failure",# Note: variable name cannot start with a number!1:"System Overloaded",2:"System Safe"}fields={'system_failure':0,'system_overload':1,'safe':2}# Custom variables to access the bitss=SpecialFlags(7)asserts.system_failure==1asserts.system_overload==1asserts.safe==1asserts.get_flags()==["2% System Failure","System Overloaded","System Safe"]s2=SpecialFlags(1)asserts.get_flags()==["2% System Failure"]You can also make a pattern for options.frombitflagsimportBitFlagsclassMyFlags(BitFlags):pattern='%i'f=MyFlags()f.value=0b101# 5 - bin(5) shows the bit values (0b101)assertf.value==0b101# Get a list of flag optionsassertf.get_flags()==['0','2']# Convert to use the data typesassertstr(f)=='0, 2'assertint(f)==5assertbytes(f)==b'\x05'Example - bitflagsThe one time object bit flags. This is basically the same thing as BitFlags only the instance constructor allows you
to set the options, fields, and number of bits/bytes.frombitflagsimportbitflagsf=bitflags(flag1=1,flag3=1,options={0:"flag1",1:"flag2",2:"flag3",3:"flag4",4:"Something Happened"})assertf.value==0b101assertf.flag1==1assertf.flag2==0assertf.flag3==1assertf.flag4==0assertf.something_happened==0# Change the fields that access the bits.f.set_fields({'a':0,'b':1,'c':2,'d':3})assertf.a==f.bit_0assertf.b==f.bit_1assertf.c==f.bit_2assertf.d==f.bit_3The bitflags constructor uses type to create a new BitFlags class. This class isn’t really re-usable unless you access
that class from the object that was created.frombitflagsimportbitflagsf=bitflags(flag1=1,flag3=1,options={0:"flag1",1:"flag2",2:"flag3",3:"flag4",4:"Something Happened"})assertf.value==0b101f2=type(f)(0b1)assertf2.flag1==1assertf2.value==1assertf.value==0b101f3=f.__class__(0b10)assertf3.flag1==0assertf3.flag2==1assertf3.value==2assertf2.value==1assertf.value==0b101If you want to use multiple bit flag objects that have the same fields then it is better to use BitFlags class
inheritance.