Tuesday, September 3, 2013

Install GeoIP from MaxMind on OS X

I could not find a package ready to use to get running GeoIP from MaxMind on OS X so I built it from sources. To do so, I
  • Downloaded sources
  • Unpacked it tar xvfz GeoIP-latest.tar.gz
  • cd GeoIP-1.5.1
  • ./configure
  • make
  • make check
  • make install
That is it. It's ready to go.


Friday, August 2, 2013

Parse any number string to float

function stringToFloat(str) {
    var fractional_part, integer, fractional = "", found = false, i;
    // remove leading and trailing non-digit symbols
    str = str.replace(/[^\d,\.]+$/, '').replace(/^[^\d,\.]+/, '');
    fractional_part = str.slice(-3);
    for (i = fractional_part.length - 1; i >= 0; i--) {
        if (/\d/.test(fractional_part[i])) {
            fractional = fractional_part[i] + fractional;
        } else {
            found = true;
            break;
        }
    }
    if (fractional && found) {
        integer = str.slice(0, i - 3);
    } else {
        integer = str;
        fractional = "";
    }
    return parseFloat(integer.replace(/[^\d]/g, '') + "." + fractional);
}

Tuesday, July 2, 2013

Delete tags

To delete tags in git that match some regex, both locally and remotely, I use these two commands

# remove tags on origin
git ls-remote --tags  | awk '/tags\/release.*[^\}]$/ {print ":" $2}' | xargs git push origin

# remove locally
git tag -l | awk '/^release.*$/ {print $1}' | xargs git tag -d

Tuesday, May 14, 2013

Increase trackpad speed beyond system settings

If you are not satisfied with your trackpad speed and you've already set it to the maximum in System Settings pane, it could help you.

Go to Terminal and

open ~/Library/Preferences/.GlobalPreferences.plist

You need com.apple.trackpad.scaling. System Preferencies's maximum is 3. From my experience, 30 is insane. I use 15, it's enough to move cursor with one movement from the left side of left screen the right of the right one.

Wednesday, April 24, 2013

Showing an image when dragging any tag in HTML

Chrome and Safari support displaying a ghost image for draggable objects out of the box, Firefox does not. It's not a big deal, I'd say a matter of one event handler.

This is a sample HTML:
Drag Me!

and JavaScript code:
$(function () {
    var dragImage = document.createElement("img");
    dragImage.src = "/image/to/show/when/dragging.jpg";
    function handleDragStart(e) {
        e.dataTransfer.effectAllowed = 'move';
        e.dataTransfer.setDragImage(dragImage, e.layerX, e.layerY);
    }

    $("#draggableObject").get(0).addEventListener('dragstart', handleDragStart, false);
});

Native HTML5 Drag and Drop

MDN::dataTransfer

Monday, April 15, 2013

Line numbers in Vim



I would like to cover all aspects known for me related to the line numbers in Vim.

CommandDescription
:0
move cursor to the first
:42
42gg
42G
move cursor to 42nd
:G
move cursor to the last


Show line numbers in current file
:set number

Hide line numbers in current file
:set nonumber

Tuesday, April 9, 2013

Computed field on TabularInline (StackedInline)

Django admin doesn't provide list_display attribute on its inline admin classes. That's right, since the only supposed view for those classes is editable form. So, to display a result of custom function's call, it needs to add the name of the function (that can be declared either on the admin or model classed) to both fields and readonly_fields.

Example:
class StatsInline(admin.TabularInline):
    model = Stats
    fields = ('clicked', 'shown', 'avg')
    readonly_fields = ('avg',)
    verbose_name = 'Stats'
    verbose_name_plural = 'Stats'
    can_delete = False

    def avg(self, obj):
        return float(obj.clicked) / obj.shown if obj.shown else 0

P.S. float conversion is needed to avoid casting result to int (e.g. 1/2=0, float(1)/2=0.5)