Skip to content

Instantly share code, notes, and snippets.

View runekaagaard's full-sized avatar

Rune Kaagaard runekaagaard

  • Copenhagen, Denmark
View GitHub Profile
@runekaagaard
runekaagaard / django-locking-fixes
Created March 4, 2011 11:42
Fixes some bugs in the django-locking module.
Index: locking/admin.py
===================================================================
--- locking/admin.py (revision 6319)
+++ locking/admin.py (working copy)
@@ -1,42 +1,26 @@
# encoding: utf-8
+import os
from datetime import datetime
-
@runekaagaard
runekaagaard / Sketch for history app
Created March 5, 2011 16:51
The other revision apps for django don't work well enough. NIH!
from django.db import models as m
from tesla.models import Invention
class _DUMMY(object):
pass
def get_model_dict(model):
def get_foreign_key_key(model):
return '%s_%s' % (
model.__module__.replace('.models', '').replace('.', '_'),
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import math
import pyglet
from pyglet import image, font
from pyglet.gl import *
from pyglet.window import key, Window
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

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';
@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;
@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 / 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 / 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 / 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