Git Product home page Git Product logo

birthday-wisher's Introduction

birthday-wisher

Problem: Email a Birthday person on his/her Birthday

Solutions

  1. Update the birthdays.csv => Go into the birthdays.csv and edit it.
  2. Check if today matches a birthday in the birthdays.csv
# Use the datetime module to obtain the current day of the week
now = dt.datetime.now()
today = (now.month, now.day)

df = pd.read_csv("birthdays.csv")
# Use dictionary comprehension to create a dictionary from birthday.csv
birthdays_dict = {(df_row.month, df_row.day): df_row for (index, df_row) in df.iterrows()}
  1. If step 2 is true, pick a random letter from letter templates folder
if today in birthdays_dict:
    file_path = f"letter_templates/letter_{random.randint(1,3)}.txt"
    birthday_person = birthdays_dict[today]["name"]
    birthday_email = birthdays_dict[today]["email"]

    with open(file_path) as letter:
        contents = letter.read()
        contents_with_name = contents.replace("[NAME]", birthday_person)
  1. Send the letter generated in step 3 to that person's email address
# ***WARNING***:
# Make sure you use a dummy email account to test this out!
my_email = "YOUR_EMAIL"
password = "YOUR_EMAIL_PASSWORD"

with smtplib.SMTP("YOUR_EMAIL_PROVIDERS_SMTP_ADDRESS", port=587) as connection:
    connection.starttls()
    connection.login(user=my_email, password=password)
    # Write messages
    connection.sendmail(
        from_addr=my_email,
        to_addrs=birthday_email,
        msg=f"Subject:Happy Birthday, {birthday_person}!\n\n{contents_with_name}"
    )

Lessons

  1. Review the dictionary comprehension:
  • From a list: new_dict = {new_key:new_val for itme in list if test}
  • From a dictionary: new_dict = {new_key:new_val for (key, val) in dict.items() if test}
  1. I can use PythonAnywhere to run a Python script automatically (also set a specific time to run it).

birthday-wisher's People

Watchers

Billy Jun Jo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.