Skip to content

Python-Cubes/bitflags-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

bitflags-python

bitflags for both python 2 and python 3.

Quick start

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)

About

bitflags for python, both 2 and 3.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages