[Maya-Python] Parsing flags using OR operator

143 views
Skip to first unread message

Marcus Ottosson

unread,
Dec 18, 2013, 3:00:26 AM12/18/13
to python_in...@googlegroups.com
Hi all,

What's a Pythonic way of parsing a set of flags, similar to how Qt does it.

E.g. layout.setAlignment(Qt.AlignTop | Qt.AlignLeft)

Both AlignTop and AlignLeft seem to be hex numbers (docs here) that seem to get combined into a third number via the | operator. Where's the logic behind that and how can I derive such logic myself?

Thanks,
Marcus

--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Dec 18, 2013, 5:11:01 AM12/18/13
to python_in...@googlegroups.com
They are bitwise flags. Each value in a given enum is incremented by shifting bits. Then you are able to use bitwise operators to combine multiple values and test their membership:

A = 1 << 0
B = 1 << 1
C = 1 << 2
D = 1 << 3
print A,B,C,D
# 1 2 4 8
print A | B
# 3
print A | B | B | B
# 3
flag = A|B
if flag & A: print True
# True
if flag & B: print True
# True
if flag & C: print True
#
flag ^ B    # toggle off B
# 5
flag ^ B ^ B  # toggle off and on B
# 7
flag & ~B  # subtract B
# 5
flag & ~B & ~B  # maintains the subtraction (not toggle)
# 5
flag &= ~B  # unary subtraction; assignment
# 5

Basically, once you define a set of flags, you have AND, OR, XOR, NOT operators to test and modify the flags. It lets you represent a number of states in a single value.





--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBUGHyG9vrny_BqWcdvcD3ezM-VHM_jNJYA70Rv4DTNug%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Marcus Ottosson

unread,
Dec 18, 2013, 5:29:25 AM12/18/13
to python_in...@googlegroups.com
Brilliant, that makes sense.

I also came across this thread, the accepted answer is especially helpful.

Thanks, Justin




For more options, visit https://groups.google.com/groups/opt_out.



--
Marcus Ottosson
konstr...@gmail.com

Marcus Ottosson

unread,
Dec 18, 2013, 7:23:04 AM12/18/13
to python_in...@googlegroups.com
What do you think my usage of flags within this "policy" for a os.listdir-type object?

class ExcludePolicy(object):

# Flags
ExcludeHidden = 1 << 0
ExcludeInvisible = 1 << 1
ExcludeRegex = 1 << 2
ExcludeFiles = 1 << 3

def __init__(self):
self.__flags = self.ExcludeHidden | self.ExcludeInvisible | self.ExcludeFiles
self.__regex = ''

@property
def regex(self):
return self.__regex

@regex.setter
def regex(self, regex):
self.__regex = regex

@property
def flags(self):
return self.__flags

@flags.setter
def flags(self, flags):
self.__flags = flags


class ListDir(object):
def __init__(self):
self.__excludePolicy = None

def dir(self):
if not self.__excludePolicy:
return

pol = self.__excludePolicy
if pol.flags & pol.ExcludeHidden:
print "Excluding hidden"

if pol.flags & pol.ExcludeInvisible:
print "Excluding invisible"

if pol.flags & pol.ExcludeRegex:
print 'Excluding regex: "%s"' % pol.regex

if pol.flags & pol.ExcludeFiles:
print "Excluding files"

@property
def excludePolicy(self):
return self.__excludePolicy

@excludePolicy.setter
def excludePolicy(self, policy):
self.__excludePolicy = policy


if __name__ == '__main__':
pol = ExcludePolicy()

# pol.regex = r'^*.$'
# pol.flags = pol.ExcludeRegex

ld = ListDir()
ld.excludePolicy = pol
ld.dir()

--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Dec 18, 2013, 2:23:32 PM12/18/13
to python_in...@googlegroups.com

Marcus Ottosson

unread,
Dec 18, 2013, 4:47:51 PM12/18/13
to python_in...@googlegroups.com
Woho!

For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA26JyquVL3KKD6GtCkcuA2F%3DT9fPEr3%2BtccvJOh7c2chA%40mail.gmail.com.

For more options, visit https://groups.google.com/groups/opt_out.


--
Marcus Ottosson
konstr...@gmail.com


Reply all
Reply to author
Forward
0 new messages