Skip to content

Instantly share code, notes, and snippets.

@stevedoyle
stevedoyle / cleanup_whitespace.py
Last active December 14, 2015 05:58
Clean whitespace from source files. Remove trailing whitespace at the end of files and converts tabs to spaces.
#!/usr/bin/env python
#########################################################################
# Script to clean up whitespace in source files.
# - Removes trailing whitespace at the end of lines
# - Converts tabs to spaces (4 spaces = 1 tab)
#
# Copyright 2013 Stephen Doyle
#########################################################################
@stevedoyle
stevedoyle / fileinput
Created July 8, 2013 13:44
Use fileinput to read input from files specified on the command line or stdin if no files specified. This allows something like: cat foo.txt | bar.py
import fileinput
for line in fileinput.input():
pass
@stevedoyle
stevedoyle / pcie_bw_table
Created November 22, 2013 16:06
Simple utility script to generate a table of PCIe bandwidth for various link width configurations and for raw and 75% efficiency
#!/usr/bin/python
def divider():
print '-'*40
def width_str(width):
return "x%d" % (width)
gens = [1,2,3]
link_widths = [1,2,4,8,16,24,32]
@stevedoyle
stevedoyle / compress_files.py
Created April 17, 2014 10:25
Compress individual files with gzip
#!/usr/bin/env python
import argparse
import subprocess
import shlex
import sys
def compress_file(filename, level):
try:
cmd = shlex.split("gzip %s -c %s" % (level, filename))
@stevedoyle
stevedoyle / xpath
Created November 18, 2014 20:35
Use xpath to find all nodes of a given name
from lxml import objectify
tree = objectify.parse(tcx_file')
root = tree.getroot()
root.xpath('//ns:HeartRateBpm', namespaces={'ns': 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2'}
@stevedoyle
stevedoyle / aes_cbc_iv_example.py
Created November 19, 2014 11:18
Demonstrate pseudo-random IV generation for AES-CBC using a counter as IV input
#!/usr/bin/env python
from Crypto.Cipher import AES
from Crypto.Cipher import DES3
def add_padding(message):
pad_len = 16 - (len(message) % 16)
message += (' ' * pad_len)
return message
@stevedoyle
stevedoyle / argparse.py
Last active December 23, 2016 13:00
Using argparse for command line argument parsing in python
import argparse
import logging
def main():
parser = argparse.ArgumentParser(description="Example")
parser.add_argument('-v', '--verbose', action='store_true', help="Verbose output")
parser.add_argument('--size', nargs='*', default=[256], type=int, choices=[256,512,768,1024], help="Data sizes (default = 256)")
@stevedoyle
stevedoyle / nsnumber_arithmetic.swift
Created January 22, 2015 09:53
Working with NSNumber in Swift
var targetDistance: NSNumber = 4000
var completedDistance: NSNumber = 121.7
var percentComplete: Float
// NSNumber can contain different types, so need to be explicit about the type of its value
// when using it in arithmetic
percentComplete = (completedDistance.floatValue / targetDistance.floatValue) * 100
println(percentComplete)
@stevedoyle
stevedoyle / NSFetchedResultsController_Example.swift
Created January 23, 2015 11:04
NSFetchedResultsController Usage Pattern
class GoalsViewController: UITableViewController, NSFetchedResultsControllerDelegate {
...
let managedContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
var fetchedResultsController: NSFetchedResultsController = NSFetchedResultsController()
...
func getFetchedResultsController() -> NSFetchedResultsController {
fetchedResultsController = NSFetchedResultsController(fetchRequest: goalFetchRequest(), managedObjectContext: managedContext!, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultsController
}
@stevedoyle
stevedoyle / xpath_find_text
Last active August 29, 2015 14:14
XPath for finding elements with a given text value
Find students whose name is Bob:
//Student/name[text()="Bob"]