Skip to content

Instantly share code, notes, and snippets.

@Kos
Kos / gamepad.js
Last active December 13, 2020 20:37
// https://w3c.github.io/gamepad/#dfn-standard-gamepad-layout
const Dpad = {
UP: 12,
LEFT: 14,
RIGHT: 15,
DOWN: 13,
};
const Buttons = {
A: 0,
B: 1,
class Book(models.Model):
isbn = models.CharField(max_length=13)
title = models.TextField()
author = models.TextField()
visible = models.BooleanField(default=True)
@Kos
Kos / views.py
Last active April 24, 2021 18:22
def book_list(request: HttpRequest) -> HttpResponse:
if request.method == "GET":
books = Book.objects.filter(visible=True)
if "author" in request.GET:
books = books.filter(author__icontains=request.GET["author"])
if "title" in request.GET:
books = books.filter(title__icontains=request.GET["title"])
response_data = [book_to_json(book) for book in books]
return JsonResponse(response_data, safe=False)
def book_to_json(book: Book):
return {
"isbn": book.isbn,
"title": book.title,
"author": book.author,
}
def book_from_json(data: dict) -> Book:
return Book(
@Kos
Kos / pytest_helpers.py
Last active May 1, 2025 13:58
Pytest helpers
class AnyUUID:
def __eq__(self, other):
if isinstance(other, str):
try:
UUID(other)
return True
except ValueError:
return False
else:
return isinstance(other, UUID)