Skip to content

Instantly share code, notes, and snippets.

View Joel-hanson's full-sized avatar
:octocat:
Looking for opportunities

Joel Hanson Joel-hanson

:octocat:
Looking for opportunities
View GitHub Profile
@Joel-hanson
Joel-hanson / videospliting.py
Created July 11, 2021 06:41
Video spliting problem
"""
- based no splits 1000 splits
- based on time eg 10s
- based on time interval 2:10 -> 2:50
- 10 people upload 1 gb 2 file daily
- we have design a system to split it into 1000 partitions
Solution:
@Joel-hanson
Joel-hanson / fib.py
Last active April 10, 2021 17:36
Find nth Fibonacci number using recursion.
def fibonacci(n):
if n <= 2:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
@Joel-hanson
Joel-hanson / urls.py
Created July 31, 2020 10:40
The urls for django rest framework
from django.urls import path, include
from rest_framework import routers
from .views import UploadViewSet
router = routers.DefaultRouter()
router.register(r'upload', UploadViewSet, basename="upload")
# Wire up our API using automatic URL routing.
urlpatterns = [
path('', include(router.urls)),
@Joel-hanson
Joel-hanson / serializers.py
Created July 31, 2020 10:33
serializer for file upload in django rest framework
from rest_framework.serializers import Serializer, FileField
# Serializers define the API representation.
class UploadSerializer(Serializer):
file_uploaded = FileField()
class Meta:
fields = ['file_uploaded']
@Joel-hanson
Joel-hanson / views.py
Last active July 31, 2020 10:33
views for file upload for django rest framework
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from .serializers import UploadSerializer
# ViewSets define the view behavior.
class UploadViewSet(ViewSet):
serializer_class = UploadSerializer
def list(self, request):
return Response("GET API")
@Joel-hanson
Joel-hanson / serializers.py
Created June 3, 2019 04:20
The Nested serializers fields can also be a dynamically modifying fields by using the SerializerMethodField . TheSerializerMethodField can be used to pass the context which contains the requestof the parent serializer, so that we can pass the fields required by us to the nested serializer.
class BookModelSerializer(ModelSerializer):
authors = SerializerMethodField("get_author_serializer")
publisher = SerializerMethodField("get_publisher_serializer")
class Meta:
model = Book
fields = '__all__'
def get_author_serializer(self, obj):
request = self.context.get('request')
@Joel-hanson
Joel-hanson / urls.py
Created June 3, 2019 04:17
In this example, we are using a DefaultRouter which adds support for automatic URL routing to Django, and provides you with a simple, quick and consistent way of wiring your view logic to a set of URLs.
from rest_framework.routers import DefaultRouter
from .views import BookModelViewSet
router = DefaultRouter(trailing_slash=False)
router.register(r'book', BookModelViewSet, base_name='book')
urlpatterns = router.urls
@Joel-hanson
Joel-hanson / views.py
Created June 3, 2019 04:16
We will be using the ModelViewSet and ModelSerializer to make the API for our Book model.
from rest_framework.viewsets import ModelViewSet
from rest_framework.authentication import SessionAuthentication
from .serializers import BookModelSerializer
from .models import Book
# Create your views here.
class BookModelViewSet(ModelViewSet):
"""
A simple ViewSet for viewing and editing books.
@Joel-hanson
Joel-hanson / models.py
Last active June 3, 2019 04:13
The example we are using to demonstrate dynamically modifying fields will have a Book model which has Publisher and Author as foreign and many to many relations.
from django.db import models
# Create your models here.
class Publisher(models.Model):
name = models.CharField(max_length=30, blank=False)
address = models.CharField(max_length=50, blank=False)
city = models.CharField(max_length=60, blank=False)
state_province = models.CharField(max_length=30, blank=False)
country = models.CharField(max_length=50, blank=False)
@Joel-hanson
Joel-hanson / serializers.py
Created June 2, 2019 12:27
Advanced serializer usage - Dynamically modifying fields
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from .models import Book, Publisher, Author
class PublisherModelSerializer(ModelSerializer):
"""
A ModelSerializer that takes an additional `fields` argument that
controls which fields should be displayed.
"""
def __init__(self, *args, **kwargs):