diff --git a/Converter/converter_values.py b/Converter/converter_values.py index 3cc9c104..f2b335b6 100644 --- a/Converter/converter_values.py +++ b/Converter/converter_values.py @@ -31,6 +31,7 @@ cubic m : m3 litre: l milliliter : ml +kilolitre : kl MASS : M ---------------------------------------------------------------- gram : g @@ -49,6 +50,11 @@ week : w year : y +TEMPERATURE : t ---------------------------------------------------------------- +kelvin : K +Celsius : C +Fahrenheit : F + ''', "help":''' @@ -98,7 +104,8 @@ "ml":10**(6) , "l":10**(3) , "in3":61023.7441 , - "ft3":35.31467} + "ft3":35.31467 , + "kl":1} M= {"g":1 , "kg":10**(-3) , @@ -115,3 +122,6 @@ "min":1440 , "sec":86400 } +t= {"K":274.15 , + "C":1 , + "F":33.8 } diff --git a/Error and Logs Handling/README.md b/Error and Logs Handling/README.md new file mode 100644 index 00000000..553ab74c --- /dev/null +++ b/Error and Logs Handling/README.md @@ -0,0 +1,26 @@ +![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flat&color=BC4E99) +![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103) +[![View My Profile](https://img.shields.io/badge/View-My_Profile-green?logo=GitHub)](https://github.com/ndleah) +[![View Repositories](https://img.shields.io/badge/View-My_Repositories-blue?logo=GitHub)](https://github.com/ndleah?tab=repositories) + +# Execute Shell Command + +## 🛠️ Description +Python code is written to perform below 3 functionalities: +1. Execute shell command and get the output and error. +2. Logging of all steps +3. Archiving of logs + +## ⚙️ Languages or Frameworks Used +Language used is Python. There are no dependencies. + +## 🌟 User Inputs +There are 3 inputs required from user before executing the code: +1. Email Id +2. Log path +3. Archive Log path + +Make sure to provide correct path for log, archive log and email_id in python file before executing it. + +## 🌟 How to run +Simply import required funtions in your Python script and execute it. diff --git a/Error and Logs Handling/error_logging_handling.py b/Error and Logs Handling/error_logging_handling.py new file mode 100644 index 00000000..eb133a99 --- /dev/null +++ b/Error and Logs Handling/error_logging_handling.py @@ -0,0 +1,108 @@ +"""This script aims at creating log file and sending it on email""" +import os +import sys +import glob +import logging +import datetime +from shutil import move +import subprocess + + +"""Generic error handling module""" +def error(status, error_msg, module_name, return_code): + + if status.lower() == 'error': + logging.error('!!! Error !!!') + logging.error('Error executing module : %s', module_name) + logging.error(error_msg) + logging.error('Module return code : %s', return_code) + logging.error('Exiting script with exit(1) !!!') + email_log_file(log_file, status='failure') + sys.exit(1) + + if status.lower() == 'warning': + logging.warning('!!! Warning !!!') + logging.warning('Error executing module : %s', module_name) + logging.warning(error_msg) + logging.warning('Module return code : %s', return_code) + logging.warning('Script execution will continue !!!') + + +"""Generic Logging function """ +def initiate_logging(): + + dt_now = datetime.datetime.today() + log_file = '' + 'testing' + dt_now.strftime('_%Y%m%d_%H%M%S') + '.log' + log_file_pattern = '' + 'testing' + '*' + '.log' + log_file_list = glob.glob(log_file_pattern) + + for file in log_file_list: + if os.path.exists(file): + dest_file = '' + file.split('/')[-1] + if os.path.exists(dest_file): + os.remove(dest_file) + move(file, '') + + logging.basicConfig(filename=log_file, level=logging.DEBUG, + format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%Y-%m-%d %H:%M:%S') + + return log_file + + +"""Executes the provided unix shell command """ +def execute_shell_command(command): + + try: + proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + out, err = proc.communicate() + return_code = proc.returncode + if err: + print(str(err)) + return out, return_code + except Exception as err: + print("Exception Occurred while executing module : %s", str(err)) + return 105 + + +"""Emails the status of job execution along with log file as attachment""" +def email_log_file(log_file, status): + + if status == 'success': + body = 'Execution successful...PFA log file' + subject = 'Execution successful - testing' + + email_cmd = """echo "{b}" | mailx -s "{s}" -a "{l}" "{to}" """.format(b=body, s=subject, l=log_file, to='') + logging.info('Email Command :- %s', email_cmd) + os.system(email_cmd) + else: + body = 'Execution Failed...PFA log file' + subject = '!!! Failure - testing !!!' + + email_cmd = """echo "{b}" | mailx -s "{s}" -a "{l}" "{to}" """.format(b=body, s=subject, l=log_file, to='') + logging.info('Email Command :- %s', email_cmd) + os.system(email_cmd) + + +# Start of Execution +if __name__ == '__main__': + + log_file = initiate_logging() + + logging.info('!!! Execution Begins !!!') + + command ='echo Madhuri' + result, status = execute_shell_command(command) + + if status != 0: + logging.info("Exception in running shell command") + STATUS = 'error' + ERROR_MSG = '!!! Error !!! Execution failed !!!' + MODULE_NAME = 'Execution Script' + RETURN_CODE = '' + error(STATUS, ERROR_MSG, MODULE_NAME, RETURN_CODE) + else: + command_result = result.decode('utf8').rstrip("\r\n") + logging.info(command_result) + + email_log_file(log_file, status='success') +# End of Execution diff --git a/README.md b/README.md index fe8ed44b..364d7d13 100644 --- a/README.md +++ b/README.md @@ -281,3 +281,4 @@ If you have any feedback or ideas to improve this project, feel free to contact Reeha's Github +# python-mini-projects diff --git a/Smart_Calculator/calculator.py b/Smart_Calculator/calculator.py index 2c46b4eb..58692966 100644 --- a/Smart_Calculator/calculator.py +++ b/Smart_Calculator/calculator.py @@ -59,7 +59,7 @@ def calculate(): list.insert(END,'something went wrong please enter again') operations = {'ADD':add,'ADDITION':add, 'SUM':add, 'PLUS':add, - 'SUB':sub, 'DIFFERENCE':sub, 'MINUS': sub, 'SUBTRACT':sub, 'DIFF':sub, + 'SUB':sub, 'DIFFERENCE':sub, 'MINUS': sub, 'SUBTRACT':sub, 'DIFF':sub, 'SUBTRACTION':sub, 'LCM':lcm, 'HCF':hcf, 'PRODUCT':mul, 'MULTIPLICATION':mul, 'MULTIPLY':mul, 'DIVISION':div, 'DIV':div, 'DIVIDE':div, 'MOD':mod,'REMAINDER':mod, 'MODULUS':mod}