Archive for the ‘code’ Category

Python Cheatsheet (v 2.7)

Tuesday, March 29th, 2016

Here’s a few quick references for python developers.

Here are some python tips & tricks that can be useful for beginners.

Merge tuples

t1 = ('a','b','c') 
a1 = ['j','k'] 
result = t1+tuple(a1)
# result == ('a','b','c','j','k')

# note here I'm converting a list into tuple right away - so you can mix/match tuples/lists this way

(more…)

Time your PHP functions

Tuesday, June 22nd, 2010

this code will let you time your function in microseconds, am too lazy to write more comments, but you can play around.
have fun.

(more…)

Recursively parse a directory and mark new or recently changed directories

Sunday, June 20th, 2010

This script is particularly useful if you’re building any kind of web app that requires directory tree with sub directories/files and requires to mark directories containing new files or with files recently removed as such.
I also wrote this function for my gallery

This specific example grabs only jpegs

(more…)

.clonePosition for $jQuery (from prototype.js)

Sunday, June 20th, 2010

So I was converting my gallery to jQuery. But I found that $jq doesn’t have the nifty .clonePosition() that Prototype does. so here’s a little plugin:
(save it as jquery.cloneposition.js and do a standard include)

(function($){
  $.fn.clonePosition = function(element, options){
	var options = $.extend({
		setLeft : true,
		setTop  : true,
		setWidth: false,
		setHeight: false,
		offsetLeft: 0,
		offsetTop: 0
	}, (options || {}));

	var el = $(this); //what to position
	var src = $(element); //where to position
	var offsets = el.offset();
	var p = [src.offset().left, src.offset().top];
	var delta = [0, 0];
	//difference in size between element and source (to align centers, not top-left corners)
	var delta_size =[0, 0];
	var parent = null;

	if(el.css('position') == 'absolute') {
		d = el.offset();//relative to the parent
		delta[0]=d.left;
		delta[1]=d.top;
	}
	//use outer width to account for borders and padding
	delta_size[0] = (el.outerWidth()-src.outerWidth())/2;
	delta_size[1] = (el.outerHeight()-src.outerHeight())/2;

	if(parent == document.body){
		delta[0] -= document.body.offsetLeft;
		delta[1] -= document.body.offsetTop;
	}

	if(options.setLeft)   el.css('left',(p[0] - delta[0] + options.offsetLeft - delta_size[0]) + 'px');
	if(options.setTop)    el.css('top', (p[1] - delta[1] + options.offsetTop - delta_size[1]) + 'px');
	if(options.setWidth)  el.width(src.width() + 'px');
	if(options.setHeight) el.height(src.height() + 'px');
	
	return el;
  }
})(jQuery);

Download here