Skip to content

Instantly share code, notes, and snippets.

View goutomroy's full-sized avatar

Goutom Roy goutomroy

View GitHub Profile
class Meta(type):
"""
when
class MyClass(metaclass=Meta):
pass
is encountered type's __call__ method is called and from that Meta's __prepare__, __new__, __init__
is called one by one.
"""
class Author(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
def __str__(self):
return self.name
@property
def url(self):
return 'author/' + str(self.id)
import concurrent.futures
import urllib.request
from time import sleep
URLS = ['http://www.foxnews.com/',
'http://www.cnn.com/',
'http://europe.wsj.com/',
'http://www.bbc.co.uk/',
'http://some-made-up-domain.com/']
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
p = Pool(5)
print(p.map(f, [1, 2, 3]))
import time
import random
from multiprocessing import Process
class Processor(Process):
def __init__(self, number):
Process.__init__(self)
self._number = number
import time
import random
from threading import Thread
class Worker(Thread):
def __init__(self, number):
Thread.__init__(self)
self._number = number
def run(self):
def check_balance(expression):
open_list = ['(', '[', '{']
close_list = [')', ']', '}']
stack = []
for c in expression:
if c in open_list:
stack.append(c)
elif c in close_list:
pos = close_list.index(c)
if len(stack) > 0 and stack[-1] == open_list[pos]:
@goutomroy
goutomroy / Step By Step Virtualenv, Django, Postgres, NGINX, Gunicorn, HTTPS on DigitalOcean This is every step, in detail, to create a Django App with Postgres, NGINX, Gunicorn, and HTTPS on Digital Ocean. The only thing I don't go over is how to create a droplet and manage the domain.
Reference:
Django/Postgres/Nginx/Gunicorn in Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
Encryption: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04
Where you see "user", "myproject" and "myprojectuser" replace that with what is relevent to what you are working on.
User Setup
$ ssh root@SERVER_IP_ADDRESS
Complete login process
# Don't. Needs database hit
blog_id = Entry.objects.get(id=200).blog.id
# Do. The foreign key is already cached, so no database hit
blog_id = Entry.objects.get(id=200).blog_id
# Do. No database hit
blog_id = Entry.objects.select_related('blog').get(id=200).blog.id
class Author(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
def __str__(self):
return self.name
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()