Skip to content

Instantly share code, notes, and snippets.

View dexterous's full-sized avatar

Saager Mhatre dexterous

View GitHub Profile
@dexterous
dexterous / metaclass_usage.py
Last active January 22, 2017 22:47
Sample Python Metaclass usage
from __future__ import print_function
from itertools import ifilter
import re
def snake_case(string):
return re.sub('[A-Z]+', lambda m: '_' + m.group().lower(), string).lstrip('_')
@dexterous
dexterous / fix-multi
Last active July 19, 2019 19:36
Sed script to fix CSV file with unescaped new lines.
#!/bin/sed -nrf
s_,,,,_,"","","",_g # first we substitute blank fields with quoted blanks for consistency
s_,,,_,"","",_g # first we substitute blank fields with quoted blanks for consistency
s_,,_,"",_g # first we substitute blank fields with quoted blanks for consistency
s_,$_,""_ # then we handle similar blank trailing fields
/^([^"]|",|"")/ { # if the line does not start with " (incomplete line)
x # first swap the previous line [see (*) below] into pattern space and this incomplete line into hold space
G # add the above held incomplete like to the pattern separated by \n
@dexterous
dexterous / psql_check.sh
Last active August 15, 2017 18:12
Quick n' dirty script to monitor PostgreSQL connectivity and connect latency and post metrics to AWS Cloudwatch
#!/bin/bash
export PGPASSWORD='password'
IFS=':'
echo ''
date '+%F %T'
for HOST in www.{foo,bar}.com; do
@dexterous
dexterous / prog.py
Last active July 19, 2019 19:28
A quick test to see how the GIL serializes seemingly concurrent operations in Cpython.
#!/usr/bin/python
from __future__ import print_function
import sys
import threading
import Queue
import argparse
@dexterous
dexterous / set-vault-ttl.sh
Created November 4, 2016 18:19
Command to set TTL for Vault logins
vault mount-tune -default-lease-ttl=1800s -max-lease-ttl=1800s auth/ldap
@dexterous
dexterous / nginx-certs
Created July 19, 2016 17:42
Get list of certificates configured for nginx sites-enabled
#!/bin/bash -e
#set -o xtrace
sed -nre '/ssl_certificate\s/ { F; s,^.*/(.*);$,\1\n,; p}' /etc/nginx/sites-enabled/*
@dexterous
dexterous / JVM Options (java -help; java -X)
Created June 29, 2016 14:50
JVM (HotSpot) settings & options
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the "server" VM
The default VM is server.
@dexterous
dexterous / turkpipe.py
Created April 20, 2016 21:03
TurkPipe, a CLI to batch MTurk jobs; copied from https://code.google.com/archive/p/turkpipe/
#!/usr/bin/python
"""
Copyright (c) 2009-2010 Voxilate, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@dexterous
dexterous / Vagrantfile
Last active July 20, 2016 19:01
Steps to install and configure OpenVPN client on Fedora 23
Vagrant.configure(2) do |config|
config.vm.box = "fedora/23-cloud-base"
config.vm.provider "virtualbox" do |vbox|
# Modify netmask of default NAT interface to avoid conflict with VPN
vbox.customize ["modifyvm", :id, "--natnet1", "192.168.20.0/24"]
end
config.vm.network "public_network", bridge: "wlan0"
#!/usr/bin/env python
def slicing(lang_idx, lang_cnt, lines):
'''Using islice; short, but requires knowledge of total line count'''
from itertools import islice
return islice(lines, lang_idx, 12, lang_cnt)
def counting(lang_idx, lang_cnt, lines):
'''Using count in conjunction with dropwhile; on and on and on and on...'''