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
""" | |
This demonstrates how to use singledispatch for dispatching on type | |
We want to draw each based on the type. Typically, we can use if else statement to check the type | |
then call the function that does draw the shape, however, we will keep having multiple if elif statement. | |
This is just another pythonic way of doing it | |
""" | |
from functools import singledispatch |
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 slow_gcd(arg1,arg2): | |
gcd = 0 | |
for num in range(1, arg1 + arg2+ 1): | |
if (arg1 % num == 0) and (arg2 %num == 0): | |
gcd = num | |
return gcd | |
def fast_gcd(arg1,arg2): | |
if arg2 == 0: |
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 slow_fibonacci_number(n): | |
if n <= 1: | |
return n | |
return slow_fibonacci_number(n - 1) + slow_fibonacci_number(n - 2) | |
def fast_fibonacci_number(n): | |
numbers = [0, 1] | |
for num in range(2, n+1): | |
numbers.append(numbers[num - 1] + numbers[num - 2]) |
Tips for using less
on the command line.
To navigate:
- f = for next page
- b = for previous page
To search:
- /<query>
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
{ | |
"root": "build/", | |
"clean_urls": false, | |
"routes": { | |
"/**": "index.html" | |
} | |
} |
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
#After looking for a way to check if a model instance can be deleted in django , | |
#i came across many sample, but was not working as expected. Hope this solution can help. | |
#Let start by creating an Abstract model class which can be inherited by other model | |
class ModelIsDeletable(models.Model): | |
name = models.CharField(max_length=200, blank=True, null=True, unique=True) | |
description = models.CharField(max_length=200, blank=True, null=True) | |
date_modified = models.DateTimeField(auto_now_add=True) | |
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
# NOTE This is probably no longer needed, now DRF does this | |
# automatically if you have ATOMIC_REQUESTS enabled. | |
# https://github.com/encode/django-rest-framework/pull/2887 | |
from django.db import transaction | |
class AtomicMixin(object): | |
""" | |
Ensures we rollback db transactions on exceptions. |