Last active
August 20, 2020 16:23
-
-
Save the-dan/41e3f3f2aa65a1d96baedd5449e72907 to your computer and use it in GitHub Desktop.
Handy tools for Pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import traceback\n", | |
"\n", | |
"\n", | |
"class ContextNotify(object):\n", | |
" def __init__(self, action, mail_list, title=\"\"):\n", | |
" self.action = action\n", | |
" self.mail_list = mail_list\n", | |
" self.title = title\n", | |
" \n", | |
" def __enter__(self):\n", | |
" return self\n", | |
" \n", | |
" def __exit__(self, exc_type, exc_value, tb):\n", | |
" ml = \" \".join(self.mail_list)\n", | |
" if exc_type:\n", | |
" self.d = traceback.format_exception(exc_type, exc_value, tb, limit=None, chain=True)\n", | |
" self.d = \"\".join(self.d)\n", | |
" self.d = self.d.replace(\"'\", \"''\\\\''\")\n", | |
" !echo -ne '{self.action} failed: {self.d}' | mail -s \"[FAILURE] {self.title}: {self.action}\" {ml}\n", | |
" else:\n", | |
" !echo \"{self.action}\" | mail -s \"[SUCCESS] {self.title} {self.action}\" {ml}\n", | |
" \n", | |
"\n", | |
"class Notify(object):\n", | |
" def __init__(self, title, mail_list):\n", | |
" self.title = title\n", | |
" if type(mail_list) == str:\n", | |
" self.mail_list = [mail_list]\n", | |
" else:\n", | |
" self.mail_list = mail_list\n", | |
" \n", | |
" def notify(self, action):\n", | |
" return ContextNotify(action, self.mail_list, self.title)\n", | |
" \n", | |
" def send(self, *args):\n", | |
" ml = \" \".join(self.mail_list)\n", | |
" msg = \" \".join(args)\n", | |
" !echo \"{msg}\" | mail -s \"[NOTIFY] {self.title}\" {ml}\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "folio", | |
"language": "python", | |
"name": "folio" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.7" | |
}, | |
"toc": { | |
"base_numbering": 1, | |
"nav_menu": {}, | |
"number_sections": false, | |
"sideBar": false, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": false, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment