Python Forum
Method works as expected on host machine but not on server
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Method works as expected on host machine but not on server
#5
(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!
Reply


Messages In This Thread
RE: Method works as expected on host machine but not on server - by DeaD_EyE - Jan-05-2023, 10:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Custom method to handle exceptions not working as expected gradlon93 3 1,083 Dec-22-2022, 07:12 PM
Last Post: deanhystad
  where to host my python script tomtom 1 1,289 Feb-09-2022, 06:45 AM
Last Post: ndc85430
  Failing to connect to a host with WMI tester_V 6 4,477 Aug-10-2021, 06:25 PM
Last Post: tester_V
  How to take the tar backup files form remote server to local server sivareddy 0 1,939 Jul-14-2021, 01:32 PM
Last Post: sivareddy
Question Google Foobar- Code works in my IDE but not in foobar. Static method? pr3ttykitty 4 5,001 Feb-24-2021, 05:03 PM
Last Post: nilamo
  Fastest Method for Querying SQL Server with Python Pandas BuJayBelvin 7 7,023 Aug-02-2020, 06:21 PM
Last Post: jefsummers
  results for sql server query not as expected kat35601 4 3,174 Sep-13-2018, 08:02 PM
Last Post: kat35601
  Get host from ip antmar904 3 2,955 Jan-31-2018, 08:27 PM
Last Post: antmar904
  Host and port problems for UDP chat server YevesDraco 2 3,836 May-07-2017, 04:35 PM
Last Post: YevesDraco

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020