This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Pool | |
def f(x): | |
return x*x | |
if __name__ == '__main__': | |
p = Pool(5) | |
print(p.map(f, [1, 2, 3])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
from multiprocessing import Process | |
class Processor(Process): | |
def __init__(self, number): | |
Process.__init__(self) | |
self._number = number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import random | |
from threading import Thread | |
class Worker(Thread): | |
def __init__(self, number): | |
Thread.__init__(self) | |
self._number = number | |
def run(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
NewerOlder