[Python-ideas] IntFlags
Andrew Barnert
abarnert at yahoo.com
Wed Mar 4 23:30:07 CET 2015
On Mar 4, 2015, at 7:44, Neil Girdhar <mistersheik at gmail.com> wrote:
> Why do you need these composite flags?
Because the whole point of this proposal is to deal with C types (otherwise, who cares about the int value?). And most such C types define combined values--the motivating example, stat, has S_IRWXU, etc. If the Python code is less readable than the equivalent C code...
> On Wed, Mar 4, 2015 at 10:31 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> On Thu, Mar 5, 2015 at 2:17 AM, Andrew Barnert
>> <abarnert at yahoo.com.dmarc.invalid> wrote:
>> > Another issue that came up was that C flags often have "combined" names that are ambiguous: RDWR = RDONLY | WRONLY), which is fine until you want a repr (in C, it's just going to print 3); does it have to be smart enough to show RDWR? (Or, worse, RDWR | CLOEXEC.)
>> >
>>
>> That could probably be handled by going through the flags in iteration
>> order. If the flag is present, emit it and move on. Something like
>> this:
>>
>> from enum import IntEnum
>>
>> class Flags(IntEnum):
>> RDWR = 3
>> RDONLY = 1
>> WRONLY = 2
>> CLOEXEC = 4
>>
>> def flag_str(flg):
>> names = []
>> for flag in Flags:
>> if (flg&flag) == flag:
>> flg -= flag
>> names.append(str(flag))
>> return "|".join(names)
>>
>> print(flag_str(Flags.RDWR|Flags.CLOEXEC))
>> print(flag_str(Flags.RDONLY|Flags.CLOEXEC))
>>
>>
>> As long as the combined versions come up ahead of the others, they'll
>> be used. Alternatively, if you prefer them _not_ to be used, just put
>> them after the individual forms, and then the str() will expand them
>> out.
>>
>> ChrisA
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>> --
>>
>> ---
>> You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/L5KfCEXFaII/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe at googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150304/d6c0e0ab/attachment-0001.html>
More information about the Python-ideas
mailing list