Skip to content

Instantly share code, notes, and snippets.

Avatar

Reimund Järnfors reimund

View GitHub Profile
@reimund
reimund / model.js
Last active Jan 4, 2016
Model base class. Features inheritance, automatically created accessors (get + set) and encapsulation of data. Setters are chainable.
View model.js
var Model = function(attrs)
{
this.attrs = attrs;
for (var attr in attrs)
var tmp = function(name) {
this[attr] = function(x) {
if (arguments.length) {
this.attrs[name] = x;
return this;
@reimund
reimund / index.html
Created Nov 26, 2013
D3.js: Animate path point by index using custom interpolator.
View index.html
<!DOCTYPE html>
<script src="http://mbostock.github.com/d3/d3.v2.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<style>path { stroke: steelblue; stroke-width: 1; fill: none; }</style>
<body>
<script>
$(window).load(function() {
// Animate between these two straight lines.
var d0 = [[
@reimund
reimund / dict2xml.php
Last active Dec 16, 2015
Php port of my xml serializer.
View dict2xml.php
<?php
function dict2xml($d, $root_node=null, $use_attr=true)
{
$wrap = (null == $root_node or is_plain_array($d)) ? false : true;
$root = (null == $root_node) ? 'objects' : $root_node;
$root_singular = ('s' == $root[strlen($root) - 1] and null == $root_node)
? substr($root, 0, strlen($root) - 1)
: $root;
$xml = '';
$end_tag = '';
@reimund
reimund / dict2xml.py
Last active Nov 17, 2021
Simple dictionary to xml serializer.
View dict2xml.py
"""
Simple xml serializer.
@author Reimund Trost 2013
Example:
mydict = {
'name': 'The Andersson\'s',
'size': 4,
@reimund
reimund / settings.py
Last active Dec 15, 2015
Class based settings for Django. Set all common settings in BaseSettings, then create a subclass for each environment. Uses the environment variable DJANGO_ENV in order to decide which subclass it should use. I named the package "zettings" rather than "settings" to avoid import problems.
View settings.py
""" settings.py """
import sys, os
from zettings.base import *
sys.path.append(os.getcwd() + '/../')
ENV = os.environ.get('DJANGO_ENV', 'localdev')
# Import the right settings for this environment.
# DJANGO_ENV should be set to the name of a module which contains a
@reimund
reimund / UsefulListView.py
Created Mar 22, 2013
Allows passing extra_context to ListView.as_view().
View UsefulListView.py
from django.views.generic.list import ListView
class UsefulListView(ListView):
""" Allows passing extra_context to ListView.as_view(). """
extra_context = {}
def get_context_data(self, **kwargs):
context = super(UsefulListView, self).get_context_data(**kwargs)
context.update(self.extra_context)