Skip to content

Instantly share code, notes, and snippets.

View robperc's full-sized avatar

Robert I. Percival robperc

View GitHub Profile
@robperc
robperc / cleanup_ebs.py
Created October 31, 2018 19:48
Lists, snapshots, then deletes unattached EBS volumes. If NO_OP is set to True then only lists unattached EBS volumes.
import boto3
NO_OP = True
REGION_NAME = 'us-west-2'
ec2r = boto3.resource('ec2', region_name=REGION_NAME)
ec2c = boto3.client('ec2', region_name=REGION_NAME)
vols = ec2r.volumes.filter(Filters=[{'Name': 'status', 'Values': ['available']}])
@robperc
robperc / EditLoginItems.py
Created March 29, 2016 17:41
Editing of Login Items in user context using PyObjC
import sys
import Cocoa
import CoreFoundation
from LaunchServices import LSSharedFileListCreate, kLSSharedFileListSessionLoginItems, LSSharedFileListCopySnapshot, kLSSharedFileListItemBeforeFirst, LSSharedFileListItemCopyDisplayName, LSSharedFileListInsertItemURL
TO_ADD_NAME = "YourAppName"
TO_ADD_PATH = "/Applications/YourAppname.app/"
items = LSSharedFileListCreate(CoreFoundation.kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, None)
snapshot = LSSharedFileListCopySnapshot(items, None)
@robperc
robperc / SharedFileList.py
Last active March 8, 2016 22:58
PyObjC SharedFileList 10.11.x Fix
"""
LSSharedFileList functions are missing from LaunchServices framework in OS X 10.11.x
(see https://bitbucket.org/ronaldoussoren/pyobjc/issues/126/sharedfilelist-imports-from-pyobjc)
This solution allows for importing of SharedFileList functions without requiring patching of PyObjC
"""
import objc
from Foundation import NSBundle
@robperc
robperc / ApachePuppetModuleBestPractice
Last active March 2, 2016 18:50
Apache Puppet Module using Puppet's best practice standards and architecture.
Apache Puppet Module using Puppet's best practice standards and architecture.
@robperc
robperc / GetWeightedRand.py
Last active February 28, 2016 04:25
Assortment of functions for selecting weighted random elements from a dictionary of 'element: weight' pairs.
"""
Assortment of functions for selecting weighted random elements from a dictionary of 'element: weight' pairs.
"""
import random
def getRand(items):
"""
Selects random element from items and returns.
@robperc
robperc / WalkDirGenerator.py
Last active February 28, 2016 04:47
Re-implementation of os.walk as a recursive generator
"""
Re-implementation of os.walk as a recursive generator.
Yields full paths of files encountered along the walk of the parent directory.
"""
import os
def walkDir(parent):
"""
@robperc
robperc / ReversePolishNotationCalculator.py
Last active February 28, 2016 04:45
Simple Reverse Polish notation calculator.
"""
Simple Reverse Polish notation calculator.
Uses a deque as a queue to push and pop operands from and eval to perform operations.
"""
from collections import deque
def isNumber(val):
"""
@robperc
robperc / ApacheMultiParse.py
Last active February 28, 2016 04:24
Concurrent Apache Log Parsing w/ multiprocessing module
"""
Concurrently parse Apache logs and generate dictionary containing aggregate of 'ip / hostname: frequency' pairs.
"""
from multiprocessing import Pool
from collections import Counter
def readLog(path):
"""