[Python-ideas] IntFlags
Chris Angelico
rosuav at gmail.com
Wed Mar 4 16:31:25 CET 2015
More information about the Python-ideas mailing list
Wed Mar 4 16:31:25 CET 2015
- Previous message (by thread): [Python-ideas] IntFlags
- Next message (by thread): [Python-ideas] IntFlags
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message (by thread): [Python-ideas] IntFlags
- Next message (by thread): [Python-ideas] IntFlags
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list