Skip to content

Instantly share code, notes, and snippets.

View sporsh's full-sized avatar

Geir Sporsheim sporsh

View GitHub Profile
@sporsh
sporsh / tally.rb
Created February 7, 2012 13:55 — forked from jimweirich/presentation.rb
Vital Ruby Lab 4
require "presentation"
p_file = ARGV[0] || "presentations.txt"
v_file = ARGV[1] || "votes.txt"
def load_presentations(filename)
result = {}
open(filename) { |file|
while entry = file.gets
m_obj = /^(\d+):([^:]+):([^:]+)/.match(entry)
@sporsh
sporsh / read_only_proxy.py
Created February 8, 2012 08:44 — forked from jimweirich/read_only_proxy.rb
Vital Ruby Lab 5
class ReadOnlyProxy(object):
def __init__(self, target):
self._target = target
def __getattribute__(self, name):
target = object.__getattribute__(self, '_target')
return getattr(target, name)
@sporsh
sporsh / url_fetcher.rb
Created February 9, 2012 10:27 — forked from jimweirich/url_fetcher.rb
Vital Ruby Advanced Lab 1
require 'open-uri'
class UrlFetcher
def fetch(url)
begin
open(url) { |stream| stream.read }
rescue SocketError, OpenURI::HTTPError, URI::InvalidURIError, Errno::ENOENT
nil
end
end
@sporsh
sporsh / html_parser.rb
Created February 9, 2012 12:54 — forked from jimweirich/html_parser.rb
Vital Ruby Advance Lab 2
require "nokogiri"
require "uri"
class HtmlParser
def parse(source, html_string)
uri = URI.parse(source)
html_doc = Nokogiri::HTML(html_string)
anchors = html_doc.xpath('//a[@href!="" and not(starts-with(@href, "#"))]')
links = anchors.map { |elem|
elem.attribute('href').value
@sporsh
sporsh / backend.py
Created October 16, 2012 22:34
twisted interactive shell
from threading import Thread, Event
class Backend(object):
reactor_started = Event()
def start(self, timeout=None):
print "Starting backend..."
self._thread = Thread(target=self._start_reactor)
self._thread.start()
self.reactor_started.wait(timeout)
@sporsh
sporsh / rpcserver.py
Created October 23, 2012 19:49
A Twisted example to demo deferreds, client/server protocols and endpoints
import time
from twisted.python import failure
from twisted.internet import reactor, defer, task
from twisted.internet.protocol import Protocol
# Define some messages for our protocol
MSG_REQUEST = '>'
MSG_REQ_ACK = ':'
MSG_REQ_SUCCEEDED = '='
@sporsh
sporsh / .gitconfig
Last active October 12, 2015 01:47
git changes - Alias to get a quick overview of local and remote changes in current branch
[alias]
ci = commit
lg = log --abbrev-commit --format='%C(yellow)%h %Cblue%aN %Cgreen%ar %Creset%s'
co = checkout
st = status
loglocal = log --abbrev-commit --format='%Cgreen* %C(yellow)%h %Cblue%aN %Cgreen%ar %Creset%s' FETCH_HEAD..
logremote = "!f() { git fetch&&\
git log --abbrev-commit --format='%Cred* %C(yellow)%h %Cblue%aN %Cgreen%ar %Creset%s' ..FETCH_HEAD;\
}; f"
changes = "!f() { echo 'REMOTE CHANGES:\n---------------';\
from twisted.internet import defer
import threading
import Queue
from twisted.python import failure
def blocking_call_from_thread(reactor, f, *args, **kwargs):
"""Calls a twisted function from a thread and blocks until
the deferred has called back.
If the function did not return a deferred, raise an exception.
"""
@sporsh
sporsh / parse.py
Created November 22, 2012 00:17
Implementation of Android Debug Bridge (ADB) protocol in Twisted
from adb import protocol
data = (
'\x4f\x50\x45\x4e\x02\x00\x00\x00'
'\x00\x00\x00\x00\x09\x00\x00\x00'
'\x31\x03\x00\x00\xb0\xaf\xba\xb1'
'\x73\x68\x65\x6c\x6c\x3a\x6c\x73'
'\x00'
)
@sporsh
sporsh / cache.py
Last active September 20, 2022 04:46
Python examples of proxy objects python -u cache.py
class CacheCallable(object):
"""Callable that cache results based on given arguments
"""
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]