Skip to content

Instantly share code, notes, and snippets.

View willstott101's full-sized avatar
🥁

Will Stott willstott101

🥁
View GitHub Profile
from itertools import chain
class DeepPartial(object):
def __init__(self, func, *args, **kwargs):
self.func = func
self.args = args
self.kwargs = kwargs
def _call(self, callee):
@willstott101
willstott101 / stl_obj_converter.py
Last active May 28, 2021 18:15
.stl to .obj file converter and slight api.
from struct import unpack
import math
try:
import numpy as np
except ImportError:
print('numpy is required to enable STL transformation.')
class _STLFile(object):
name = None
@willstott101
willstott101 / nested_lists.py
Last active August 29, 2015 14:22
Nested Lists endpoints within DRF viewsets
class NestedListMixin(object):
def get_nested_list(self, queryset, serializer_class):
"""
List view in function form.
First argument can be a list or a queryset.
"""
page = self.paginate_queryset(queryset)
if page is not None:
serializer = serializer_class(page, many=True,
@willstott101
willstott101 / constant_field.py
Created June 12, 2015 10:57
Very simple fixed value field for DRF.
from rest_framework import serializers
class ConstantField(serializers.ReadOnlyField):
def __init__(self, value, *args, **kwargs):
self._constant = value
super(ConstantField, self).__init__(*args, **kwargs)
def get_attribute(self, obj):
return self._constant
def to_representation(self, value):
return value
@willstott101
willstott101 / model_perms_decorator.py
Last active August 4, 2018 14:29
Simple method for using the DjangoModelPermissions permissions class on wrapped function-based views.
from rest_framework.permissions import DjangoModelPermissions
class BaseModelPerm(DjangoModelPermissions):
def has_permission(self, request, view):
perms = self.get_required_permissions(request.method, self.model)
return (
request.user and
(request.user.is_authenticated() or not self.authenticated_users_only) and
request.user.has_perms(perms)
@willstott101
willstott101 / django_maybe_prefetched.py
Last active August 29, 2015 14:24
Checks prefetched cache of a queryset, and filters/orders in python. To avoid large number of queries.
from operator import attrgetter
def maybe_prefetched(parent, related_manager):
#return related_manager
try:
cache = parent._prefetched_objects_cache
except AttributeError:
# Nothing is prefetched.
return related_manager
@willstott101
willstott101 / search_term_parsing.py
Last active August 29, 2015 14:26
A few simple implementations of quotable search terms splitting on whitespace.
def search_terms(s, quotes="\"'", delims='\n '):
"""
Same as shlex.split but allows for quotes to be left open.
And treats them as if they were never quoted.
"""
quote = ''
new_s = ['']
for i, c in enumerate(s, start=1):
if quote:
if c == quote:
javascript:(function() {
var videoId = /[&?]v=(\w+)/g.exec(window.location.search);
if (videoId) {
videoId = videoId[1];
window.location.href = window.location.origin + '/embed/' + videoId + '?autoplay=1';
} else {
videoId = /\/embed\/(\w+)/g.exec(window.location.pathname);
if (!videoId)
alert("You're not on a video page dumbass.");
videoId = videoId[1];
<html>
<head>
<title></title>
<style type="text/css">
div.sq {
position: absolute;
left: 10px;
width: 300px;
height: 300px;
@willstott101
willstott101 / reset_usb_device.py
Last active March 24, 2022 16:16
Disable and enable a specific USB device.
#! /usr/bin/python3
import os
import argparse
from time import sleep
PATH = '/sys/bus/usb/devices/'
def reset_device(key, value, coerce, sleep_time):
for device_dir, dirs, files in os.walk(PATH, followlinks=True):
if device_dir != PATH: