Skip to content

Instantly share code, notes, and snippets.

View runekaagaard's full-sized avatar

Rune Kaagaard runekaagaard

  • Copenhagen, Denmark
View GitHub Profile
from __future__ import division
from itertools import izip
from math import exp, sqrt
from random import random
from pprint import pprint
"""
Working through http://neuralnetworksanddeeplearning.com and
implementing it in no-library python.
"""
#!/usr/bin/env python
# encoding=utf-8
import sys
import os
from pprint import pprint
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "qa.settings")
from django.contrib.contenttypes.models import ContentType
# Output utf-8 on the command line, why is this buggy on my box?
reload(sys)
@runekaagaard
runekaagaard / celerybeat
Created March 27, 2013 13:57
An /etc/init.d/celerybeat script modified from https://raw.github.com/ask/celery/master/contrib/generic-init.d/celeryd to support a status command using the standard lsb functions.
#!/bin/bash
# =========================================================
# celerybeat - Starts the Celery periodic task scheduler.
# =========================================================
#
# :Usage: /etc/init.d/celerybeat {start|stop|force-reload|restart|try-restart|status}
# :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
#
# See http://docs.celeryq.org/en/latest/cookbook/daemonizing.html#init-script-celerybeat
@runekaagaard
runekaagaard / fizzbuzz-forloop.php
Created August 7, 2012 19:23
FizzBuzz solutions
<?php
function fizzbuzz($n) {
for ($f=$b=$i=1;
$i<=$n;
$line = '',
($f==3 && !$f=0) ? $line = "Fizz" : null,
($b==5 && !$b=0) ? $line .= "Buzz" : null,
print ($line !== '' ? $line : $i) . "\n",
++$f,++$b,++$i
@runekaagaard
runekaagaard / php-generator-example.php
Created July 4, 2012 11:02
A userland implementation of a generator function. A bit like a python context manager too.
<?php
namespace Functional;
error_reporting(E_ALL|E_STRICT);
const BEFORE = 1;
const NEXT = 2;
const AFTER = 3;
const FORWARD = 4;
@runekaagaard
runekaagaard / gist:1729500
Created February 3, 2012 10:14
/usr/local/bin/hg-delete-obstructed-files
#!/usr/bin/env php
<?php
$obstructed_files = array();
foreach (array_filter(explode("\n", `hg status $(hg root)`)) as $line) {
if ($line{0} !== "!") continue;
$obstructed_files[] = trim($line, "! ");
}
$files_str = implode(" ", $obstructed_files);
$cmd1 = "hg revert $files_str";
$cmd2 = "hg rm $files_str";
@runekaagaard
runekaagaard / gist:934612
Created April 21, 2011 14:21
firstset, firstfull, setor, fullor userland implementation.
<?php
// Errors on.
error_reporting(E_ALL);
ini_set('display_errors', 1);
/**
* Returns the first passed argument that is set.
*
* @return mixed
*/
@runekaagaard
runekaagaard / CakePHP 2.0.0-dev
Created April 21, 2011 10:40
Popular PHP frameworks/CMS: isset/empty in ternary operator
Instances of isset:
###################
1 return isset($this->request->params['action']) ? $this->request->params['action'] : '';
2 $this->plugin = isset($request->params['plugin']) ? $request->params['plugin'] : null;
3 $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
4 $newUnique = isset($value['unique']) ? $value['unique'] : 0;
5 $oldUnique = isset($old[$name]['unique']) ? $old[$name]['unique'] : 0;
6 $fields[$key] = isset($matches[4]) ? $matches[2] . '.' . $matches[4] : $matches[1];
7 $default = isset($col['default']) ? $col['default'] : null;

Intro

Isset and IsNotEmpty operators have for sure been a hot topic for several years now and in my opinion rightly so. The non-DRY style of:

$my_array['my_long_boring_key'] = !empty($my_array['my_long_boring_key']) 
              ? $my_array['my_long_boring_key'] : 'Default value';
$my_array['my_long_boring_key'] = isset($my_array['my_long_boring_key']) 
              ? $my_array['my_long_boring_key'] : 'Default value';
local background = display.newImage( "grass.png" )
local screen = {
top = display.screenOriginY,
bottom = display.viewableContentHeight + display.screenOriginY,
left = display.screenOriginX,
right = display.viewableContentWidth + display.screenOriginX,
}
screen.width = screen.right - screen.left