Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Configuration settings library

    Scheduled Pinned Locked Moved Discussion
    lopycurrent
    7 Posts 2 Posters 1.6k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      misterlisty
      last edited by

      Is there a library or method to store settings(preferably in json) that survives reboots.

      1 Reply Last reply Reply Quote 0
      • R Offline
        Ralph
        last edited by

        Just use the json lib and open/write it manually. Something like this

        import json
        
        class Config:
        
            def __init__(self,filename="config.json"):
                self.filename = filename
                self.data = self.__read_file()
        
            def __read_file(self):
                with open(self.filename) as data:
                    return json.load(data)
        
            def set(self,key,value):
                self.data[key] = value
        
            def get(self,key):
                return self.data[key]
            
            def save(self):
                with open(self.filename,'w') as f:
                    f.write(json.dumps(self.data))
        
        1 Reply Last reply Reply Quote 1
        • M Offline
          misterlisty
          last edited by

          from config import Config

          try:
          Config.set(key="here",value="there") #failes with function missing required positional argument #0
          Config.save()
          devname = Config.get("here")
          print("devname",devname)
          except Exception as e:
          print("config testfailed",e)

          R 1 Reply Last reply Reply Quote 0
          • R Offline
            Ralph @misterlisty
            last edited by Ralph

            @misterlisty You need to make an object instead of using it like a method, and not use keyed params. Example:

            from config import Config
            c = Config()
            c.get("existing_key")
            c.set("existing_key","whatever_value")
            c.save()

            1 Reply Last reply Reply Quote 0
            • M Offline
              misterlisty
              last edited by

              i get an error... syntax error in JSON

              R 1 Reply Last reply Reply Quote 0
              • R Offline
                Ralph @misterlisty
                last edited by

                @misterlisty I'm not sure what you want me to say, other than the fact that you probably have a syntax error in your JSON file

                1 Reply Last reply Reply Quote 0
                • M Offline
                  misterlisty
                  last edited by

                  Haven't tried yet, but assume the file has to have an initial json string first?

                  1 Reply Last reply Reply Quote 0

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better 💗

                  Register Login
                  • 1 / 1
                  • First post
                    Last post

                  Pycom on Twitter

                  Pycom