Skip to content

Instantly share code, notes, and snippets.

View willstott101's full-sized avatar
🥁

Will Stott willstott101

🥁
View GitHub Profile
@willstott101
willstott101 / psd_to_gltf.py
Created October 7, 2023 12:29
Converts a PSD or PSB file to a layered shadeless GLTF file
# Depends on psd-tools pygltflib
from psd_tools import PSDImage
import pygltflib
from pygltflib import GLTF2
import sys
import os
import numpy as np
@willstott101
willstott101 / test_finalizer_weak_dict.py
Created August 15, 2022 15:35
MCVE of how finalizers cannot depend on weak reference state in the presence of circular references.
from weakref import WeakValueDictionary
import gc
objs = WeakValueDictionary()
class Registered:
def __init__(self):
objs[id(self)] = self
def __del__(self):
del objs[id(self)]
@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:
<html>
<head>
<title></title>
<style type="text/css">
div.sq {
position: absolute;
left: 10px;
width: 300px;
height: 300px;
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];
@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:
@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 / 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 / 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 / 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,