Skip to content

Instantly share code, notes, and snippets.

@hasenj
hasenj / oud-lines.html
Created February 6, 2011 02:24
تخطيط العود .. افتح الملف في المتصفح و اطبعه
<p>تخطيط العود</p>
<style type="text/css">
p { direction: rtl; text-align: center; font: 24pt normal serif; width: 400px; }
div { float: left; height: 100px; border: solid 1px gray; border-right: none; }
</style>
<script type="text/javascript">
// <div style="width: 3.3cm;">&nbsp;</div>
var m = 1.059463094; //magic number
var a = 60;
var b = a;
@hasenj
hasenj / haiku.py
Created July 30, 2012 07:45 — forked from friggeri/haiku
python version: random heroku-like name generator
import random
def haiku():
# originally from: https://gist.github.com/1266756
# with some changes
# example output:
# "falling-late-violet-forest-d27b3"
adjs = [ "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark",
"summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter",
"patient", "twilight", "dawn", "crimson", "wispy", "weathered", "blue",
"billowing", "broken", "cold", "damp", "falling", "frosty", "green",
@hasenj
hasenj / random_alphanum.js
Created September 7, 2012 03:38
random alphanum in javascript
alpha = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"
alpha[Math.round(Math.random() * 62)]
@hasenj
hasenj / peewee_cleardb.py
Created April 6, 2013 15:47
Connect to a ClearDB database via the peewee ORM
from peewee import *
# assume environment variable CLEARDB_DATABASE_URL is set (true on Heroku if you have the cleardb addon)
url = urlparse.urlparse(os.getenv('CLEARDB_DATABASE_URL'))
dbname = url.path[1:url.path.index('?')]
db = MySQLDatabase(dbname, host=url.hostname, user=url.username, passwd=url.password)
# Now use db to connect to the database ..
class BaseModel(Model):
@hasenj
hasenj / text-only-tumblr-theme.html
Last active December 16, 2015 00:19
Clean Tumblr theme that only supports Text and Link posts, with LiveFyre for comments. Uses bootstrap and a few fonts from Google Web Fonts. Includes support for automagically fixing Arabic text to run from right-to-left.
<html>
<head>
<title>{Title}</title>
<link
href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css"
rel="stylesheet">
<meta name="text:LiveFyreSiteID" content=""/>
<link rel="shortcut icon" href="{Favicon}">
@hasenj
hasenj / timeout_demo.py
Created April 10, 2013 07:42
setTimeout for python. Requires the package `apscheduler` (install via pip).
from apscheduler.scheduler import Scheduler
import datetime as dt
sched = Scheduler()
sched.start()
def timeout(job_fn, *fn_args, **delta_args):
"""Like setTimeout in javascript; returns a job object
First argument is the function to be called.
@hasenj
hasenj / requests.js
Created April 13, 2013 14:37
Experimental "ajax" library
echo = function(areq) { console.log(areq.response) };
requests = (function() {
var module = {};
var AsyncRequest = XMLHttpRequest;
// takes a dict and returns a string "a=b&c=d"
var toUrlParams = function(dict) {
var res = [];
for(var key in dict) {
@hasenj
hasenj / markdown_furigana.py
Last active February 24, 2016 18:04 — forked from huangziwei/markdown_furigana.py
a python script for writing furigana in markdown
import fileinput
import re
import sys
```
eg. [東京]{とうきょう} -> <ruby>東京<rp>(</rp><rt>とうきょう</rt><rp>)</rp></ruby>
```
def furiganize(line):
# (?<!\\) means don't match if preceeded by a backslash
return re.sub(ur'(?<!\\)\[(.*?)\]\{(.*?)\}', ur'<ruby>\1<rp>(</rp><rt>\2</rt><rp>)</rp></ruby>', line.rstrip())
@hasenj
hasenj / app_metal.c
Created December 30, 2019 12:46
Pure C Cocoa Application with Window and Metal
// How to build:
// # compile the metal shaders
// xcrun -sdk macosx metal -c shaders.metal -o shaders.air
// xcrun -sdk macosx metallib shaders.air -o shaders.metallib
// # compile the c file
// clang app_metal.c -framework Cocoa -framework Metal -o metal_c.app
//
//
// Draw a triangle using metal
// Metal tutorial followed here: https://www.raywenderlich.com/7475-metal-tutorial-getting-started
@hasenj
hasenj / cache.ts
Last active August 3, 2023 16:51
Caching for Javascript/Typescript using arbitrary objects as keys
import * as refs from "./refs";
class ById {
constructor(public param: any) { }
}
// wraps an object in a special flag so that the cache uses the object's id instead of hashing its keys and values
export function byId(param: any) {
return new ById(param)
}