Skip to content

Instantly share code, notes, and snippets.

@jnns
jnns / form_data.py
Created October 19, 2023 13:14
Use this to get a Python dictionary of the form data given in a HTML page. lxml is used to read the form data.
def form_data(response: HttpResponse, **kwargs) -> dict:
"""Read form data from an HTML response."""
return parse_html_form(response.content, **kwargs)
def parse_html_form(html: str, **kwargs) -> dict:
"""Returns form data as a dict.
The form lookup can be narrowed down by using attributes to filter for
as `kwargs`.
@jnns
jnns / echohttpd.py
Last active July 12, 2019 09:40
Echo incoming HTTP requests using the Python3 `http.server` module.
#!/usr/bin/env python3
import argparse
from http.server import HTTPServer, BaseHTTPRequestHandler
class RequestHandler(BaseHTTPRequestHandler):
"""Print request headers/body and respond with empty JSON object."""
def do_GET(self):
self.send_response(200)
@jnns
jnns / error.js
Last active January 21, 2019 11:58
a javascript file that throws an error
window.setTimeout(function () { throw Error("hello world") }, 10000);
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Foundation Starter Template</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"></script>
<!-- Compressed CSS -->
@jnns
jnns / fkey_choice_cache_mixin.py
Created August 22, 2013 12:24
A Mixin to prevent unnecessary queries in Django Formsets. This Mixin can be added to `admin.StackedInline` or `admin.TabularInline` in order to prevent querysets to be evaluated for each and every occurence of a `ModelChoiceField`. Say, you have a ModelAdmin that has currently 25 related objects in an InlineFormSet where each form has 2 Foreign…
class ForeignKeyCacheMixin(object):
"""
Cache foreignkey choices in the request object to prevent unnecessary queries.
"""
def formfield_for_foreignkey(self, db_field, request, **kwargs):
formfield = super(ForeignKeyCacheMixin, self).formfield_for_foreignkey(db_field, **kwargs)
cache = getattr(request, 'db_field_cache', {})
if cache.get(db_field.name):
formfield.choices = cache[db_field.name]
else:
@jnns
jnns / decrypt_tva.py
Last active December 15, 2015 19:09
Decrypts a TVA result folder.
#!/usr/bin/env python
#! -*- coding: utf-8 -*-
__license__ = "GPL"
__version__ = "1.0"
import os
import re
import shutil
/*
Fixed positioning an element for as long as it's in the boundaries of its parent element.
Have an element stay in a particular position of the viewport,
but only inside a container element.
This is a function to extend jQuery. If you specify a parent element, the targeted
element will have `position: fixed` for as long as it is inside the parent's boundaries;
from then on, it switches over to `position: absolute`.