I have several flags:
None = 0
HR = 1
HD = 2,
DT = 4,
Fl = 8
..
etc
I want to create a function where I input a certain flag, lets say : 6.
Returned should be HDDT, since 2 | 4 = 6. It can also happen that 3 or more flags are combined or just a single one. e.g. : 7 = 1 | 2 | 4 => HRHDDT.
How can I return the concated string depending on the flag value?
In my case the enum has many more entries, which would make a simple if statement really uncomfortable, because I would have to cover hundreds of cases.
Is there any clever solution to this?
Let's assume we have a list somewhere with the powers of two:
flags = ['HR','HD','DT','Fl']
Then you can write the following function:
def power2():
i = 1
while True:
yield i
i *= 2
def obtain_flag(val):
if val:
return ''.join([f for f,i in zip(flags,power2()) if i&val])
else:
return None
The return None
is not necessary: it is the flag that maps to zero.
Or if you want a list of the flags (in that case the flags can be anything):
def obtain_flag(val):
if val:
return [f for f,i in zip(flags,power2()) if i&val]
This gives:
>>> obtain_flag(6)
'HDDT'
>>> obtain_flag(7)
'HRHDDT'
>>> obtain_flag(0) # returns None, but None is not printed
>>> obtain_flag(1)
'HR'
>>> obtain_flag(2)
'HD'
>>> obtain_flag(3)
'HRHD'
>>> obtain_flag(4)
'DT'
You can store the flags as a dict
like so:
flags = {1:'HR', 2:'HD', 4:'DT', 8:'FL'}
and bit-wise and
your number with the flags to retrieve the strings:
def get_flags(num):
if num:
return ''.join(flags[x] for x in flags if x & num)
return None
>>> get_flags(6)
'HDDT'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With