Skip to content

Instantly share code, notes, and snippets.

View odyniec's full-sized avatar

Michał Wojciechowski odyniec

View GitHub Profile
@odyniec
odyniec / gist:3454084
Created August 24, 2012 18:33
Using a get valHook in a jQuery plugin
(function ($) {
// jQuery plugin definition
$.fn.bytesInput = function () {
$(this).filter('input[type="text"]').each(function() {
$(this).data('bytesInput', true);
});
return this;
};
var origHook;
@odyniec
odyniec / gist:3470977
Created August 25, 2012 21:04
Autocropping a transparent image in Python using PIL
#!/usr/bin/env python
import sys
import Image
def autocrop_image(image, border = 0):
# Get the bounding box
bbox = image.getbbox()
# Crop the image to the contents of the bounding box
@odyniec
odyniec / gist:3734249
Created September 16, 2012 20:27
Iterating over sorted properties of an object (JavaScript 1.8.5)
object = { foo: 42, bar: 43, baz: 44 };
Object.keys(object).sort().every(function (key) {
console.log(key + ' -> ' + object[key]);
// Keep going
return true;
});
@odyniec
odyniec / fizzbuzz.pl
Created December 16, 2012 13:58
Perl oneliner solution to the FizzBuzz problem (http://rosettacode.org/wiki/FizzBuzz)
say+(Fizz)[++$_%3].Buzz x/.?[50]$/||$_ until$`
@odyniec
odyniec / identify-browser.js
Created January 21, 2013 23:29
A JavaScript function that extracts the browser name and version number from user agent string. Recognized browsers: Firefox, Internet Explorer, Opera, Chrome, and Safari.
/**
* Extracts the browser name and version number from user agent string.
*
* @param userAgent
* The user agent string to parse. If not specified, the contents of
* navigator.userAgent are parsed.
* @param elements
* How many elements of the version number should be returned. A
* value of 0 means the whole version. If not specified, defaults to
* 2 (major and minor release number).
@odyniec
odyniec / pace.sh
Created January 25, 2013 00:46
A Perl oneliner (with a shell alias) that calculates the average running pace based on distance and time.
# A Perl oneliner (with a shell alias) that calculates the average running pace based on distance and time
# Examples:
# pace 5 28:05 # 5 kilometres, 28 minutes and 5 seconds
# pace 10.5 45:28 # 10.5 kilometres, 45 minutes and 28 seconds
# pace 42.195 2:03:38 # Marathon, 2 hours, 3 minutes and 38 seconds (current WR)
alias pace="perl -e 'map{\$t+=\$_*60**\$i++}reverse split/:/,pop;\$t/=pop;printf\"%d:%02d\n\",\$t/60,\$t%60'"
@odyniec
odyniec / tempdir.pl
Last active February 11, 2022 13:36
An example Perl program that uses File::Temp to create a temporary directory for the time of program execution.
#!/usr/bin/env perl
use Cwd;
use File::Temp;
# Remember the current directory
my $oldcwd = getcwd;
# Create a temporary directory
my $dir = File::Temp->newdir;
# Go to the temporary directory
@odyniec
odyniec / moo-attributes.pl
Created May 13, 2014 00:09
An example Perl program that shows how to enumerate all attributes of a Moo object.
#!/usr/bin/env perl
use warnings;
use strict;
{
package Person;
use Moo;
@odyniec
odyniec / test_temp_directory.py
Last active August 29, 2024 17:19
An example Python unittest test case that creates a temporary directory before a test is run and removes it when it's done.
import shutil, tempfile
from os import path
import unittest
class TestExample(unittest.TestCase):
def setUp(self):
# Create a temporary directory
self.test_dir = tempfile.mkdtemp()
def tearDown(self):
@odyniec
odyniec / img_identical.py
Created July 14, 2014 21:03
Comparing two graphics files in Python (using PIL)
from PIL import Image, ImageChops
def img_identical(file1, file2):
"""
Take two image file names and return a boolean value indicating if the
images are identical.
"""
im1 = Image.open(file1)
im2 = Image.open(file2)
return ImageChops.difference(im1, im2).getbbox() is None