Created
June 25, 2019 22:34
-
-
Save pybokeh/016c6a68bef52e2eb7ecbf4b3c6fca37 to your computer and use it in GitHub Desktop.
CountDown.ipynb
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": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Python Script that will calculate the time until an arbitrary date" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### This version just outputs once:" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "from datetime import datetime\nimport pytz\nfrom math import floor\n\nend_date = datetime(2014,12,18,19,30,0,0,pytz.UTC) # explicitly make time zone UTC\n\ntime_till_end_date = end_date - datetime.now(pytz.UTC) # explicitly make time zone UTC\n\ntotal_seconds = time_till_end_date.total_seconds()\ndays_left_remainder = total_seconds/(60*60*24) - floor(total_seconds/(60*60*24))\nhours_left = days_left_remainder * 24\nhours_left_remainder = days_left_remainder * 24 - floor(hours_left)\nminutes_left = hours_left_remainder * 60\nminutes_left_remainder = minutes_left - floor(minutes_left)\nseconds_left = minutes_left_remainder * 60\n\nprint('Days:', time_till_end_date.days, '|', 'Hours:', int(hours_left), '|', 'Minutes:', int(minutes_left), '|',\n 'Seconds:', int(seconds_left))", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### This version will continuously update the countdown:" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "from datetime import datetime\nimport pytz\nfrom math import floor\n\nend_date = datetime(2017,9,27,19,30,0,0,pytz.UTC) # explicitly make time zone UTC\n\nwhile True:\n time_till_end_date = end_date - datetime.now(pytz.UTC) # explicitly make time zone UTC\n\n total_seconds = time_till_end_date.total_seconds()\n days_left_remainder = total_seconds/(60*60*24) - floor(total_seconds/(60*60*24))\n hours_left = days_left_remainder * 24\n hours_left_remainder = days_left_remainder * 24 - floor(hours_left)\n minutes_left = hours_left_remainder * 60\n minutes_left_remainder = minutes_left - floor(minutes_left)\n seconds_left = minutes_left_remainder * 60\n\n # print on the same line\n print('\\r'+'Days: ' + str(time_till_end_date.days) + ' | ' + 'Hours: ' + str(int(hours_left)) + ' | ' + 'Minutes: ' + \\\n str(int(minutes_left)) + ' | ' + 'Seconds: ' + \"{0:d}\".format(int(seconds_left)),end='')", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "#### Thanks to Jim at the DOJO who suggested that I take advantage of the divmod() built-in function, the code is now so much more simpler:" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "from datetime import datetime\nimport pytz\nimport time\n\n# Time until July 2019 shutdown\nend_date = datetime(2019,6,28,20,0,0,0,pytz.UTC) # explicitly make time zone UTC. EST is 5 hours behind UTC\n\nwhile True:\n time_till_end_date = end_date - datetime.now(pytz.UTC) # explicitly make time zone UTC\n total_seconds = time_till_end_date.total_seconds()\n \n minutes, seconds = divmod(total_seconds, 60)\n hours, minutes = divmod(minutes, 60)\n days, hours = divmod(hours, 24)\n\n print('\\r' + str(int(days)) + ' day(s)' + ' | ' + str(int(hours)) + ' hour(s)' + ' | ' + str(int(minutes)) + \\\n ' minute(s)' + ' | ' + str(int(seconds)) + ' second(s)',end='')\n \n time.sleep(1)", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "16 day(s) | 3 hour(s) | 48 minute(s) | 55 second(s)" | |
}, | |
{ | |
"ename": "KeyboardInterrupt", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-3-2924c2b3ed7c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 17\u001b[0m ' minute(s)' + ' | ' + str(int(seconds)) + ' second(s)',end='')\n\u001b[0;32m 18\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 19\u001b[1;33m \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;31mKeyboardInterrupt\u001b[0m: " | |
] | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Another silly example of printing on the same line" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import time\n\nmessage = 'ALL YOUR BASE ARE BELONG TO US!'\n\nfor i in range(1, len(message)+1):\n time.sleep(0.3)\n print('\\r'+message[:i],end='')", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "ALL YOUR BASE ARE BELONG TO US!" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import time\nimport subprocess\nimport platform\n\nmessage = 'ALL YOUR BASE ARE BELONG TO US!'\n\nwhile True:\n for i in range(1, len(message)+1):\n time.sleep(0.3)\n print('\\r'+message[:i],end='')\n time.sleep(0.2)\n subprocess.Popen( \"cls\" if platform.system() == \"Windows\" else \"clear\", shell=True)", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "CountDown.ipynb", | |
"public": true | |
} | |
}, | |
"hide_input": false, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.5", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"latex_envs": { | |
"bibliofile": "biblio.bib", | |
"cite_by": "apalike", | |
"current_citInitial": 1, | |
"eqLabelWithNumbers": true, | |
"eqNumInitial": 0 | |
}, | |
"varInspector": { | |
"window_display": false, | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"library": "var_list.py", | |
"delete_cmd_prefix": "del ", | |
"delete_cmd_postfix": "", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"library": "var_list.r", | |
"delete_cmd_prefix": "rm(", | |
"delete_cmd_postfix": ") ", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
] | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment