Skip to content

Instantly share code, notes, and snippets.

View aljiwala's full-sized avatar
☯️
Present.

Mohsin aljiwala

☯️
Present.
View GitHub Profile
@aljiwala
aljiwala / admin.py
Created October 27, 2016 09:56
Unicode usernames in Django admin panel
"""
This code aims to override the username's regex of the Django admin panel to be
able to allow unicode usernames.
Just add this code to an app as admin.py and everything will be done.
"""
import re
from django import forms
from django.contrib import admin
@aljiwala
aljiwala / read_related_notifications.py
Created October 27, 2016 10:40
Mark Notification as Read automatically after visiting that URL!
@aljiwala
aljiwala / bin_packing.py
Created November 4, 2016 07:30
Bin Packing Algorithm
''' Partition a list into sublists whose sums don't exceed a maximum
using a First Fit Decreasing algorithm.
'''
class Bin(object):
''' Container for items that keeps a running sum '''
def __init__(self):
self.items = []
self.sum = 0
@aljiwala
aljiwala / get_pip.py
Created November 7, 2016 10:34
Get Pip (Python)
This file has been truncated, but you can view the full file.
#!/usr/bin/env python
#
# Hi There!
# You may be wondering what this giant blob of binary data here is, you might
# even be worried that we're up to something nefarious (good for you for being
# paranoid!). This is a base85 encoding of a zip file, this zip file contains
# an entire copy of pip.
#
# Pip is a thing that installs packages, pip itself is a package that someone
# might want to install, especially if they're looking to run this get-pip.py
@aljiwala
aljiwala / linked_list.py
Created November 29, 2016 05:51
Linked List Implementation (Python)
class Node :
def __init__( self, data ) :
self.data = data
self.next = None
self.prev = None
class LinkedList :
def __init__( self ) :
self.head = None
@aljiwala
aljiwala / app.js
Last active December 3, 2016 20:44
Google Maps/Places Autocomplete (AngularJS Directive)
var myApp = angular.module('myApp', []);
myApp.directive('googleplace', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, model) {
var options = {
types: [],
componentRestrictions: {}
};
@aljiwala
aljiwala / traverse.py
Last active December 23, 2016 09:46
Traverse Generator Function in Python
def traverse(o, tree_types=(list, tuple)):
if isinstance(o, tree_types):
for value in o:
for subvalue in traverse(value, tree_types):
yield subvalue
else:
yield o
data = [(1,1,(1,1,(1,"1"))),(1,1,1),(1,),1,(1,(1,("1",)))]
print list(traverse(data))
@aljiwala
aljiwala / time_celery_task.py
Created January 6, 2017 14:46
Log execution time for every Celery task (Celery 3.1.x)
import logging
import time
from celery import shared_task as orig_shared_task
def timeit(func):
"""A decorator used to log the function execution time."""
logger = logging.getLogger('tasks')
@aljiwala
aljiwala / celery.sh
Created January 4, 2017 14:08
Basic Celery Useful Commands
/* Useful celery config.
app = Celery('tasks',
broker='redis://localhost:6379',
backend='redis://localhost:6379')
app.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
CELERY_QUEUES=(
Queue('default', routing_key='tasks.#'),
@aljiwala
aljiwala / s3_meta.py
Created February 24, 2017 21:02
Script to update the metadata on Amazon S3
"""
To run the script
s3_meta.py --access-key=AWS_ACCESS_KEY_ID
--secret-key=AWS_SECRET_ACCESS_KEY
--bucket-name=BUCKET_NAME
-d=METADATA
e.g.
python s3_meta.py --access_key=AKIAIOSFODNN7EXAMPLE --secret-key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY --bucket-name=test-bucket -d '{"Cache-Control": "max-age=3600"}'
"""