Skip to content

Instantly share code, notes, and snippets.

View tomchristie's full-sized avatar
🌿

Tom Christie tomchristie

🌿
View GitHub Profile
@mjumbewu
mjumbewu / renderers.py
Last active November 28, 2019 09:19
A Django REST Framework renderer which renders data from DRF serializers into CSV. The underlying functions will render any hierarchy of Python primitive containers to CSV.
import csv
from collections import defaultdict
from rest_framework.renderers import *
from StringIO import StringIO
class CSVRenderer(BaseRenderer):
"""
Renderer which serializes to CSV
"""

Where people struggle learning Django

Over the last 3 years or so I've helped a bunch of companies, small and large, switch to Django. As part of that, I've done a lot of teaching Django (and Python) to people new to the platform (and language). I'd estimate I've trained something around 200-250 people so far. These aren't people new to programming — indeed, almost all of them are were currently employed as software developers — but they were new to Python, or to Django, or to web development, or all three.

In doing so, I've observed some patterns about what works and what doesn't. Many (most) of the failings have been my own pedagogical failings, but as I've honed my coursework and my skill I'm seeing, time and again, certain ways that Django makes itself difficult to certain groups of users.

This document is my attempt at organizing some notes around what ways different groups struggle. It's not particularly actionable — I'm not making any arguments about what Django should or shouldn't do (at least

from rest_framework import serializers
class HyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
"""
This is a performance wrapper for HyperlinkedIdentityField.
We save a ton of time by not calling reverse potentially
thousands of times per request.
"""
def __init__(self, *args, **kwargs):

2015-01-29 Unofficial Relay FAQ

Compilation of questions and answers about Relay from React.js Conf.

Disclaimer: I work on Relay at Facebook. Relay is a complex system on which we're iterating aggressively. I'll do my best here to provide accurate, useful answers, but the details are subject to change. I may also be wrong. Feedback and additional questions are welcome.

What is Relay?

Relay is a new framework from Facebook that provides data-fetching functionality for React applications. It was announced at React.js Conf (January 2015).

@gdamjan
gdamjan / client.py
Last active November 10, 2019 20:22
Python 3.5 async/await with aiohttp, parallel or sequential
import aiohttp
import asyncio
async def get_body(url):
response = await aiohttp.request('GET', url)
raw_html = await response.read()
return raw_html
async def main():
# run them sequentially (but the loop can do other stuff in the meanwhile)
@marteinn
marteinn / info.md
Last active January 21, 2024 06:57
Using the Fetch Api with Django Rest Framework

Using the Fetch Api with Django Rest Framework

Server

First, make sure you use the SessionAuthentication in Django. Put this in your settings.py

# Django rest framework
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
 'rest_framework.authentication.SessionAuthentication'
@g-cassie
g-cassie / models.py
Created August 20, 2015 14:12
This is how we are handling complex permissions right now (this is actually a grossly simplified version). The main problem is permissions logic is split between multiple places. It would be nice to consolidate all permissions logic in permissions.py. Additionally it would be nice to make this more composable so you can have AdminPermission and …
class Organization(models.Model):
name = models.CharField()
class Project(models.Model):
name = models.CharField()
users = models.ManyToManyField('User', related_name='users', through='UserProjectPermission')
organization = models.ForeignKey(Organization)
@roadsideseb
roadsideseb / python_job_sites.md
Last active April 4, 2018 10:44
Job sites for Django and Python developers
@thehesiod
thehesiod / async_exit_stack.py
Last active July 23, 2017 19:35
Async ExitStack
from inspect import iscoroutinefunction, isawaitable
import sys
from collections import deque
# NOTE: this follows the contextlib.ExitStack implementation
class _BaseExitStack:
def __init__(self):
import requests3
import trio
session = requests3.AsyncSession()
async def main():
async def request():
r = await session.get('http://127.0.0.1:8000/uuid', stream=False)