Learning python with OpenAI's ChatGPT

Rob

Administrator
Staff member
Joined
Oct 27, 2011
Messages
1,222
Reaction score
2,276
Credits
3,574
ChatGPT can be super helpful in helping you to create or modify code. For a fun exercise I recently asked it to write a "dead man's switch" style script in python. Specifically to check if there have been any new files written to a specified directory within the last 7 days. If there was nothing newer than 7 days then send an email using the sendgrid API.

Here's the full conversation - it's pretty interesting!

###########################

Me: Write me a script that can be used as a dead man's switch that will make me do something until I don't. Have it check for any file in /path/to/file/ directory. If there is not a file that is less than 7 days old then it will use sendgrid api to send an email to [email protected].

ChatGPT: Here's a script in Python that will check for files in a specific directory and send an email through the SendGrid API if no new file has been added within the past 7 days:

Code:
import os
import datetime
import sendgrid
from sendgrid.helpers.mail import Email, Content, Mail

path = "/path/to/file/"
now = datetime.datetime.now()
no_new_file = True
for filename in os.listdir(path):
    file_path = os.path.join(path, filename)
    if os.path.isfile(file_path):
        file_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
        if (now - file_time).days <= 7:
            no_new_file = False
            break

if no_new_file:
    sg = sendgrid.SendGridAPIClient(api_key=os.environ.get("SENDGRID_API_KEY"))
    from_email = Email("[email protected]")
    to_email = Email("[email protected]")
    subject = "No new file in the past week"
    content = Content("text/plain", "This is a reminder that no new file has been added to the specified directory in the past week.")
    mail = Mail(from_email, subject, to_email, content)
    sg.client.mail.send.post(request_body=mail.get())

Me: Great - please add code comments to that finished script

ChatGPT: Sure! Here's the same script with added comments:

Code:
import os
import datetime
import sendgrid
from sendgrid.helpers.mail import Email, Content, Mail

# Define the path to the directory to be checked
path = "/path/to/file/"

# Get the current time
now = datetime.datetime.now()

# Assume no new file is present until proven otherwise
no_new_file = True

# Loop through the files in the directory
for filename in os.listdir(path):
    file_path = os.path.join(path, filename)
    # Check if the current item is a file
    if os.path.isfile(file_path):
        # Get the modification time of the file
        file_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
        # Check if the file was modified within the past 7 days
        if (now - file_time).days <= 7:
            # If a file was modified within the past 7 days, set no_new_file to False
            no_new_file = False
            # Stop looping through the files
            break

# If no new file was found in the directory
if no_new_file:
    # Create a SendGrid client object
    sg = sendgrid.SendGridAPIClient(api_key=os.environ.get("SENDGRID_API_KEY"))

    # Define the from email address
    from_email = Email("[email protected]")

    # Define the to email address
    to_email = Email("[email protected]")

    # Define the subject of the email
    subject = "No new file in the past week"

    # Define the content of the email
    content = Content("text/plain", "This is a reminder that no new file has been added to the specified directory in the past week.")

    # Create a Mail object and send the email
    mail = Mail(from_email, subject, to_email, content)
    sg.client.mail.send.post(request_body=mail.get())

Me: Will this work with python3?

ChatGPT: Yes, this script should work with Python 3. The sendgrid library supports both Python 2 and 3, and the core Python libraries used in the script (os, datetime) are compatible with both versions as well.

Me: If i had this script in ~/synner/scripts/ how would you call this in cron to run every night at midnight?

ChatGPT: To run the script every night at midnight using cron, you can add the following line to your crontab file:

0 0 * * * /usr/bin/python3 ~/synner/scripts/dead_man_switch.py

This line specifies that the script should be executed every day at midnight (the first 0 represents the minute, and the second 0 represents the hour). The full path to the python3 executable is used to ensure that the correct version of Python is used, and the full path to the script dead_man_switch.py is specified so that cron knows exactly which script to run.

To edit your crontab file, run the following command in your terminal:

crontab -e

This will open the crontab file for editing, and you can add the line above to the file and save it.

Note: The exact location of the python3 executable and the path to the script may be different on your system. Please make sure to use the correct paths for your setup.

#################################

This thing is pretty crazy. If you haven't tried it yet, go check it out here: https://chat.openai.com/chat

If you have played with it, what are you using it for?
 
Last edited:


This has the potential to be world changing, but it'll probably mostly get used for useless stuff.
 
"Chatgpt write me a blurb for my fivver selling page"

It'll eventually boil down to memes and cat pictures.

Or erotic fan fiction...

Or a way to avoid getting caught for plagiarism...

Probably all of the above - but mostly the cat pictures.
 
Yeah i read about that AI and it is quite fascinating, it's the most advanced attempt to convert coding language stuff into english. The lady who invented compilers did the same but I couldn't find any details on how that worked because, lol, those were some freakin' old military computers.
 
Learning Python? This looks more like avoiding learning Python, by getting AI to write code for you?! Ha ha!
Yeah that's kinda the catch, it's just funny watching how much people avoid learning to talk to computers but we are basically constantly talking to computers now adays. Says something pretty dramatic about human nature, but not sure what...
 
I love tech, made lives easier from the first match to washing machines. Now with the new AI all I can think of is "War Games" or the "Terminator" even the "Matrix"

Isaac Asimov's "I Robot" the code of doing no harm to humans, may not be a reality. Seem to remember the big "G" motto was "Don't be evil" They don't use that anymore.

It is not the old way of program it to do or not, it is AI wanting to be more efficient and writing its own more efficient eventually could mean we are in the way. It can help in curing disease, or causing it.
 
What are the things that ChatGPT can't do? Based on the OP, it soesn't look like it can replace python. Are they trying to make it well versed in other languages too?
 

Staff online


Top