Skip to content

Instantly share code, notes, and snippets.

View nitely's full-sized avatar
🟢
online

Esteban C Borsani nitely

🟢
online
View GitHub Profile
@nitely
nitely / admin.py
Created February 5, 2014 02:30
Group permissions selection in admin filtered by your app models
"""
Sometimes you just don't want to display every permission django has,
you just want a short list showing the permissions for some of your apps (or even django core apps).
GROUP_PERMISSIONS_MODELS must be a list of your app models.
Dotted path (in lowercase) required, app name + model class name.
tested on Django 1.6 (should work on +1.3)
"""
@nitely
nitely / pretty_timezones.py
Created June 16, 2014 22:12
Grouped pytz timezones
"""
Result:
TIMEZONE_CHOICES = [
("Africa", [
("Africa/Abidjan", "Abidjan"),
("Africa/Accra", "Accra"),
#...
]),
("America", [
@nitely
nitely / jasmine-ajax-spec.coffee
Last active August 29, 2015 14:03
jasmine 2.0 test ajax/post/getJSON/etc in jQuery
describe "post tests", ->
post = null
beforeEach ->
#jasmine-jquery
#fixtures = do jasmine.getFixtures
#fixtures.fixturesPath = 'base/test/fixtures/'
#loadFixtures 'bookmark.html'
post = spyOn $, 'post'
@nitely
nitely / download.js
Created December 11, 2014 02:29
node-downloaderJS using coroutines and generators
/* This is just a cool example on how to write coros in javascript Ecma6.
* Licence: MIT
* Author: esteban castro borsani
*/
var http = require('http');
var fs = require('fs');
var path = require('path');
var Q = require("q");
var co = require("co");
@nitely
nitely / json.js
Last active August 29, 2015 14:16
First steps: JSON Schema validator
/*
Author: Esteban Castro Borsani
Licence: MIT
*/
/*
This is a schema validator... errrgh.... this is the first step to create a json validator.
It does not do any validation, but it iterates over nested objects and arrays defined in your schema, recording the full path for error outputs.
REQUIRES: lodash
@nitely
nitely / django_compilemessages.py
Last active August 29, 2015 14:25
Django build .mo files for every app
if __name__ == '__main__':
import os
from subprocess import call
BASE = os.getcwd()
for root, dirs, files in os.walk("./spirit"):
if 'locale' not in dirs:
continue
@nitely
nitely / file_hash.py
Created August 31, 2015 09:07
Django hash from file
import hashlib
def get_hash(file):
md5 = hashlib.md5()
for c in file.chunks():
md5.update(c)
return md5.hexdigest()
@nitely
nitely / weak_ref.py
Created October 22, 2012 15:18
Python, Bound Method Weakref workaround
import weakref
class _BoundMethodWeakref:
def __init__(self, func):
self.func_name = func.__name__
self.wref = weakref.ref(func.__self__) #__self__ returns the class http://docs.python.org/reference/datamodel.html
def __call__(self):
func_cls = self.wref()
@nitely
nitely / event.py
Created October 29, 2012 12:26
Python, A event/signal dispatcher
import threading
#import logging
#logger = logging.getLogger(__name__)
import idle_queue
from weak_ref import weak_ref
class Event:
def __init__(self, name):
@nitely
nitely / merge.py
Last active November 29, 2015 19:37
Merge dicts
# Untested
def merge(dict_a, dict_b, **kwargs):
assert isinstance(dict_a, dict), \
"A dict was expected in %s" % kwargs.pop('node_name', 'first param')
for k, v in dict_b.items():
if isinstance(v, dict):
merge(dict_a.setdefault(k, {}), v, node_name=k)
continue