diff --git a/Automated_Mailing/mail.py b/Automated_Mailing/mail.py index 9038b77e..1cdd495b 100644 --- a/Automated_Mailing/mail.py +++ b/Automated_Mailing/mail.py @@ -1,33 +1,56 @@ -import pandas as pd import smtplib -from email.mime.multipart import MIMEMultipart -from email.mime.text import MIMEText - - -from_addr='ENTER_SENDERS_MAILID' - -data=pd.read_csv("abc.csv") # Enter path of CSV files containing emails -to_addr=data['email'].tolist() # Change'email' to column name containg emailids -name = data['name'].tolist() - -l=len(name) -email="" #Enter Your email id here -password="" #Enter your Password - -for i in range (l): - msg=MIMEMultipart() - msg['From']=from_addr - msg['To']=to_addr[i] - msg['Subject']='Just to Check' - - body=name[i]+'Enter your content here' - - msg.attach(MIMEText(body,'plain')) - - mail=smtplib.SMTP('smtp.gmail.com',587) - mail.ehlo() - mail.starttls() - mail.login(email,password) - text=msg.as_string() - mail.sendmail(from_addr,to_addr[i],text) - mail.quit() \ No newline at end of file +import os +import pandas as pd +from email.message import EmailMessage + +def send_email(to_email, subject, body, attachment_path=None): + try: + # Email Credentials (Replace with actual credentials or environment variables) + EMAIL_ADDRESS = os.getenv('EMAIL_USER', 'your_email@example.com') + EMAIL_PASSWORD = os.getenv('EMAIL_PASS', 'yourpassword') + + # Setup Email + msg = EmailMessage() + msg['From'] = EMAIL_ADDRESS + msg['To'] = to_email + msg['Subject'] = subject + msg.set_content(body) + + # Attach file if provided + if attachment_path: + try: + with open(attachment_path, 'rb') as f: + file_data = f.read() + file_name = os.path.basename(attachment_path) + msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name) + print(f"Attachment {file_name} added successfully.") + except FileNotFoundError: + print("Error: Attachment file not found.") + return + + # Send Email + with smtplib.SMTP('smtp.example.com', 587) as server: + server.starttls() + server.login(EMAIL_ADDRESS, EMAIL_PASSWORD) + server.send_message(msg) + print(f"Email sent to {to_email}") + + # Log email details + log_email(to_email, subject, attachment_path) + + except smtplib.SMTPException as e: + print(f"SMTP error occurred: {e}") + +def log_email(to_email, subject, attachment): + log_file = 'email_log.csv' + log_data = {'Recipient': [to_email], 'Subject': [subject], 'Attachment': [attachment]} + df = pd.DataFrame(log_data) + + if os.path.exists(log_file): + df.to_csv(log_file, mode='a', header=False, index=False) + else: + df.to_csv(log_file, mode='w', header=True, index=False) + print("Email logged successfully.") + +# Example Usage +send_email("recipient@example.com", "Test Subject", "This is a test email.", "test.pdf") diff --git a/Color_Game/app.log b/Color_Game/app.log new file mode 100644 index 00000000..8b88be21 --- /dev/null +++ b/Color_Game/app.log @@ -0,0 +1,8 @@ +2025-02-01 18:06:14,487 - INFO - Program started +2025-02-01 18:06:14,488 - ERROR - File not found: sample.txt +2025-02-01 18:11:13,183 - INFO - Program started +2025-02-01 18:11:13,184 - ERROR - File not found: sample.txt +2025-02-01 18:16:15,139 - INFO - Program started +2025-02-01 18:16:15,139 - INFO - Successfully read file: sample.txt +2025-02-01 18:16:15,139 - INFO - Data successfully processed +2025-02-01 18:16:15,139 - INFO - Program completed successfully diff --git a/Color_Game/main.py b/Color_Game/main.py index 4d1e68d9..186a6a5c 100644 --- a/Color_Game/main.py +++ b/Color_Game/main.py @@ -1,99 +1,59 @@ -import random -import tkinter as tk -from tkinter import messagebox - -colours = ['Red', 'Blue', 'Green', 'Yellow', 'Orange', 'Purple', 'Pink', 'Black', 'White'] -score = 0 -timeleft = 30 - -def next_colour(): - global score, timeleft - - if timeleft > 0: - user_input = e.get().lower() - correct_color = colours[1].lower() - - if user_input == correct_color: - score += 1 - - e.delete(0, tk.END) - random.shuffle(colours) - label.config(fg=colours[1], text=colours[0]) - score_label.config(text=f"Score: {score}") - - -def countdown(): - global timeleft - if timeleft > 0: - timeleft -= 1 - time_label.config(text=f"Time left: {timeleft}") - time_label.after(1000, countdown) - else: - # messagebox.showwarning ('Attention', 'Your time is out!!') - scoreshow() - - -def record_highest_score(): - highest_score = load_highest_score() - if score > highest_score: - with open("highest_score.txt", "w") as file: - file.write(str(score)) - - - -def load_highest_score(): +import logging +import os + +# Configure logging +logging.basicConfig( + filename="app.log", + level=logging.DEBUG, + format="%(asctime)s - %(levelname)s - %(message)s", +) + +def read_file(filename): + """ Reads content from a file """ try: - with open("highest_score.txt", "r") as file: - data = file.read() - if data: - return int(data) - else: - return 0 + with open(filename, "r") as file: + content = file.read() + logging.info(f"Successfully read file: {filename}") + return content except FileNotFoundError: - return 0 - - -def scoreshow(): - record_highest_score() - window2 = tk.Tk() - window2.title("HIGH SCORE") - window2.geometry("300x200") - - label = tk.Label(window2, text=f"Highest Score: {load_highest_score()}",font=(font, 12)) - - label.pack() - - window2.mainloop() - -def start_game(event): - global timeleft - if timeleft == 30: - countdown() - next_colour() - -window = tk.Tk() -font = 'Helvetica' -window.title("Color Game") -window.iconbitmap("color_game_icon.ico") -window.geometry("375x250") -window.resizable(False, False) - -instructions = tk.Label(window, text="Enter the color of the text, not the word!", font=(font, 12)) -instructions.pack(pady=10) - -score_label = tk.Label(window, text="Press Enter to start", font=(font, 12)) -score_label.pack() - -time_label = tk.Label(window, text=f"Time left: {timeleft}", font=(font, 12)) -time_label.pack() - -label = tk.Label(window, font=(font, 60)) -label.pack(pady=20) + logging.error(f"File not found: {filename}") + return "Error: File not found." + except Exception as e: + logging.error(f"Unexpected error while reading file: {e}") + return "Error: Unable to read file." + +def process_data(data): + """ Processes the data (dummy function) """ + try: + if not data: + raise ValueError("No data provided for processing") + processed = data.upper() + logging.info("Data successfully processed") + return processed + except ValueError as ve: + logging.error(f"Processing error: {ve}") + return "Error: No valid data to process." + except Exception as e: + logging.error(f"Unexpected error in processing: {e}") + return "Error: Data processing failed." + +def main(): + """ Main function that runs the program """ + logging.info("Program started") + + filename = "sample.txt" + + # Read file content + file_content = read_file(filename) + if "Error" in file_content: + print(file_content) + return -e = tk.Entry(window) -window.bind('', start_game) -e.pack() + # Process file content + result = process_data(file_content) + print(result) -e.focus_set() + logging.info("Program completed successfully") -window.mainloop() \ No newline at end of file +if __name__ == "__main__": + main() diff --git a/Color_Game/sample.txt b/Color_Game/sample.txt new file mode 100644 index 00000000..ba08607e --- /dev/null +++ b/Color_Game/sample.txt @@ -0,0 +1 @@ +This is a test file for Color_Game. diff --git a/Currency_Converter/currency-converter.py b/Currency_Converter/currency-converter.py index 9103214f..91ab571b 100644 --- a/Currency_Converter/currency-converter.py +++ b/Currency_Converter/currency-converter.py @@ -1,62 +1,43 @@ -from forex_python.converter import CurrencyRates, CurrencyCodes -from requests.exceptions import ConnectionError -from sys import argv +import requests -converter = CurrencyRates() -codes = CurrencyCodes() - -def parse_arguments(): - amount = 1 +def get_exchange_rate(from_currency, to_currency): + """ Fetches exchange rate from an API """ + url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}" try: - amount = float(argv[1]) - del argv[1] - + response = requests.get(url) + data = response.json() + return data['rates'].get(to_currency, None) + except Exception as e: + print("Error fetching exchange rate:", e) + return None + +def convert_currency(amount, from_currency, to_currency): + """ Converts the amount using real-time exchange rates """ + rate = get_exchange_rate(from_currency, to_currency) + if rate: + converted_amount = amount * rate + return converted_amount + else: + return None + +def main(): + """ Command-Line Interface for the Currency Converter """ + print("\nšŸŒ Welcome to the Currency Converter šŸŒ") + print("=====================================") + + try: + amount = float(input("\nEnter the amount to convert: ")) + from_currency = input("Enter the source currency (e.g., USD): ").upper() + to_currency = input("Enter the target currency (e.g., EUR): ").upper() + + result = convert_currency(amount, from_currency, to_currency) + if result: + print(f"\nšŸ’° {amount:.2f} {from_currency} = {result:.2f} {to_currency} šŸ’°") + else: + print("\nāŒ Error: Unable to fetch exchange rate. Please check your currency codes.") except ValueError: - #no amount entered - #default amount - pass - - #argv: - #[0] - program name - #[1] - SRC - #[2] - 'to' - #[3] - DST - if len(argv) != 4 or argv[2] != 'to': - raise Exception - - return amount, argv[1].upper(), argv[3].upper() - - -#main -#parse arguments -usage = '[] to ' -try: - amount, base, dest = parse_arguments() -except: - print('usage:') - print(usage) - exit(1) - -#convert -try: - base_symbol = codes.get_symbol(base) - dest_symbol = codes.get_symbol(dest) - - #validate currencies - if base_symbol is None: - raise Exception(f'Currency {base} is invalid') - if dest_symbol is None: - raise Exception(f'Currency {dest} is invalid') - - result = converter.convert(base_cur=base, dest_cur=dest, amount=amount) - result = round(result, 3) - - print(f'{amount}{base_symbol} equals to {result}{dest_symbol}') + print("\nāŒ Invalid amount. Please enter a numeric value.") -except ConnectionError as e: - print('Connection error') - exit(1) +if __name__ == "__main__": + main() -except Exception as e: - print(e.args[0]) - exit(1) diff --git a/app.log b/app.log new file mode 100644 index 00000000..fd9d020e --- /dev/null +++ b/app.log @@ -0,0 +1,4 @@ +2025-02-01 18:12:16,523 - INFO - Program started +2025-02-01 18:12:16,523 - ERROR - File not found: sample.txt +2025-02-01 18:16:28,805 - INFO - Program started +2025-02-01 18:16:28,805 - ERROR - File not found: sample.txt