dserodio
7/31/2018 - 2:21 PM

Python snippets

Python snippets

# 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)
"""Handle SIGINT (aka Ctrl+C)"""

import sys
import signal

signal.signal(signal.SIGINT, lambda signal_number, current_stack_frame: sys.exit(1))
# 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)