Configuration file format, complex datastructures (list, dictionaries), comments, editable












3














I have long used normal python files for configuration purposes. I can include comments (to remind me what an entry was all about if the variable name isn't sufficient) and supports lists and dictionaries.



Now that I need to have something for a customer importing this seems a bit dangerous as it could break the system if they make an error editing by hand.



In the future I want to be able to update the file from within a program, and also be able to edit it with a normal editor ( and prefer not having to match <> or ():




  • XML is inappropriate as it is binary in ascii disguise

  • .ini files have to little hierarchical structure

  • JSON cannot handle comments.


I am now considering adding comments to JSON, but that essentially would require rewriting the parser to preserve comments. And JSON is not that nicely readable.



There is a ConfigObj library for .ini files that preserves comments, but the .ini is to flat for my needs (lists of dicts containing values that are lists of dicts).



Is there some other config file format I should use? Or should I look at parsing my .py files in safe way before importing them (that also allows writing them out again)?










share|improve this question


















  • 1




    You might look at YAML (I'm not sure at a glance whether it supports your requirements). Alternatively, you can simply include comments in your JSON under a 'comments' key. It may seem like a kludge, but it has the added benefit that your comments can be parsed and manipulated easily.
    – jme
    Apr 2 '15 at 14:29
















3














I have long used normal python files for configuration purposes. I can include comments (to remind me what an entry was all about if the variable name isn't sufficient) and supports lists and dictionaries.



Now that I need to have something for a customer importing this seems a bit dangerous as it could break the system if they make an error editing by hand.



In the future I want to be able to update the file from within a program, and also be able to edit it with a normal editor ( and prefer not having to match <> or ():




  • XML is inappropriate as it is binary in ascii disguise

  • .ini files have to little hierarchical structure

  • JSON cannot handle comments.


I am now considering adding comments to JSON, but that essentially would require rewriting the parser to preserve comments. And JSON is not that nicely readable.



There is a ConfigObj library for .ini files that preserves comments, but the .ini is to flat for my needs (lists of dicts containing values that are lists of dicts).



Is there some other config file format I should use? Or should I look at parsing my .py files in safe way before importing them (that also allows writing them out again)?










share|improve this question


















  • 1




    You might look at YAML (I'm not sure at a glance whether it supports your requirements). Alternatively, you can simply include comments in your JSON under a 'comments' key. It may seem like a kludge, but it has the added benefit that your comments can be parsed and manipulated easily.
    – jme
    Apr 2 '15 at 14:29














3












3








3


1





I have long used normal python files for configuration purposes. I can include comments (to remind me what an entry was all about if the variable name isn't sufficient) and supports lists and dictionaries.



Now that I need to have something for a customer importing this seems a bit dangerous as it could break the system if they make an error editing by hand.



In the future I want to be able to update the file from within a program, and also be able to edit it with a normal editor ( and prefer not having to match <> or ():




  • XML is inappropriate as it is binary in ascii disguise

  • .ini files have to little hierarchical structure

  • JSON cannot handle comments.


I am now considering adding comments to JSON, but that essentially would require rewriting the parser to preserve comments. And JSON is not that nicely readable.



There is a ConfigObj library for .ini files that preserves comments, but the .ini is to flat for my needs (lists of dicts containing values that are lists of dicts).



Is there some other config file format I should use? Or should I look at parsing my .py files in safe way before importing them (that also allows writing them out again)?










share|improve this question













I have long used normal python files for configuration purposes. I can include comments (to remind me what an entry was all about if the variable name isn't sufficient) and supports lists and dictionaries.



Now that I need to have something for a customer importing this seems a bit dangerous as it could break the system if they make an error editing by hand.



In the future I want to be able to update the file from within a program, and also be able to edit it with a normal editor ( and prefer not having to match <> or ():




  • XML is inappropriate as it is binary in ascii disguise

  • .ini files have to little hierarchical structure

  • JSON cannot handle comments.


I am now considering adding comments to JSON, but that essentially would require rewriting the parser to preserve comments. And JSON is not that nicely readable.



There is a ConfigObj library for .ini files that preserves comments, but the .ini is to flat for my needs (lists of dicts containing values that are lists of dicts).



Is there some other config file format I should use? Or should I look at parsing my .py files in safe way before importing them (that also allows writing them out again)?







python config






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 2 '15 at 14:22









Frida

204




204








  • 1




    You might look at YAML (I'm not sure at a glance whether it supports your requirements). Alternatively, you can simply include comments in your JSON under a 'comments' key. It may seem like a kludge, but it has the added benefit that your comments can be parsed and manipulated easily.
    – jme
    Apr 2 '15 at 14:29














  • 1




    You might look at YAML (I'm not sure at a glance whether it supports your requirements). Alternatively, you can simply include comments in your JSON under a 'comments' key. It may seem like a kludge, but it has the added benefit that your comments can be parsed and manipulated easily.
    – jme
    Apr 2 '15 at 14:29








1




1




You might look at YAML (I'm not sure at a glance whether it supports your requirements). Alternatively, you can simply include comments in your JSON under a 'comments' key. It may seem like a kludge, but it has the added benefit that your comments can be parsed and manipulated easily.
– jme
Apr 2 '15 at 14:29




You might look at YAML (I'm not sure at a glance whether it supports your requirements). Alternatively, you can simply include comments in your JSON under a 'comments' key. It may seem like a kludge, but it has the added benefit that your comments can be parsed and manipulated easily.
– jme
Apr 2 '15 at 14:29












3 Answers
3






active

oldest

votes


















2














As Chazeon and jme indicate you should take a look at YAML. It supports nested data structures ( list (sequence in YAML), dict (mapping) and various primitives ( integers, float, string, date ).



YAML also supports end-of-line comments ( introduced with #) but the "standard" PyYAML parser mentioned by Chazeon throws these away while reading data in (and cannot write these).



The package ruamel.yaml (of which I am the author) that derives from PyYAML, preserves the comments when doing a round trip (YAML file to python datastructures to YAML file). It also keeps most of the YAML formatting (block vs. one-line for lists and dictionaries).



Indentation however is "standardised" so all block mappings and sequences look the same after the first roundtrip. Indentation defaults to 2 spaces for both, but these can be set separately, as well as that the dash can be "pushed in" within the space before the sequence element using e.g. yaml.indent(mapping=3, sequence=5, offset=2)






share|improve this answer























  • Very good, thanx for sharing.
    – Frida
    Apr 2 '15 at 21:15



















1














Have you considered YAML? It can have comments start with # and some parsers like PyYAML already exsists.



As far as I know, some relatively large programs like MongoDB is using YAML as configuration file, and some programs use YAML to store metadata in front of a file, for example, Pandoc support yaml in front of Markdown and .



For further information you can visit the The Official YAML Website which presented some good examples and a bunch information or the Wikipedia page.






share|improve this answer





























    1














    For me (Just my opinion)



    I will use JSON since it is small, easy to read and compatible with almost any language and platform.
    For comment, I will use some keyword e.g. "Comment" and assign it as a JSON tag. JSON will ignore unused tags anyway.



    Example of JSON



    {
    "param1" : "value1",
    "param2" : "value2",
    }


    Example of JSON with my comment



    {
    "param1" : "value1",
    "param2" : "value2",
    "Comment" : "My Comment."
    }


    By the way, from my experience, we can't rely on customer to edit any formatted data JSON, XML, ini or even YAML. They will brake somethings.






    share|improve this answer























      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%2f29415326%2fconfiguration-file-format-complex-datastructures-list-dictionaries-comments%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      As Chazeon and jme indicate you should take a look at YAML. It supports nested data structures ( list (sequence in YAML), dict (mapping) and various primitives ( integers, float, string, date ).



      YAML also supports end-of-line comments ( introduced with #) but the "standard" PyYAML parser mentioned by Chazeon throws these away while reading data in (and cannot write these).



      The package ruamel.yaml (of which I am the author) that derives from PyYAML, preserves the comments when doing a round trip (YAML file to python datastructures to YAML file). It also keeps most of the YAML formatting (block vs. one-line for lists and dictionaries).



      Indentation however is "standardised" so all block mappings and sequences look the same after the first roundtrip. Indentation defaults to 2 spaces for both, but these can be set separately, as well as that the dash can be "pushed in" within the space before the sequence element using e.g. yaml.indent(mapping=3, sequence=5, offset=2)






      share|improve this answer























      • Very good, thanx for sharing.
        – Frida
        Apr 2 '15 at 21:15
















      2














      As Chazeon and jme indicate you should take a look at YAML. It supports nested data structures ( list (sequence in YAML), dict (mapping) and various primitives ( integers, float, string, date ).



      YAML also supports end-of-line comments ( introduced with #) but the "standard" PyYAML parser mentioned by Chazeon throws these away while reading data in (and cannot write these).



      The package ruamel.yaml (of which I am the author) that derives from PyYAML, preserves the comments when doing a round trip (YAML file to python datastructures to YAML file). It also keeps most of the YAML formatting (block vs. one-line for lists and dictionaries).



      Indentation however is "standardised" so all block mappings and sequences look the same after the first roundtrip. Indentation defaults to 2 spaces for both, but these can be set separately, as well as that the dash can be "pushed in" within the space before the sequence element using e.g. yaml.indent(mapping=3, sequence=5, offset=2)






      share|improve this answer























      • Very good, thanx for sharing.
        – Frida
        Apr 2 '15 at 21:15














      2












      2








      2






      As Chazeon and jme indicate you should take a look at YAML. It supports nested data structures ( list (sequence in YAML), dict (mapping) and various primitives ( integers, float, string, date ).



      YAML also supports end-of-line comments ( introduced with #) but the "standard" PyYAML parser mentioned by Chazeon throws these away while reading data in (and cannot write these).



      The package ruamel.yaml (of which I am the author) that derives from PyYAML, preserves the comments when doing a round trip (YAML file to python datastructures to YAML file). It also keeps most of the YAML formatting (block vs. one-line for lists and dictionaries).



      Indentation however is "standardised" so all block mappings and sequences look the same after the first roundtrip. Indentation defaults to 2 spaces for both, but these can be set separately, as well as that the dash can be "pushed in" within the space before the sequence element using e.g. yaml.indent(mapping=3, sequence=5, offset=2)






      share|improve this answer














      As Chazeon and jme indicate you should take a look at YAML. It supports nested data structures ( list (sequence in YAML), dict (mapping) and various primitives ( integers, float, string, date ).



      YAML also supports end-of-line comments ( introduced with #) but the "standard" PyYAML parser mentioned by Chazeon throws these away while reading data in (and cannot write these).



      The package ruamel.yaml (of which I am the author) that derives from PyYAML, preserves the comments when doing a round trip (YAML file to python datastructures to YAML file). It also keeps most of the YAML formatting (block vs. one-line for lists and dictionaries).



      Indentation however is "standardised" so all block mappings and sequences look the same after the first roundtrip. Indentation defaults to 2 spaces for both, but these can be set separately, as well as that the dash can be "pushed in" within the space before the sequence element using e.g. yaml.indent(mapping=3, sequence=5, offset=2)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 20 at 8:42

























      answered Apr 2 '15 at 14:42









      Anthon

      28.4k1693144




      28.4k1693144












      • Very good, thanx for sharing.
        – Frida
        Apr 2 '15 at 21:15


















      • Very good, thanx for sharing.
        – Frida
        Apr 2 '15 at 21:15
















      Very good, thanx for sharing.
      – Frida
      Apr 2 '15 at 21:15




      Very good, thanx for sharing.
      – Frida
      Apr 2 '15 at 21:15













      1














      Have you considered YAML? It can have comments start with # and some parsers like PyYAML already exsists.



      As far as I know, some relatively large programs like MongoDB is using YAML as configuration file, and some programs use YAML to store metadata in front of a file, for example, Pandoc support yaml in front of Markdown and .



      For further information you can visit the The Official YAML Website which presented some good examples and a bunch information or the Wikipedia page.






      share|improve this answer


























        1














        Have you considered YAML? It can have comments start with # and some parsers like PyYAML already exsists.



        As far as I know, some relatively large programs like MongoDB is using YAML as configuration file, and some programs use YAML to store metadata in front of a file, for example, Pandoc support yaml in front of Markdown and .



        For further information you can visit the The Official YAML Website which presented some good examples and a bunch information or the Wikipedia page.






        share|improve this answer
























          1












          1








          1






          Have you considered YAML? It can have comments start with # and some parsers like PyYAML already exsists.



          As far as I know, some relatively large programs like MongoDB is using YAML as configuration file, and some programs use YAML to store metadata in front of a file, for example, Pandoc support yaml in front of Markdown and .



          For further information you can visit the The Official YAML Website which presented some good examples and a bunch information or the Wikipedia page.






          share|improve this answer












          Have you considered YAML? It can have comments start with # and some parsers like PyYAML already exsists.



          As far as I know, some relatively large programs like MongoDB is using YAML as configuration file, and some programs use YAML to store metadata in front of a file, for example, Pandoc support yaml in front of Markdown and .



          For further information you can visit the The Official YAML Website which presented some good examples and a bunch information or the Wikipedia page.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 2 '15 at 14:31









          Chazeon

          47729




          47729























              1














              For me (Just my opinion)



              I will use JSON since it is small, easy to read and compatible with almost any language and platform.
              For comment, I will use some keyword e.g. "Comment" and assign it as a JSON tag. JSON will ignore unused tags anyway.



              Example of JSON



              {
              "param1" : "value1",
              "param2" : "value2",
              }


              Example of JSON with my comment



              {
              "param1" : "value1",
              "param2" : "value2",
              "Comment" : "My Comment."
              }


              By the way, from my experience, we can't rely on customer to edit any formatted data JSON, XML, ini or even YAML. They will brake somethings.






              share|improve this answer




























                1














                For me (Just my opinion)



                I will use JSON since it is small, easy to read and compatible with almost any language and platform.
                For comment, I will use some keyword e.g. "Comment" and assign it as a JSON tag. JSON will ignore unused tags anyway.



                Example of JSON



                {
                "param1" : "value1",
                "param2" : "value2",
                }


                Example of JSON with my comment



                {
                "param1" : "value1",
                "param2" : "value2",
                "Comment" : "My Comment."
                }


                By the way, from my experience, we can't rely on customer to edit any formatted data JSON, XML, ini or even YAML. They will brake somethings.






                share|improve this answer


























                  1












                  1








                  1






                  For me (Just my opinion)



                  I will use JSON since it is small, easy to read and compatible with almost any language and platform.
                  For comment, I will use some keyword e.g. "Comment" and assign it as a JSON tag. JSON will ignore unused tags anyway.



                  Example of JSON



                  {
                  "param1" : "value1",
                  "param2" : "value2",
                  }


                  Example of JSON with my comment



                  {
                  "param1" : "value1",
                  "param2" : "value2",
                  "Comment" : "My Comment."
                  }


                  By the way, from my experience, we can't rely on customer to edit any formatted data JSON, XML, ini or even YAML. They will brake somethings.






                  share|improve this answer














                  For me (Just my opinion)



                  I will use JSON since it is small, easy to read and compatible with almost any language and platform.
                  For comment, I will use some keyword e.g. "Comment" and assign it as a JSON tag. JSON will ignore unused tags anyway.



                  Example of JSON



                  {
                  "param1" : "value1",
                  "param2" : "value2",
                  }


                  Example of JSON with my comment



                  {
                  "param1" : "value1",
                  "param2" : "value2",
                  "Comment" : "My Comment."
                  }


                  By the way, from my experience, we can't rely on customer to edit any formatted data JSON, XML, ini or even YAML. They will brake somethings.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 2 '15 at 20:35









                  Frida

                  204




                  204










                  answered Apr 2 '15 at 14:38









                  Chetchaiyan

                  1418




                  1418






























                      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%2f29415326%2fconfiguration-file-format-complex-datastructures-list-dictionaries-comments%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

                      Create new schema in PostgreSQL using DBeaver

                      Deepest pit of an array with Javascript: test on Codility

                      Costa Masnaga