[Python-ideas] IntFlags

Neil Girdhar mistersheik at gmail.com
Wed Mar 4 16:44:33 CET 2015


Why do you need these composite flags?

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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150304/fe22189d/attachment.html>


More information about the Python-ideas mailing list