Skip to content

Instantly share code, notes, and snippets.

@marcoala
marcoala / pre-commit
Last active December 11, 2018 15:46
Precommit hook for a python project
#!/bin/sh
# colors for pretty output
RED="\033[00;31m"
BLUE="\033[00;34m"
GREEN="\033[00;32m"
RESET=$(tput sgr0)
echo "Linting python files with flake8"
LINTER="$(git diff --name-only --cached | grep .py | xargs python3 -m flake8 --config /Users/marco/Code/development/festicket/setup.cfg {})"
@marcoala
marcoala / multi_thread.js
Created January 15, 2018 12:23
Demonstration of how setTimeout and multi-thread work in JS. Read the comment for more details.
// original reference https://davidwalsh.name/es6-generators
/**
* This function create a timeout that should be executed after 1 millisecond
*/
function timeoutFunction() {
setTimeout(function() {
console.log('Timeout');
}, 1);
}
@marcoala
marcoala / DefaultKeyBinding.dict
Created February 24, 2017 10:50
This allow to remap the `home` and `end` buttons on mac to behave like in Windows. Save this file in ~/Library/KeyBindings/DefaultKeyBinding.dict
{
/* Remap Home / End keys to be correct */
"\UF729" = "moveToBeginningOfLine:"; /* Home */
"\UF72B" = "moveToEndOfLine:"; /* End */
"$\UF729" = "moveToBeginningOfLineAndModifySelection:"; /* Shift + Home */
"$\UF72B" = "moveToEndOfLineAndModifySelection:"; /* Shift + End */
"^\UF729" = "moveToBeginningOfDocument:"; /* Ctrl + Home */
"^\UF72B" = "moveToEndOfDocument:"; /* Ctrl + End */
"$^\UF729" = "moveToBeginningOfDocumentAndModifySelection:"; /* Shift + Ctrl + Home */
"$^\UF72B" = "moveToEndOfDocumentAndModifySelection:"; /* Shift + Ctrl + End */
@marcoala
marcoala / js-interview-question.js
Last active January 24, 2017 14:21
Ask the candidate what s/he can say about this code, and what the output is.
function createFunctions() {
var i;
var functions = [];
for (i = 0; i < 5; i++){
functions[i] = function(){
console.log(i);
};
}
@marcoala
marcoala / .bash_profile
Last active December 19, 2018 15:04
Some line of my .bash_profile
# virtualenvwrapper
export WORKON_HOME=~/.vienvs
source /usr/local/bin/virtualenvwrapper.sh
export PATH=/Users/$USER/Library/Python/2.7/bin/:/usr/local/bin:$PATH
# nicer prompt
export PS1='\u@\h:\[\033[36;2m\]\w\[\033[m\]\$ '
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
@marcoala
marcoala / .gitconfig
Last active December 19, 2018 15:06
Some line of my .gitconfig file
[user]
name = Marco Alabruzzo
email = marco.alabruzzo@gmail.com
# signingkey = KEY_HERE
[merge]
conflictstyle = diff3
[push]
default = simple
[alias]
unadd = reset HEAD
@marcoala
marcoala / command_name.py
Last active May 23, 2021 16:48
Base structure of a django management command, use transaction to rollback every change if an exception is raised and track time of execution.
import sys
import traceback
from django.utils import timezone
from django.db import transaction
from django.core.management.base import BaseCommand, CommandError
# django will strip new lines split helptext in 80 char lines
HELP_TEXT = """
@marcoala
marcoala / forms.py
Last active January 4, 2017 17:22
A small class for django form that allow to create Form field from Model when you are not using a ModelForm
class FieldBuilder(object):
"""Return the default form field for a model field. This field will be validated oin the same
way as the form field would be on a ModelForm.
Optionally, extra kwargs can be added to override what the form field is initialized with.
So this:
class ExampleForm(forms.ModelForm):
class Meta:
model = ExampleModel
@marcoala
marcoala / main.js
Created November 29, 2016 18:23
jQuery - listen for event
//listen on event on an element that is in the DOM when the page load
$('#my_element').change(function(e){
//do stuff
});
//listen on event on an element that is NOT in the DOM when the page, formerly .live()
$(document).on( 'change', '#my_element', function(e) {
//do stuff
});