Skip to content

Instantly share code, notes, and snippets.

@dgroft
dgroft / rename_files_using_regex
Last active August 29, 2015 14:06
Rename multiple files in a directory using Regex.
# rename multiple files in a directory using regex
# source: http://stackoverflow.com/questions/5574648/use-regex-powershell-to-rename-files
ls | %{ ren $_ $($_.name -replace '(TestFile_\w+)(\d+)(\.txt)', '${1}20140910${3}')}
@dgroft
dgroft / gist:dfb6db932ad93b4c26b3
Created July 9, 2014 18:36
Extract contents between parens in a string using RegEx
// fetch "grab this part!"
Regex.Match("blah blah (grab this part!)", @"\(([^)]*)\)").Groups[1].Value
// source: http://stackoverflow.com/questions/378415/how-do-i-extract-a-string-of-text-that-lies-between-two-parenthesis-using-net
@dgroft
dgroft / unity_editor_window_progress_bar.cs
Created February 17, 2014 19:56
Create a progress bar embedded in a Unity editor window
Rect r = EditorGUILayout.BeginVertical();
EditorGUI.ProgressBar(r, 0.1f, "Doing something helpful");
GUILayout.Space(18);
EditorGUILayout.EndVertical();
@dgroft
dgroft / async_lambda_with_callback.cs
Last active August 29, 2015 13:56
Asynchronously invoke a lambda and invoke a callback lambda upon completion back on the main thread. Helpful in the event you're unable to enjoy TPL or async/await.
using System.Runtime.Remoting.Messaging;
using System.Threading;
// capture the ui thread context for completion callback
private SynchronizationContext context = SynchronizationContext.Current;
Action action = () => {
/* do some stuff */
};
@dgroft
dgroft / xml_transform_and_validate.py
Created November 22, 2013 15:17
Applies XSL transform to XML returned from an HTTP request, then validates it against an XSD/schema.
from __future__ import print_function
import argparse
import lxml.etree as ET
import urllib
parser = argparse.ArgumentParser(description="Transforms and Validates XML")
parser.add_argument("--feeds", help="the text file that holds the urls to be validated", default="feeds.txt")
parser.add_argument("--log", help="the output log file that reports all errors", default="log.txt")
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
@dgroft
dgroft / launch_sln.bat
Last active December 21, 2015 18:19
Launching Visual Studio 2008 with temporary environment variables and temporarily appending to PATH.
' invoke vsvars32.bat. just do it.
call "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
' temporary environment variables assignment
' set ENV_VAR = value
' temporary path modification
set path=%path%;C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE
' launch visual studio
@dgroft
dgroft / redis-centos.sh
Created July 30, 2013 20:31
Installs Redis on CentOS.
#!/bin/bash
################################################################################
# Installing Redis on CentOS
# with lots of help from:
# * https://gist.github.com/coder4web/5762999
# * http://www.codingsteps.com/install-redis-2-6-on-amazon-ec2-linux-ami-or-centos/
# * https://coderwall.com/p/ypo94q
# * https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522004/redis-server
################################################################################
@dgroft
dgroft / ParseEnum.cs
Created July 8, 2013 15:08
Parsing an enum.
enum DaysOfTheWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
var tues = (DaysOfTheWeek)Enum.Parse(typeof(DaysOfTheWeek), "Tuesday");
@dgroft
dgroft / maxsumnonadjacent.py
Last active December 19, 2015 08:19
Maximize the sum of a given collection such that if you include the number in sum, you may not use adjacent numbers toward the sum. For the collection [1, 5, 3, 9, 4], the max sum is 5 + 9 = 14. For the collection [1, 2, 3, 4, 5], the max sum is 1 + 3 + 5 = 9. For the collection [1, 3, 5, -1, 12, 6, 7, 11], the max sum is 1 + 5 + 12 + 11 = 29.
from __future__ import print_function
import heapq
# construct initial collection
initial_col = [1, 5, 3, 9, 4]
# construct heap
heap = []
# assemble the heap, store tuples as such: (original_value, original_index)
@dgroft
dgroft / GetPermutations.cs
Created June 25, 2013 16:50
Finds all permutations of a supplied list of generic values.
private static List<List<T>> GetPermutations<T>(List<T> values)
{
if (values.Count <= 1) return new List<List<T>>() { values };
var results = new List<List<T>>();
var perms = GetPermutations(values.Skip(1).ToList());
foreach (var perm in perms)
{