Here's an updated version of your code that includes the requested print statements:
import pytz
import random
from datetime import datetime, timedelta
import time
# Define Chicago timezone
chicago_tz = pytz.timezone('America/Chicago')
def get_current_chicago_time():
return datetime.now(chicago_tz)
def is_before_4pm():
now = get_current_chicago_time()
four_pm = now.replace(hour=16, minute=0, second=0, microsecond=0)
return now < four_pm
# Start randomly with True or False
current_value = random.choice([True, False])
while is_before_4pm():
previous_value = current_value
value_str = str(current_value).lower()
# Update both systems
caribou.update('parametervariables', 'SPX.IMCGlobal', {'variables': {'AsymmetricQuotingVal': value_str}})
haya_caribou.update('parametervariables', 'SPX.IMCGlobal', {'variables': {'AsymmetricQuotingVal': value_str}})
# 70% chance to toggle
if random.random() < 0.7:
current_value = not current_value
# Print the change
print(f"Toggled from {str(previous_value).lower()} to {str(current_value).lower()}")
time.sleep(15 * 60)
This code introduces a previous_value variable to keep track of the value before it's potentially toggled. It then prints out the change after the update.
However, if you want to make the code more readable and maintainable, you could consider using a more descriptive variable name instead of previous_value, and also consider using an f-string to make the print statement more readable.
Here's an updated version:
import pytz
import random
from datetime import datetime, timedelta
import time
# Define Chicago timezone
chicago_tz = pytz.timezone('America/Chicago')
def get_current_chicago_time():
return datetime.now(chicago_tz)
def is_before_4pm():
now = get_current_chicago_time()
four_pm = now.replace(hour=16, minute=0, second=0, microsecond=0)
return now < four_pm
# Start randomly with True or False
current_value = random.choice([True, False])
while is_before_4pm():
original_state = current_value
value_str = str(current_value).lower()
# Update both systems
caribou.update('parametervariables', 'SPX.IMCGlobal', {'variables': {'AsymmetricQuotingVal': value_str}})
haya_caribou.update('parametervariables', 'SPX.IMCGlobal', {'variables': {'AsymmetricQuotingVal': value_str}})
# 70% chance to toggle
if random.random() < 0.7:
current_value = not current_value
# Print the change
print(f"AsymmetricQuotingVal toggled from {str(original_state).lower()} to {str(current_value).lower()} at {get_current_chicago_time()}")
time.sleep(15 * 60)
This version includes the current time in the print statement, which can be helpful for debugging purposes.