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 / MultipleFieldLookupORMixin.py
Last active May 7, 2024 10:47
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 = {}
@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 / 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 / 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)
# 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 / 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.
@nik-hil
nik-hil / Resize_image.py
Created September 13, 2018 13:55
resize image in python from one location to another. Not a clean one
from pathlib import Path
from tqdm import tqdm
import pydicom
import numpy as np
from PIL import Image
sz=224
PATH = Path('big')
img_dir = PATH/'image'
TEST = False
@nik-hil
nik-hil / alpha.py
Created October 19, 2018 09:25
plot image with alpha, with mask
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.patches import Rectangle
import pydicom
import numpy as np
import torchvision as tv
img = pydicom.dcmread('/Users/nikhil/workspace/src/rsna/dcm/00aecb01-a116-45a2-956c-08d2fa55433f.dcm').pixel_array
img = np.expand_dims(img, -1)
img_pil = tv.transforms.functional.to_pil_image(img)
@nik-hil
nik-hil / cmd.sh
Created October 27, 2018 07:49
small useful bash commands
# see gpu usage
nvidia-smi
# see gpu usage every 10 sec
watch -n 10 nvidia-smi
@nik-hil
nik-hil / Google Colab SSH
Created July 4, 2019 04:46 — forked from yashkumaratri/Google Colab SSH
SSH into google colab
#CODE
#Generate root password
import random, string
password = ''.join(random.choice(string.ascii_letters + string.digits) for i in range(20))
#Download ngrok
! wget -q -c -nc https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
! unzip -qq -n ngrok-stable-linux-amd64.zip
#Setup sshd