
I’ve used CoPilot twice this month to write programs. One is a python Flask program that ping a set of terminals, and display on a webpage red for down, green for up, with the date it was first seen down. I had to construct the sentences in detail, but could ask it to amend the code to improve it. I reached a problem that the code became too large, but up until that point I could ask it for the whole thing each time. Very easy and definitely quicker than me remembering and stumbling (my usual way when I’m not programming regularly.)
The second time a much smaller program which involved parsing the postfix maillog and printing out date, from, to, subject of mail. Much smaller program but honestly, working out the regular expressions would have taken me a bit of time myself. So much quicker. It did get some things wrong, but they were easy to spot.
#!/usr/bin/env python3
import re
import json
from datetime import datetime
# Path to the maillog file
log_file_path = "/var/log/maillog"
# Regular expression pattern to match the required lines and extract data
log_pattern = r"(\w{3}\s\d{1,2}\s[\d:]+)\s[\w.-]+\spostfix/cleanup\[\d+\]:\s\w+: warning: header Subject: (.*?); *?from=<([\w.@]+)> to=<([\w.@]+)>"
# Output list to store extracted data
output_data = []
# Open the log file and process lines
with open(log_file_path, "r") as file:
for line in file:
match = re.search(log_pattern, line)
if match:
date_str, subject, sender, recipient = match.groups()
# Convert date to Y-m-d format
date = datetime.strptime(date_str, "%b %d %H:%M:%S").strftime("%m-%d %H:%M")
# Append extracted data to the output list as a dictionary
output_data.append({
"date": date,
"from": sender,
"to": recipient,
"subject": subject
})
for line in output_data:
print ( f"{line['date']}\t{line['from']}\t{line['to']}\t\'{line['subject']}\'")
# Convert the output data to JSON format
json_data = json.dumps(output_data, indent=4)
# Optional: Save the JSON data to a file
with open("snoop-dog.json", "w") as json_file:
json_file.write(json_data)