(Jan-04-2023, 03:44 PM)gradlon93 Wrote: Description of the error: when updating current_color from within the while loop, I forgot to PARSE again the .ini file, so it didn't update but stayed always the same.
I didn't know that you also want to detect changes.
The
configparser
object does not have a mechanism to automatically detect changes and reading the file again.I guess it's better if you put the whole
configparser
logic into one function.I introduced some changes and modernized the code.
import time from configparser import ConfigParser from datetime import datetime as DateTime from pathlib import Path RED = "RED" TIMECOUNT = Path("timecount.html") CONFIG = Path("traffic_light.ini") def get_sign_color(file=CONFIG): """ Return SIGN_COLOR from configuration By default, the Path CONFIG is used. """ constants = ConfigParser() constants.read(file) return constants.get("CONSTANTS", "SIGN_COLOR") def write_html(counter): with TIMECOUNT.open("a") as html_output: now = DateTime.now() today = now.date() hhmmss = now.time().replace(microsecond=0) output = f'#"{counter:08d}" | {today} | <b>{hhmmss}</b>\n' #"00000010" | 2023-01-05 | <b>11:24:28</b>\n html_output.write(output) time.sleep(1) def main(): counter = 1 while get_sign_color() != RED: write_html(counter) counter += 1 with TIMECOUNT.open("a") as html_output: html_output.write( '<p><font color="red">Ending cycle from signal "RED".</font></p>\n' ) if __name__ == "__main__": main()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!