Skip to content

Instantly share code, notes, and snippets.

Sam Stokes samstokes

Block or report user

Report or block samstokes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@samstokes
samstokes / regex-groups-global.rb
Created Nov 18, 2009
How to extract groups from a regex match in Ruby without globals or temporary variables. Code snippets supporting http://blog.samstokes.co.uk/post/251167556/regex-style-in-ruby
View regex-groups-global.rb
if "foo@example.com" =~ /@(.*)/
$1
else
raise "bad email"
end
# => "example.com"
@samstokes
samstokes / todo
Last active Nov 2, 2018
A todo management system in a gist
View todo
#!/bin/bash -e
if [[ $# > 0 ]]; then
case "$1" in
-h | -\? | --help )
{
echo "Add a todo:"
echo " todo Reformulate the widget plans."
echo "See what you have to do:"
echo " todo"
@samstokes
samstokes / sqlalchemy_logging.py
Created May 6, 2016
Turning on SQL logging in SQLAlchemy
View sqlalchemy_logging.py
import logging
logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
View keybase.md

Keybase proof

I hereby claim:

  • I am samstokes on github.
  • I am samstokes (https://keybase.io/samstokes) on keybase.
  • I have a public key ASBjVCcl1P5w4m-tmbthV5BAb3AOcY4g2Uxu3v7G6e8kXAo

To claim this, I am signing this object:

@samstokes
samstokes / stash
Created Nov 6, 2012
example git log with a stash
View stash
sstokes@sstokes-mba-ubuntu:gittest$ git log --all --decorate --graph --oneline
* 15e2132 (refs/stash) WIP on master: 2e83ed8 initial commit
|\
| * 61442a1 index on master: 2e83ed8 initial commit
|/
* 2e83ed8 (HEAD, master) initial commit
@samstokes
samstokes / test.rb
Created Apr 18, 2012
Test public Ruby gist
View test.rb
# This is a large amount of Ruby code
# No, really, it is
# Can't you tell?
def foo(bar)
baz(bar)
end
# That was a large amount of Ruby code
# Wiffle bamble boo
@samstokes
samstokes / dg_example_bind.rb
Created Nov 14, 2011
DG example: bind: following links on a Google search
View dg_example_bind.rb
EM.run do
fetch('http://google.com/search?q=deferrable_gratification').bind! do |doc|
fetch((doc / 'ol' / 'li' / 'a')[0][:href])
end.bind! do |doc|
fetch((doc / '#repository_homepage').at(:a)[:href])
end.callback do |doc|
puts doc.at(:title).inner_text
# now the previous 'doc's aren't in scope, so I can't accidentally
# refer to them
end.errback do |error|
@samstokes
samstokes / dg_loop_syntaxen.rb
Created Nov 7, 2011
DG loop combinator thinking
View dg_loop_syntaxen.rb
# Hmmm
DG::loop_until { url.present? && chunks_done < 25 }.body do
make_http_request(stuff, things, other_things).safe_callback do |blah|
blah
end
end.transform
# Yum
DG::loop_until(lambda { url.present? && chunks_done < 25 }) do
make_http_request(stuff, things, other_things).safe_callback do |blah|
@samstokes
samstokes / methods_returning.rb
Created Oct 21, 2011
Stick this in your .irbrc and smoke it
View methods_returning.rb
def methods_returning(answer, *args)
# probably want to filter out some methods
# e.g. ones that mutate their receiver...
blacklist = %w()
(methods - blacklist).select do |method|
guinea_pig = begin
clone
rescue TypeError => e
raise unless e.message =~ /can't clone/
@samstokes
samstokes / SerializableSerialization.java
Created Oct 17, 2011
One ISerialization to rule them all?
View SerializableSerialization.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import backtype.storm.serialization.ISerialization;
public class SerializableSerialization<T extends Serializable> implements ISerialization<T> {
You can’t perform that action at this time.