Skip to content

Instantly share code, notes, and snippets.

View ducin's full-sized avatar
🛠️
creatin' some stuff

Tomasz Ducin ducin

🛠️
creatin' some stuff
View GitHub Profile
@ducin
ducin / ajax-ich.js
Last active December 15, 2015 14:48
ICanHaz.js `grabTemplates()` demo (see more at http://symfony-world.blogspot.com/2013/03/icanhazjs-grabtemplates-example.html). Example shows how you can load additional templates using ajax and reload ICanHaz container to use them. Note that the ajax call should be synchronous.
$(document).ready( function() {
$.ajax({
type: "GET",
dataType: 'text',
async: false,
url: "templates.html"
}).done(function(response) {
$("body").append(response);
ich.grabTemplates();
});
@ducin
ducin / ich-loop.js
Last active December 15, 2015 15:29
ICanHaz loop demo: example showing how to render looped data
$(document).ready( function() {
$('#display').click(function () {
var data = [
{
"name": "Paul McCartney",
"email": "paul.mccartney@beatles.com",
"salary": "400"
},
{
"name": "John Lennon",
@ducin
ducin / concat-ich.sh
Created April 7, 2013 15:27
Concatenate all ICanHaz templates to one single file to make your project both load templates efficient/flexible and easy to maintain for many developers to work within a version control system
#!/bin/bash
path="src/main/web"
> "$path"/templates.ich
for file in "$path"/templates/*
do
cat "$file" >> "$path"/templates.ich
@ducin
ducin / ich-template-loader.js
Created April 7, 2013 15:53
ICanHaz templates loader object - load all your templates after the website has been initially rendered. See http://symfony-world.blogspot.com/2013/04/managing-lots-of-icanhaz-templates.html for details on concatenating template files.
var TemplateLoader = {
path: 'templates/', // template files are stored in this directory
templates: ['file1', 'file2', 'file3'], // put all your template files here
fetchTemplate: function(path) {
$.ajax({
type: 'GET',
dataType: 'text',
async: false,
url: path
}).done(function(response) {
@ducin
ducin / sum_dict_switch.py
Created July 10, 2013 21:12
summing up python dictionary values: add all values, where type='+' should be added, type='-' should be subtracted and other type should be ignored
# define entries
entries = [{'type': '+', 'value': 9},\
{'type': '+', 'value': 12.3},\
{'type': '-', 'value': 4},\
{'type': '+', 'value': 0.89},\
{'type': '-', 'value': 2.65},\
{'type': '-', 'value': 14.3},\
{'type': '+', 'value': 6.8},\
{'type': 'unknown', 'value': 200}]
@ducin
ducin / time_clock.py
Created July 10, 2013 21:28
benchmarking/timing python instructions
import time
start = time.clock()
for x in range(1,10000):
y = x**3
elapsed = time.clock() - start
print elapsed
@ducin
ducin / bind.js
Last active December 20, 2015 10:30
binding "this" object to a function
// binding "this" object only (function arguments are ignored)
if (!Function.prototype.bind) {
Function.prototype.bind = function bind(thisObject) {
var fun = this;
return function() {
fun.apply(thisObject, arguments);
};
};
}
@ducin
ducin / bind.js
Created July 30, 2013 18:47
binding both "this" object and optionally arguments
// binding both "this" object and optionally arguments
if (!Function.prototype.bind) {
Function.prototype.bind = function bind(thisObject) {
var fun = this, boundArgs = Array.prototype.slice.call(arguments, 1);
return function() {
var allArgs = boundArgs;
if (arguments.length) {
allArgs = allArgs.concat(Array.prototype.slice.call(arguments));
}
@ducin
ducin / knotify.py
Created August 4, 2013 21:53
KNotify (KDE DBUS notifications); use it with: ./knotify.py "header" "content" (credited to http://www.documentroot.net/en/linux/knotify-python)
#!/usr/bin/python
import sys, dbus
knotify = dbus.SessionBus().get_object("org.kde.knotify", "/Notify")
try: title, text = sys.argv[1:3]
except: print 'Usage: knotify.py title text'; sys.exit(1)
knotify.event("warning", "kde", [], title, text, [], [], 0, 0, dbus_interface="org.kde.KNotify")
@ducin
ducin / curry.js
Created August 6, 2013 07:28
currying is a mechanism to pre-fill arguments of a later function call
if (!Function.prototype.curry) {
(function () {
var slice = Array.prototype.slice;
Function.prototype.curry = function () {
var target = this;
var args = slice.call(arguments);
return function () {
var allArgs = args;