Skip to content

Instantly share code, notes, and snippets.

View chhantyal's full-sized avatar
💭
🤷‍♂

Nar Kumar Chhantyal chhantyal

💭
🤷‍♂
View GitHub Profile
@chhantyal
chhantyal / oop.js
Created June 6, 2012 11:05
JS OOP
function Subject(name, topics){
this.name = name;
this.topics = topics;
}
var subjectMath = new Subject("Maths", ["sets", "trigonomitry", "algebra"]);
var subjectScience = new Subject("Science", ["Biology", "Astronomy", "Physics"]);
function Person(gender, age, currentSubject, currentTopicIndex){
this.gender = gender;
@chhantyal
chhantyal / oop.js
Last active October 5, 2015 21:38
Concept of OOP in Javascript
function Subject(name, topics){
this.name = name;
this.topics = topics;
}
var subjectMath = new Subject("Maths", ["sets", "Trigonometry", "algebra"]);
var subjectScience = new Subject("Science", ["Biology", "Astronomy", "Physics"]);
function Person(name, gender, age, currentSubject, currentTopicIndex){
this.name = name;
@chhantyal
chhantyal / file_download.py
Created February 7, 2013 20:04
taken from http://bit.ly/XT7ban for easy forking.
import urllib2 # needed for functions,classed for opening urls.
url = raw_input( "enter the url needed for downloading file(pdf,mp3,zip...etc)\n");
usock = urllib2.urlopen(url) #function for opening desired url
file_name = url.split('/')[-1] #Example : for given url "www.cs.berkeley.edu/~vazirani/algorithms/chap6.pdf" file_name will store "chap6.pdf"
f = open(file_name, 'wb') #opening file for write and that too in binary mode.
file_size = int(usock.info().getheaders("Content-Length")[0]) #getting size in bytes of file(pdf,mp3...)
print "Downloading: %s Bytes: %s" % (file_name, file_size)
downloaded = 0
@chhantyal
chhantyal / constructor.py
Created February 25, 2013 19:50
A python constructor.
class Color(object):
'''An RGB color, with red, green and blue components.'''
def __init__(self, r, g, b):
'''A new color with red value r, green value g, and blue value b. All components are integers in the range 0-255.'''
self.red = r
self.green = g
self.blue = b
@chhantyal
chhantyal / get_upload_path.py
Last active May 31, 2019 18:32
get dynamic upload path and slugify in Django
from django.db import models
from django.utils.text import slugify
class Document(models.Model):
def get_upload_path(self, filename):
"""
Returns the upload path for docs
"""
filename = filename.split('.')
extension = filename.pop()
@chhantyal
chhantyal / embed_youtube_template_tag.py
Last active April 10, 2019 21:07
A normal Youtube url, saved in database is not enough to embed videos on app. This django template tag takes url, and returns embed url.
import urlparse
from django import template
register = template.Library()
def video_embed(context, url):
url_data = urlparse.urlparse(url)
query = urlparse.parse_qs(url_data.query)
try:
video_id = query["v"][0]
@chhantyal
chhantyal / month_from_number.py
Created July 23, 2013 15:55
Get month from a month number in Django like 1 => Jan.
# in templatetags/app_tags.py
import calendar
from django import template
register = template.Library()
@register.filter
def month_name(month_number):
return calendar.month_name[month_number]
def change_keys(row_dict):
"""
It returns a dictionary with keys in English and all lowercase.
Create a dictionary to map keys to given dictionary keys.
Following is Dutch to English text mapping.
"""
dict_map = {
'Leerlingnummer': 'student_id',
'Voornamen': 'first_name',
'Achternaam': 'last_name',
@chhantyal
chhantyal / birthday.py
Last active December 20, 2015 23:58
Thank all the people who wished you birthday on Facebook
import requests
import json
import random
AFTER = 1388952000
TOKEN = 'Token'
def get_posts():
"""
Returns dictionary of id, first names of people who posted on my wall
@chhantyal
chhantyal / periodic_task.py
Created September 13, 2013 07:35
Periodic tasks with celery
from celery.task.schedules import crontab
from celery.decorators import periodic_task
from .models import send_all
# Every ten minutes
@periodic_task(run_every=crontab(hour="*", minute="*/10", day_of_week="*"))
def send_newsletter():
print("Sending Newsletters")