0

I've created a python script that reads from a config.py file using import config to inform it's output.

config.py:

current_brightness = 0

script.py:

import config

set_brightness(config.current_brightness + 1)

I need current_brightness to be permanently incremented in config.py after running the script, so that subsequent script runs will import the newly updated value instead of it always being 0.

In other words, how do I write to a config.py file like I would any other standard config format like config.ini?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Michael Moreno
  • 947
  • 1
  • 7
  • 24
  • 1
    instead of config.py, you should use config.json. Then you could easily load, change and rewrite the file without a ton of headaches. You are basically trying to rewrite a python file while you are using it. That's a bad design, and the amount of work that would be necessary to achieve it will essentially equate to building your own parser. – OneMadGypsy Dec 28 '21 at 16:40
  • 1
    Why not just use `config.ini`? – demberto Dec 28 '21 at 16:41
  • To do that, you will need to literally change the contents of the file. If you do `config.brightness = 1` it will persist only for the run of the current program as it just changes the namespace. But the next you run the code, it will be back to the value that exists in the file. You could rewrite the py file dynamically but as others said, why not just use a `config.ini` and read/write it with a dedicated module like [`configparser`](https://docs.python.org/3/library/configparser.html)... – Tomerikoo Dec 28 '21 at 17:11

2 Answers2

0

You will need to persist the data somewhere on the hard disk. Two ways to go about it would be

  1. database
  2. file

database would be an overkill for storing just a number.

store the number in a file, programmatically read it using open() , programmatically update the value and write back the value into the file

Checkout this for more info on file I/O

D.B.K
  • 410
  • 2
  • 15
  • I was trying to avoid having to have a third file just for tracking state, and instead just have all of it in `config.py`. Are you suggesting that I don't do this approach, and keep `current_brightness` in it's own text file? – Michael Moreno Dec 28 '21 at 16:43
  • you can just use `config.ini` as well. – D.B.K Dec 28 '21 at 16:45
-1

My suggestion is to use classAttributes While running the program, you can define the settings you need as an attribute and use them

config.py

class Config:
current_brightness = 0 # define config attribute

and for use in script.py

Config.current_brightness +=1
# ....
print(Config.current_brightness) # return new value
  • 1
    But this won't be persistent right? The class will be reinitialized every script run. – Michael Moreno Dec 28 '21 at 16:52
  • @MichaelMoreno, yes, this answer should be downvoted – FLAK-ZOSO Dec 28 '21 at 19:41
  • Before you vote no - note that I only answered the question of how to maintain the status of a setting in the program Use the database to get the initial settings if the settings are dynamic and the dictionary inside a file if the settings are fixed. – Amirreza Madanifard Jan 15 '22 at 09:47