Skip to content

Instantly share code, notes, and snippets.

View nik-hil's full-sized avatar
🏠
Working from home

Nikhil nik-hil

🏠
Working from home
View GitHub Profile
@nik-hil
nik-hil / shuffle.sh
Created September 1, 2018 10:20
bash script to shuffle and copy
https://unix.stackexchange.com/a/217720
shuf -zn<count> -e *.<file_ext_in_curnt_dir> | xargs -0 cp -vt <target_dir>
shuf -zn8 -e *.jpg | xargs -0 cp -vt target/
shuf shuffles the list of *.jpg files in the current directory.
-z is to zero-terminate each line, so that files with special characters are treated correctly.
-n8 exits shuf after 8 files.
xargs -0 reads the input delimited by a null character (from shuf -z) and runs cp.
# copied from kaggle microsoft malware classification problem discussion forum
import sys
import os
from math import log, pow
import numpy as np
import scipy as sp
from PIL import Image
import matplotlib.pyplot as plt
def saveimg(array,name):
@nik-hil
nik-hil / workflows-in-django.md
Created February 17, 2016 09:08 — forked from Nagyman/workflows-in-django.md
Workflows in Django

Workflows (States) in Django

I'm going to cover a simple, but effective, utility for managing state and transitions (aka workflow). We often need to store the state (status) of a model and it should only be in one state at a time.

Common Software Uses

  • Publishing (Draft->Approved->Published->Expired->Deleted)
@nik-hil
nik-hil / fabric deployment script
Created November 24, 2015 10:11
Fabric Script to deploy code on django+uwsgi+nginx on aws server
from __future__ import with_statement
from fabvenv import virtualenv
from fabric.api import *
from fabric.contrib.console import confirm
# http://stackoverflow.com/questions/1180411/activate-a-virtualenv-via-fabric-as-deploy-user
# http://stackoverflow.com/questions/30964502/python-fabric-nested-roles
@nik-hil
nik-hil / aws_sg_recipe.py
Created November 24, 2015 10:04 — forked from steder/aws_sg_recipe.py
Create and update AWS security groups using Python and Boto.
#!/usr/bin/env python
"""
Recipe for creating and updating security groups programmatically.
"""
import collections
import boto
@nik-hil
nik-hil / MultipleFieldLookupORMixin.py
Last active March 25, 2025 11:02
Django REST framework support only one lookup field at a time. To support more than one field we have to use MultipleFieldLookupMixin. But it has a limitation. It require all fields to be present in URL. I have different requirement. I can call view using two differnet identifier.
class MultipleFieldLookupORMixin(object):
"""
Actual code http://www.django-rest-framework.org/api-guide/generic-views/#creating-custom-mixins
Apply this mixin to any view or viewset to get multiple field filtering
based on a `lookup_fields` attribute, instead of the default single field filtering.
"""
def get_object(self):
queryset = self.get_queryset() # Get the base queryset
queryset = self.filter_queryset(queryset) # Apply any filter backends
filter = {}