Skip to content

Instantly share code, notes, and snippets.

View mikecharles's full-sized avatar

Mike Charles mikecharles

View GitHub Profile
@mikecharles
mikecharles / list_modules.py
Last active April 25, 2024 17:55
Python script to list all submodules of a given Python package
#!/usr/bin/env python
import os, sys
import pkgutil
def list_submodules(list_name, package_name):
for loader, module_name, is_pkg in pkgutil.walk_packages(package_name.__path__, package_name.__name__+'.'):
list_name.append(module_name)
module_name = __import__(module_name, fromlist='dummylist')
if is_pkg:
@mikecharles
mikecharles / get-python-dependencies.md
Created April 11, 2024 14:11
Get all Python dependencies for a Python application

To get a unique list of all Python packages your application depends on:

grep -irhP '^import ' interp_stn_to_grid | sort | uniq

where interp_stn_to_grid is the top-level directory containing all the Python scripts in your application.

@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 / 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 / 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 / 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 / start-vpn.py
Last active September 17, 2021 01:42
Start and stop OpenConnect VPN
#!/usr/bin/env python
from subprocess import call, PIPE
import os
user = '<user>'
vpn_servers = ['<server1>', '<server2>']
for vpn_server in vpn_servers:
@mikecharles
mikecharles / README.md
Created July 18, 2019 19:05
Create a NetCDF file in Python

This is a simple example that creates a NetCDF file for 1 degree global 2m temperature. There is no time coordinate.

For this portion of code:

ds = xr.DataArray(data, ...)

data is a NumPy array, assumed to have a shape of num_lats x num_lons

@mikecharles
mikecharles / README.md
Last active March 4, 2019 19:30
Deploy a Python application using Ansible and the Ansistrano role

To deploy your app, you'll need to install Ansible and the Ansistrano deploy and rollback Ansible Galaxy roles.

Then create a directory in your app (eg. named ansible) and put these files in there. You should only need to modify the top few variables in vars.yml to get the deployment working for your app.

To do the deployment, use a command like this:

ansible-playbook --limit ansible/deploy.yml

@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():