in python I want to have a file that stores for each website 3 properities: utc timestamp when the website was last accessed and the url of the website and the int. website and int are const, the timestamp would be updated after each succesful run of the corresponding function (separate functions for each website)
I want the program to read from file at the start
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import json
import datetime
from pathlib import Path
class WebsiteTracker:
def __init__(self, data_file="data.json"):
self.data_file = Path(data_file)
self.websites = self.load_data()
def load_data(self):
"""Load website data from JSON file"""
if self.data_file.exists():
with open(self.data_file, 'r') as f:
return json.load(f)
else:
# Create empty file if it doesn't exist
return {}
def save_data(self):
"""Save website data to JSON file"""
with open(self.data_file, 'w') as f:
json.dump(self.websites, f, indent=2)
def update_timestamp(self, website_name):
"""Update the last accessed timestamp for a website"""
if website_name in self.websites:
self.websites[website_name]["last_accessed"] = datetime.datetime.utcnow().isoformat()
self.save_data()
else:
print(f"Website {website_name} not found")
def get_website_info(self, website_name):
"""Get all info for a specific website"""
return self.websites.get(website_name)
# Initialize tracker
tracker = WebsiteTracker()
# Example functions for each website
def process_website1():
"""Function for website1"""
website_info = tracker.get_website_info("website1")
if website_info:
url = website_info["url"]
int_val = website_info["int_value"]
# Your processing logic here
print(f"Processing {url} with value {int_val}")
# Update timestamp on successful completion
tracker.update_timestamp("website1")
return True
return False
def process_website2():
"""Function for website2"""
website_info = tracker.get_website_info("website2")
if website_info:
url = website_info["url"]
int_val = website_info["int_value"]
# Your processing logic here
print(f"Processing {url} with value {int_val}")
# Update timestamp on successful completion
tracker.update_timestamp("website2")
return True
return False
# Example usage
if __name__ == "__main__":
# Process websites
process_website1()
process_website2()
# View current data
print(json.dumps(tracker.websites, indent=2))
This approach gives you:
JSON storage that's human-readable and easy to modify
Automatic file creation if it doesn't exist
UTC timestamps in ISO format
Separate functions for each website
Easy access to all website properties
Automatic saving after timestamp updates
The timestamp will be stored as an ISO format string (e.g., "2025-06-03T10:30:45.123456") which you can easily convert back to a datetime object if needed using datetime.datetime.fromisoformat().
But this creates a problem: how do you know which array index corresponds to which website function? You'd need to hardcode indices, which is fragile.
The object approach I showed is actually simpler because:
You can directly access websites by name: websites["website1"]
No need to search through arrays or remember indices
Easy to add/remove websites without breaking existing code
Clear mapping between website names and their functions
If you really prefer the array approach, here's how it would work:
python
# Assuming website1 is always index 0, website2 is index 1, etc.
def process_website1():
website_info = tracker.websites[0] # Fragile - depends on order
# ... rest of function
tracker.update_timestamp(0)
def process_website2():
website_info = tracker.websites[1] # What if you reorder the JSON?
# ... rest of function
tracker.update_timestamp(1)
The named object approach is more maintainable and less error-prone.