Turn a list of strings into an `IntFlag`












2














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.










share|improve this question



























    2














    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.










    share|improve this question

























      2












      2








      2







      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 3:59









      Thom Smith

      10.9k23667




      10.9k23667
























          1 Answer
          1






          active

          oldest

          votes


















          1














          If by backport you mean aenum1, 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.






          share|improve this answer























          • 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










          • @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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          If by backport you mean aenum1, 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.






          share|improve this answer























          • 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










          • @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
















          1














          If by backport you mean aenum1, 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.






          share|improve this answer























          • 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










          • @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














          1












          1








          1






          If by backport you mean aenum1, 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.






          share|improve this answer














          If by backport you mean aenum1, 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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 16:38

























          answered Nov 21 '18 at 16:16









          Ethan Furman

          35.8k1188150




          35.8k1188150












          • 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










          • @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


















          • 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










          • @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
















          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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin