Last active
February 27, 2023 17:29
-
-
Save dserodio/0751fe4d3e0199a5c7ef8498640faa8e to your computer and use it in GitHub Desktop.
Python snippets
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
# Parse date | |
# $ pip install python-dateutil | |
from dateutil import parser | |
begin = parser.parse("Aug 28 1999 12:00AM") | |
end = parser.parse("2013-09-11") | |
# Print duration | |
delta = end - begin | |
print str(delta) |
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
"""Handle SIGINT (aka Ctrl+C)""" | |
import sys | |
import signal | |
signal.signal(signal.SIGINT, lambda signal_number, current_stack_frame: sys.exit(1)) |
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
# bare-bones logging config | |
# | |
# see https://gist.github.com/dserodio/0751fe4d3e0199a5c7ef8498640faa8e for a sophisticated config | |
import logging | |
from pathlib import Path | |
log_level = logging.INFO | |
log_format = "%(asctime)-15s [%(levelname)s] %(message)s" | |
log_file = 'script' | |
if '__file__' in globals(): | |
# Python 3.4+ | |
filename = Path(__file__).stem | |
logging.basicConfig(format=log_format, level=log_level, filename=log_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment