Skip to content

Instantly share code, notes, and snippets.

@vinaychittora
vinaychittora / admin_download_xls.py
Last active December 27, 2015 15:58
Generic admin action to export data into excel/csv
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse, HttpResponseRedirect
from pyExcelerator import *
from django.contrib.admin.util import lookup_field
from django.utils.html import strip_tags
from django.contrib import messages
def export_as_xls(modeladmin, request, queryset):
"""
@vinaychittora
vinaychittora / get_postal_code.py
Last active January 1, 2016 17:59
This is a short python snippet to find the address detail and it's postal code using pygeocoder lib
"""
AUTHOR - Vinay Chittora
This api is written under pygeocoder
Google Maps api-v3
INSTALLATION
------------------------------------------------------
$ pip install pygeocoder
class CPCommonQuerySet(QuerySet, CPCommonMixin):
def _filter_or_exclude(self, negate, *args, **kwargs):
cust_lookups = []
if kwargs.items():
if kwargs.items()[0][0].endswith('__false'):
negate = True
cust_lookups = filter(lambda s: s[0].endswith('__false'), kwargs.items())
elif kwargs.items()[0][0].endswith('__true'):
negate = False
cust_lookups = filter(lambda s: s[0].endswith('__true'), kwargs.items())
@vinaychittora
vinaychittora / decision_tree.py
Last active August 29, 2015 13:59
Decision Tree Using SciKit
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.externals.six import StringIO
import os
import pydot
import csv
import re
class LoadData():
def __init__(self):
@vinaychittora
vinaychittora / imdb.py
Created April 24, 2014 13:10
To fetch summary of any movie from IMDB
import urllib2
from BeautifulSoup import BeautifulSoup
from mechanize import Browser
import re
def getunicode(soup):
body=''
if isinstance(soup, unicode):
soup = soup.replace("'","'")
soup = soup.replace('"','"')
# -*- coding: utf-8 -*-
"""Add permissions for proxy model.
This is needed because of the bug https://code.djangoproject.com/ticket/11154
in Django (as of 1.6, it's not fixed).
When a permission is created for a proxy model, it actually creates if for it's
base model app_label (eg: for "article" instead of "about", for the About proxy
model).
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
/* Reset
--------------------------------------------------------------------------------------- */
html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code,
del, dfn, font, img, ins, kbd, q, s, samp, small, strong, em, strike, sub, sup, tt, var, dl, dt, dd, ol, ul, li, fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td { margin: 0; padding: 0; border: 0; outline: 0; font-weight: normal; font-style: normal; font-size: 100%; font-family: inherit; vertical-align: baseline; list-style-type: none;}
import requests
import urllib
import time
import hmac
import random
import base64
from hashlib import sha1
import string
import pymongo
import urlparse