Skip to content

Instantly share code, notes, and snippets.

View shayelkin's full-sized avatar

Shay Elkin shayelkin

View GitHub Profile
@shayelkin
shayelkin / tornado_delay.py
Created June 24, 2015 18:54
Tornado sever that responds after a given delay
import logging
import tornado.web
import tornado.ioloop
from tornado.options import options, define, parse_command_line
define('port', default=8888, type=int, metavar='PORT', help='port to listen on')
define('timeout', default=20.0, type=float, metavar='SECONDS', help='number of seconds to timeout calls after')
class MainHandler(tornado.web.RequestHandler):
@shayelkin
shayelkin / private.xml
Last active August 29, 2015 14:04
Karabiner config: switch langauge with Option(Alt)+Shift
<?xml version="1.0"?>
<root>
<vkchangeinputsourcedef>
<name>KeyCode::VK_CHANGE_INPUTSOURCE_HEBREW</name>
<languagecode>he</languagecode>
<inputsourceid_equal>org.unknown.keylayout.Hebrew</inputsourceid_equal>
</vkchangeinputsourcedef>
<item>
<name>Option+Shift to switch between English and Hebrew</name>
// ==UserScript==
// @name Event Merge for Google Calendar™ (by @imightbeAmy. Thanks!)
// @namespace gcal-multical-event-merge
// @include https://www.google.com/calendar/*
// @include http://www.google.com/calendar/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @version 1
// @grant none
// ==/UserScript==
@shayelkin
shayelkin / gist:2397850f9b2c3df7e3aa
Last active August 29, 2015 14:01
What is the smallest natural number that can not be generated using the numbers 1,2,3,4 (exactly once each) and four basic arithmetic operations?
import operator
from sys import maxint
from itertools import permutations, product
ops = [operator.add, operator.sub, operator.mul, operator.div]
possibilities = set(reduce(lambda v, t: t[0](v,t[1]), zip(o, n[1:]), n[0]) for n in permutations(range(1,5)) for o in product(ops, repeat=3))
# Without repeating operators:
# possibilities = set(reduce(lambda v, t: t[0](v,t[1]), zip(o, n[1:]), n[0]) for n in permutations(range(1,5)) for o in permutations(ops, repeat=3))
class BooleanFunctor:
"""
A neat trick: this can be used in conditions, where it returns the value of b,
or as a decorator, where it returns the decorated function if b, or a lambda returning
falseValue otherwise.
>>> if BooleanFunctor(cond):
... # only if cond is true
... pass
@shayelkin
shayelkin / set-bing-wallpaper.rb
Created October 31, 2013 20:26
Download and set the current Bing wallpaper as GNOME wallpaper
#!/usr/bin/env ruby
OUT_FILE = "#{ENV['HOME']}/Pictures/BingWallpaper.jpg"
require 'open-uri'
f = open('http://www.bing.com/HPImageArchive.aspx?format=xml&idx=1&n=1&mkt=en-US')
s = f.read
img_base = s.match('<urlBase>(.*)</urlBase>').captures[0]
img_url = 'http://www.bing.com/' + img_base + '_1920x1200.jpg'
@shayelkin
shayelkin / fizzbuzz.rb
Last active August 23, 2020 20:26
Modulus-less fizzbuzz in Ruby
#!/usr/bin/env ruby
def fizzbuzz(n)
c = lambda {|i, s| ([nil] * i << s).cycle}
# Must first zip with a finite range, else we'd get infinite loop
Range.new(1, n).zip(c.call(2, 'Fizz'), c.call(4, 'Buzz')).map do |a,b,c|
s = "#{b}#{c}"
s.empty? ? a : s
end
end
@shayelkin
shayelkin / fizzbuzz.hs
Last active December 17, 2015 09:09
Inspiration for fizzbuzz.py
c i s = take i (repeat "") ++ [s]
fb = zipWith (++) (cycle (c 2 "Fizz")) (cycle (c 4 "Buzz"))
fizzbuzz = zipWith (\x y -> if null x then show y else x) fb [1..]
@shayelkin
shayelkin / fizzbuzz.py
Last active July 31, 2020 03:15
Modulus-less fizzbuzz
#!/usr/bin/env python
from itertools import cycle, izip
def lazy_fizzbuzz(n):
"""
Returns iterator over fizzbuzz printout, starting at 1 and ending at n (inclusive)
"""
c = lambda i, s: cycle(([''] * i) + [s])
fb = ('%s%s' % x for x in izip(c(2, 'Fizz'), c(4, 'Buzz')))
@shayelkin
shayelkin / take-timed-photo.sh
Last active December 15, 2015 14:49
Take a photo from webcam when connected to a wifi network with a given SSID. Combine with cron to have your picture taken every N minutes.
#!/bin/sh
allowed_ssid="everything"
capture_path="$HOME/captures"
# only take a photo when at work (connected to work network)
check_wireless=$(/sbin/iwconfig wlan0 | grep "ESSID:\"$allowed_ssid\"")
if [ -n "$check_wireless" ]; then
streamer -s 1280x720 -f jpeg -o "$capture_path/$(date +%Y%m%d.%H%M).jpeg"