Turn a list of strings into an `IntFlag`
I have a bunch of IntFlag
types, and I expect to frequently convert lists of strings from config files into members of those types. My current plan is to extend IntFlag
:
class BetterIntFlag(IntFlag):
@classmethod
def parse(cls, items):
value = cls(0)
for item in items:
value |= cls[item]
return value
I'm satisfied with this solution, but I can't help but feel that I must be missing a concise built-in way to do this.
I'm on 3.3 with backported enums.
python enums
add a comment |
I have a bunch of IntFlag
types, and I expect to frequently convert lists of strings from config files into members of those types. My current plan is to extend IntFlag
:
class BetterIntFlag(IntFlag):
@classmethod
def parse(cls, items):
value = cls(0)
for item in items:
value |= cls[item]
return value
I'm satisfied with this solution, but I can't help but feel that I must be missing a concise built-in way to do this.
I'm on 3.3 with backported enums.
python enums
add a comment |
I have a bunch of IntFlag
types, and I expect to frequently convert lists of strings from config files into members of those types. My current plan is to extend IntFlag
:
class BetterIntFlag(IntFlag):
@classmethod
def parse(cls, items):
value = cls(0)
for item in items:
value |= cls[item]
return value
I'm satisfied with this solution, but I can't help but feel that I must be missing a concise built-in way to do this.
I'm on 3.3 with backported enums.
python enums
I have a bunch of IntFlag
types, and I expect to frequently convert lists of strings from config files into members of those types. My current plan is to extend IntFlag
:
class BetterIntFlag(IntFlag):
@classmethod
def parse(cls, items):
value = cls(0)
for item in items:
value |= cls[item]
return value
I'm satisfied with this solution, but I can't help but feel that I must be missing a concise built-in way to do this.
I'm on 3.3 with backported enums.
python enums
python enums
asked Nov 21 '18 at 3:59
Thom Smith
10.9k23667
10.9k23667
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If by backport you mean aenum
1, it's built-in:
from aenum import IntFlag
class Color(IntFlag):
red = 1
green = 2
blue = 4
and in use:
--> Color['red|blue']
<Color.blue|red: 5>
--> items = ['red', 'blue']
--> Color['|'.join(items)]
<Color.blue|red: 5>
1 Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
Alas, I'm usingenum.py
from Python 3.6.5, which does not support that.
– Thom Smith
Nov 21 '18 at 18:02
In any case, given that you've made this suggestion, it seems safe to say that there's no built-in functionality for this in stdlib.
– Thom Smith
Nov 26 '18 at 17:44
@ThomSmith: You could blur the lines between names and values and define your own_missing_
to handle calls likeColor('red blue')
(or however you wanted your strings).
– Ethan Furman
Nov 26 '18 at 21:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405076%2fturn-a-list-of-strings-into-an-intflag%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If by backport you mean aenum
1, it's built-in:
from aenum import IntFlag
class Color(IntFlag):
red = 1
green = 2
blue = 4
and in use:
--> Color['red|blue']
<Color.blue|red: 5>
--> items = ['red', 'blue']
--> Color['|'.join(items)]
<Color.blue|red: 5>
1 Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
Alas, I'm usingenum.py
from Python 3.6.5, which does not support that.
– Thom Smith
Nov 21 '18 at 18:02
In any case, given that you've made this suggestion, it seems safe to say that there's no built-in functionality for this in stdlib.
– Thom Smith
Nov 26 '18 at 17:44
@ThomSmith: You could blur the lines between names and values and define your own_missing_
to handle calls likeColor('red blue')
(or however you wanted your strings).
– Ethan Furman
Nov 26 '18 at 21:44
add a comment |
If by backport you mean aenum
1, it's built-in:
from aenum import IntFlag
class Color(IntFlag):
red = 1
green = 2
blue = 4
and in use:
--> Color['red|blue']
<Color.blue|red: 5>
--> items = ['red', 'blue']
--> Color['|'.join(items)]
<Color.blue|red: 5>
1 Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
Alas, I'm usingenum.py
from Python 3.6.5, which does not support that.
– Thom Smith
Nov 21 '18 at 18:02
In any case, given that you've made this suggestion, it seems safe to say that there's no built-in functionality for this in stdlib.
– Thom Smith
Nov 26 '18 at 17:44
@ThomSmith: You could blur the lines between names and values and define your own_missing_
to handle calls likeColor('red blue')
(or however you wanted your strings).
– Ethan Furman
Nov 26 '18 at 21:44
add a comment |
If by backport you mean aenum
1, it's built-in:
from aenum import IntFlag
class Color(IntFlag):
red = 1
green = 2
blue = 4
and in use:
--> Color['red|blue']
<Color.blue|red: 5>
--> items = ['red', 'blue']
--> Color['|'.join(items)]
<Color.blue|red: 5>
1 Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
If by backport you mean aenum
1, it's built-in:
from aenum import IntFlag
class Color(IntFlag):
red = 1
green = 2
blue = 4
and in use:
--> Color['red|blue']
<Color.blue|red: 5>
--> items = ['red', 'blue']
--> Color['|'.join(items)]
<Color.blue|red: 5>
1 Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
edited Nov 21 '18 at 16:38
answered Nov 21 '18 at 16:16
Ethan Furman
35.8k1188150
35.8k1188150
Alas, I'm usingenum.py
from Python 3.6.5, which does not support that.
– Thom Smith
Nov 21 '18 at 18:02
In any case, given that you've made this suggestion, it seems safe to say that there's no built-in functionality for this in stdlib.
– Thom Smith
Nov 26 '18 at 17:44
@ThomSmith: You could blur the lines between names and values and define your own_missing_
to handle calls likeColor('red blue')
(or however you wanted your strings).
– Ethan Furman
Nov 26 '18 at 21:44
add a comment |
Alas, I'm usingenum.py
from Python 3.6.5, which does not support that.
– Thom Smith
Nov 21 '18 at 18:02
In any case, given that you've made this suggestion, it seems safe to say that there's no built-in functionality for this in stdlib.
– Thom Smith
Nov 26 '18 at 17:44
@ThomSmith: You could blur the lines between names and values and define your own_missing_
to handle calls likeColor('red blue')
(or however you wanted your strings).
– Ethan Furman
Nov 26 '18 at 21:44
Alas, I'm using
enum.py
from Python 3.6.5, which does not support that.– Thom Smith
Nov 21 '18 at 18:02
Alas, I'm using
enum.py
from Python 3.6.5, which does not support that.– Thom Smith
Nov 21 '18 at 18:02
In any case, given that you've made this suggestion, it seems safe to say that there's no built-in functionality for this in stdlib.
– Thom Smith
Nov 26 '18 at 17:44
In any case, given that you've made this suggestion, it seems safe to say that there's no built-in functionality for this in stdlib.
– Thom Smith
Nov 26 '18 at 17:44
@ThomSmith: You could blur the lines between names and values and define your own
_missing_
to handle calls like Color('red blue')
(or however you wanted your strings).– Ethan Furman
Nov 26 '18 at 21:44
@ThomSmith: You could blur the lines between names and values and define your own
_missing_
to handle calls like Color('red blue')
(or however you wanted your strings).– Ethan Furman
Nov 26 '18 at 21:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405076%2fturn-a-list-of-strings-into-an-intflag%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown