Skip to content

Instantly share code, notes, and snippets.

View atbradley's full-sized avatar

Adam Bradley atbradley

View GitHub Profile
@atbradley
atbradley / gist:946435
Created April 28, 2011 14:18
Basic scaffold for a Python decorator
def SomeDecorator(next):
def getFunction(fn):
def doStuff(req, *args):
return fn(req, *args)
return doStuff
return getFunction
@atbradley
atbradley / gist:946438
Created April 28, 2011 14:19
Python equivalent of php's md5()
def md5(str):
import md5
hasher = md5.new()
hasher.update(str)
return hasher.hexdigest()
@atbradley
atbradley / gist:946439
Created April 28, 2011 14:20
Generate a random-enough string. Maybe useful for passwords or for the encryption seed required in some frameworks' configuration file.
function randstr($len = 8) {
$src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$src = $src.$src.$src.$src.$src;
return substr(str_shuffle($src), 0, $len);
}
@atbradley
atbradley / stripTags.groovy
Created April 28, 2011 14:22
Very simplistically remove HTML tags from strings.
public static String stripTags(String input) {
return input.replaceAll("\\<.*?>","");
}
@atbradley
atbradley / gist:1006998
Created June 3, 2011 19:30
CodeIgniter configuration to allow both GET variables and segment-based addressing
$config['uri_protocol'] = &quot;PATH_INFO&quot;;
$config['enable_query_strings'] = TRUE;
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_-?=+&;';
@atbradley
atbradley / import_links_from_delicious.php
Created June 17, 2011 21:07
Sample event to demonstrate importing outside data (Delicious links from RSS) into the Symphony CMS
@atbradley
atbradley / gist:2014184
Created March 11, 2012 00:15
Return a String representation of an XmlParser Node.
/*
Similar to XSLT's copy-of element. Useful if e.g. you have node containing XHTML
that you want to return as-is.
*/
Node.metaClass.asString = {
def text = []
delegate.children().each { child ->
@atbradley
atbradley / gist:2163004
Created March 22, 2012 20:02
Disable automatic redirect handling in HTTPBuilder
// Return the actual location of a web page given a shortened url.
// Based in part on this Stack Overflow answer:
// http://stackoverflow.com/questions/1519392/how-to-prevent-apache-http-client-from-following-a-redirect
import groovyx.net.http.HTTPBuilder
import org.apache.http.impl.client.AbstractHttpClient
import org.apache.http.params.BasicHttpParams
import static groovyx.net.http.Method.HEAD
@atbradley
atbradley / gist:2710414
Created May 16, 2012 13:41
EAP creation example using XmlSlurper
import groovy.xml.MarkupBuilder
//Read a tab-delimited file.
def tdl = new TLD('comiclist.txt', false, 'Box No.')
/*
//Turn it into a simple xml format, one 'comic' per spreadsheet row.
def wrt = new StringWriter()
def xml = new MarkupBuilder(wrt)
xml.comics {
@atbradley
atbradley / profile.R
Created July 18, 2012 19:30
Configure RStudio to generate fragments, not full webpages, from R Markdown
library(markdown)
options(rstudio.markdownToHTML =
function(i,o) {
markdownToHTML(i,o,fragment.only=TRUE)
}
)