Skip to content

Instantly share code, notes, and snippets.

# users = queryset.values_list('group', 'username').distinct()
users = [('admin', 'root'), ('group1', 'andy'), ('group1', 'tim')]
grouped_users = reduce(lambda d, (k, v): dict(d, **{k: d.get(k, [])+[v]}), users, {})
print grouped_users # {'admin': ['root'], 'group1': ['andy', 'tim']}
choices = map(lambda (k, v): (k, [('%s_%s' % (k, x), x) for x in v]), grouped_users.items())
print choices # [('admin', [('admin_root', 'root')]), ('group1', [('group1_andy', 'andy'), ('group1_tim', 'tim')])]
@WayneSan
WayneSan / authentication.py
Last active January 7, 2024 20:34
PyJWT + Django REST framework 2
import jwt
from django.conf import settings
from django.contrib.auth.models import User
from rest_framework import exceptions
from rest_framework.authentication import TokenAuthentication
class JSONWebTokenAuthentication(TokenAuthentication):
#!/bin/bash
#### Preprocessing ####
if [ "$1" == "--color" ]; then COLOR="TRUE"; shift; fi
if [ $# -lt 2 ]; then
echo "Usage: $0 [--color] TITLE PROGRESS [MAXIMUM]"
exit
fi
@WayneSan
WayneSan / Master.java
Created November 14, 2012 22:04
Construct inner class instance base on extended class.
import java.lang.reflect.Constructor;
public class Master
{
public Master()
{
System.out.println( "Master >> " + this.getClass().getName() );
}
public interface IJob {
@WayneSan
WayneSan / ElementWrapper.java
Last active May 14, 2019 10:18
Wrap W3C DOM Element as a fluent interface
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class ElementWrapper {
Document doc;