Skip to content

Instantly share code, notes, and snippets.

View uppfinnarjohnny's full-sized avatar

Johnny Karhinen uppfinnarjohnny

View GitHub Profile
@uppfinnarjohnny
uppfinnarjohnny / ActiveCollab.php
Created December 5, 2011 16:01
Simple API wrapper for ActiveCollab
<?php
class ActiveCollab {
protected $base_url;
protected $api_key;
public function __construct($base_url, $api_key) {
$this->base_url = $base_url;
$this->api_key = $api_key;
}
@uppfinnarjohnny
uppfinnarjohnny / php.php
Created November 24, 2011 14:20
Something about lists, references and stuff, in php & python
$some_array[$some_id]['key_one'] = $value_one;
$some_array[$some_id]['key_two'] = $value_two;
$some_array[$some_id]['key_three'] = $value_three;
@uppfinnarjohnny
uppfinnarjohnny / admin.py
Created November 14, 2011 12:35
Django app for internal aliases
from alias.models import Alias
from django.contrib import admin
class AliasAdmin(admin.ModelAdmin):
list_display = ('path', 'target')
admin.site.register(Alias, AliasAdmin)
@uppfinnarjohnny
uppfinnarjohnny / pick_by_keys.py
Created October 6, 2011 19:03
pick_by_keys()
def pick_by_keys(d, ks):
return dict((k, v) for k, v in d.iteritems() if k in ks)
d = {
'hello': 'world',
'foo': 'bar',
'romeo': 'juliet',
'alice': 'bob'
}
@uppfinnarjohnny
uppfinnarjohnny / gist:1051819
Created June 28, 2011 18:31
latest_video()
<?php
function latest_video($id_only = False) {
$url = trim(file_get_contents('/home/cyklarunt/latest'));
return $id_only ? end(explode('=', $url)) : $url;
}
@uppfinnarjohnny
uppfinnarjohnny / get_google_spreadsheet.php
Created March 16, 2011 11:11
A simple way of getting the data from a Google Docs Spreadsheet in PHP
<?php
function get_google_spreadsheet($key) {
$url = "https://spreadsheets.google.com/feeds/list/{$key}/od6/public/values";
$google_sheet = file_get_contents($url);
$xml = simplexml_load_string($google_sheet);
$data = array();
foreach($xml->entry as $entry) {
$row = array();
// The fields are in the gsx: namespace, so we need to specify that to be able to access them through SimpleXML.
@uppfinnarjohnny
uppfinnarjohnny / index.php
Created March 12, 2011 17:37
Tok-simpel MVC-struktur
<?php
include('posts_model.php');
$posts = get_posts();
include('view.php');
<?php
function word_pairs($string, $min_matches = 1) {
$pair_counts = array();
$words = explode(' ', $string);
$previous_word = FALSE;
foreach($words as $word) {
if($previous_word)
$pair_counts[$previous_word.' '.$word]++;
$previous_word = $word;
}
@uppfinnarjohnny
uppfinnarjohnny / caesar_cipher.php
Created December 23, 2010 13:20
A basic implementation of a Caesar cipher
<?php
function rollover($max, $value) {
return $value < $max ? $value : rollover($max, $value - $max);
}
function caesar_cipher($string, $offset = 3) {
$letter_numbers = array_map('ord', str_split(strtolower($string)));
foreach($letter_numbers as &$number)
if($number >= 97 && $number <= 122)
$number = rollover(25, $number - 97 + $offset) + 97;
<?php
function swedish_date($date = NULL) {
if(is_null($date))
$date = time();
elseif( ! is_numeric($date))
$date = strtotime($date);
set_locale(LC_TIME, 'sv_SV');
return strftime('%A den %e %B %Y' , $date);
}