Skip to content

Instantly share code, notes, and snippets.

View mikecharles's full-sized avatar

Mike Charles mikecharles

View GitHub Profile
@mikecharles
mikecharles / activate.csh
Last active October 31, 2021 19:38
Activate and deactivate a conda environment in C Shell
#!/bin/csh
# Get the name of this script
if ( $?_ ) then
# With tcsh the name of the file being sourced is available in
# $_.
set script_name = `basename $_`
else
# Fall back to $0 which, sometimes, will be the name of the
@mikecharles
mikecharles / cdf-correction.ipynb
Last active August 29, 2015 14:26
CDF correction
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mikecharles
mikecharles / MANIFEST.in
Last active October 26, 2015 21:04
Cut a release of a Python package - updates a VERSION file, makes a git commit and tag, and uploads the updated package to a PyPI repository
include VERSION
@mikecharles
mikecharles / README.md
Last active December 11, 2015 15:40
Execute a function in Python when the main application is finished running

When driver.py finishes running, email.send_email() is executed. This is because in the driver, it is registered in the atexit module:

import atexit
atexit.register(send_email)

which means that when the driver is finished and is about to exit, the send_email() function will be the last thing that runs. In this example send_email() simply prints that it's sending an email, but this is where you should actually email report_obj out.

See this script in action.

@mikecharles
mikecharles / find-missing-files.py
Last active September 24, 2018 19:58
Find missing files, given a range of dates and a file template
#!/usr/bin/env python
from datetime import date, datetime, timedelta
import os
import sys
import jinja2
script_name = os.path.basename(__file__)
def usage():
@mikecharles
mikecharles / README.md
Last active December 1, 2022 08:50
Validate command line date argument in Python

Validates a --dates argument in Python with the following possible formats:

  • YYYYMMDD (eg. 20120515)
  • YYYYMMDD-YYYYMMDD (eg. 20140115-20140315)
  • yesterday
  • today

To use this function, reference it in the argparse setup:

@mikecharles
mikecharles / ordinal.py
Created February 8, 2016 21:33
Ordinal of a number in Python
ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4])
@mikecharles
mikecharles / deploy.yml
Created March 3, 2016 14:52
Ansible playbook and vars file for deploying a Python application using Anaconda
# Ansible deployment playbook
---
# ------------------------------------------------------------------------------------------------
# Hosts to deploy to (set to all if you want to be able to just limit installation to specific
# hosts using the `--limit` arg to `ansible-playbook`.
#
- hosts: all
# ----------------------------------------------------------------------------------------------
# Files containing additional variables
#
@mikecharles
mikecharles / README.md
Created March 3, 2016 18:59
Load config data in a Python application from a default config file, and override some user-defined variables from a separate config file

Replace all instances of with your app name (eg. 'my-python-app', dashes are allowed), and all instances of with your app's main module (eg. 'my_python_app', dashes are NOT allowed), and

@mikecharles
mikecharles / backup-conda-env.sh
Last active March 25, 2016 21:21
Backup an anaconda environment
#!/bin/sh
# Set usage function
usage() {
status=$1
read -rd '' usage << EOM
Usage: [OPTIONS] SOURCE DEST
Backup the SOURCE anaconda environment to DEST