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()



On 18 December 2013 10:29, Marcus Ottosson <[email protected]> wrote:

> Brilliant, that makes sense.
>
> I also came across this thread, the accepted answer is especially helpful.
> http://stackoverflow.com/questions/276706/what-are-bitwise-operators
>
> Thanks, Justin
>
>
>
> On 18 December 2013 10:11, Justin Israel <[email protected]> wrote:
>
>> 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:
>> https://wiki.python.org/moin/BitwiseOperators
>>
>> 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.
>>
>>
>>
>>
>>
>> On Wed, Dec 18, 2013 at 9:00 PM, Marcus Ottosson 
>> <[email protected]>wrote:
>>
>>> 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<http://qt-project.org/doc/qt-5.1/qtcore/qt.html>)
>>> 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*
>>> [email protected]
>>>
>>> --
>>> 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 [email protected].
>>> 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.
>>>
>>
>>  --
>> 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 [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1sQCWRnfP5LVZRNFpRM1uPMmNZYEikOLSFRZ%3DUGE16ig%40mail.gmail.com
>> .
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> --
> *Marcus Ottosson*
> [email protected]
>



-- 
*Marcus Ottosson*
[email protected]

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODL40VrgCeZ-P%2Bc%3DwpB9feVZFd0KnC79BNtbw5xrxANeA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to