Skip to content

Instantly share code, notes, and snippets.

View rafaelcanovas's full-sized avatar

Rafael Canovas rafaelcanovas

  • Indaband
  • São Paulo, SP - Brazil
  • 03:02 (UTC -03:00)
View GitHub Profile
@rafaelcanovas
rafaelcanovas / closure.js
Created February 16, 2012 18:34
Closure
function fooAdd(x) {
var y = 2;
return function() {
return x + y;
}
}
var fooClosure = fooAdd(5);
alert(fooClosure());
<?php
/**
* Copyright (C) 2012 Rafael Canovas
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
@rafaelcanovas
rafaelcanovas / Preferences.sublime-settings
Last active December 19, 2015 08:29
My Sublime Text preferences
{
"bold_folder_labels": true,
"caret_style": "phase",
"color_scheme": "Packages/Ciapre Color Scheme/CiapreBlack.tmTheme",
"create_window_at_startup": false,
"ensure_newline_at_eof_on_save": true,
"file_exclude_patterns":
[
"*~",
"*.pyc",
@rafaelcanovas
rafaelcanovas / forms.py
Last active March 2, 2021 17:04
Monkey-patching HTML5 required attribute for Django forms.
from django import forms
class HTML5Form(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
for _, field in self.fields.items():
if field.widget.is_required:
field.widget.attrs['required'] = 'required'
@rafaelcanovas
rafaelcanovas / bitly.py
Created October 17, 2013 17:32
bit.ly templatetag for Django
# coding: utf-8
import bitly_api
from django import template
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
register = template.Library()
try:
$ django-admin.py startproject myproject .
$ ./manage.py startapp myapp
$ tree . -d -L 2
.
├── manage.py
├── myapp
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
#!/usr/bin/env python3
from subprocess import call, check_output, CalledProcessError
top_ps = check_output('ps haxo pid,command k -pcpu | head -n 1', shell=True)
top_ps = str(top_ps, encoding='UTF-8')
top_pid, top_cmd = top_ps.strip().split(' ', 1)
try:
question = check_output([
@rafaelcanovas
rafaelcanovas / modals.js
Last active April 20, 2016 19:15
Simple modal windows
(function ($) {
'use strict';
// Compat
MutationObserver = window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;
var body = $(document.body),
modalOverlay = $('<div>', {'id': 'modal-overlay'}).appendTo(body),
@rafaelcanovas
rafaelcanovas / rotate-image.js
Created June 1, 2016 22:54
Javascript utility to rotate image.
function rotateImage(path, degrees, callback) {
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
image = new Image();
image.src = path;
image.onload = function () {
canvas.width = image.height;
canvas.height = image.width;
ctx.translate(image.width/2, image.width/2);
@rafaelcanovas
rafaelcanovas / main.js
Created June 14, 2016 19:29
A widget that shows the current image thumbnail instead of clear/modify controls.
function styleFileUploader(fileInputId) {
var fileInput = $(fileInputId),
addFileField = fileInput.siblings('button');
fileInput.hide();
addFileField.on('click touchstart', function (e) {
fileInput.trigger('click').trigger('touchstart');
});