Skip to content

Instantly share code, notes, and snippets.

View davidbgk's full-sized avatar
🚪
Let’s escape GAFAM+ when/while we can!

David Larlet davidbgk

🚪
Let’s escape GAFAM+ when/while we can!
View GitHub Profile
@davidbgk
davidbgk / fields.py
Created October 28, 2010 10:30
Easily add an empty choice to a Django ChoiceField
from django import forms
class EmptyChoiceField(forms.ChoiceField):
def __init__(self, choices=(), empty_label=None, required=True, widget=None, label=None,
initial=None, help_text=None, *args, **kwargs):
# prepend an empty label if it exists (and field is not required!)
if not required and empty_label is not None:
choices = tuple([(u'', empty_label)] + list(choices))
@davidbgk
davidbgk / server.py
Created April 11, 2017 15:39
An attempt to create the simplest HTTP Hello world in Python3
import http.server
import socketserver
from http import HTTPStatus
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
self.wfile.write(b'Hello world')
@davidbgk
davidbgk / watch_turtle.py
Last active September 18, 2023 23:07 — forked from magopian/watch_turtle.py
Auto-reload turtle code on file save (https://docs.python.org/3/library/turtle.html)
"""Reloads the `my_turtle.py` code on save.
Put simple turtle instructions in the `my_turtle.py` file,
and they'll be re-run (on a clean window) on each file save.
Usage:
1/ put some turtle instructions in a `my_turtle.py` file
(eg `turtle.forward(100)`)
2/ run `python watch_turtle.py` on a commandline
(no dependencies needed)
@davidbgk
davidbgk / index.html
Created May 10, 2016 16:06
Test DOM Content Loaded and promises
<!doctype html>
<meta charset=utf-8>
<title>Test DOM Content Loaded and promises</title>
<script>
console.log(document.querySelector('main')) // => null
document.ready = new Promise(
(resolve) => document.addEventListener('DOMContentLoaded', resolve))
document.ready.then(() => console.log(document.querySelector('main'))) // => <main>
</script>
<main></main>
@davidbgk
davidbgk / README.md
Created February 13, 2023 13:45 — forked from baldurbjarnason/README.md
Book production files for Out of the Software Crisis

Book production files

For Out of the Software Crisis.

These are under the MIT License. Do what you want, essentially. Probably doesn't work out of the box. Uses Pandoc, zx, weasyprint, and Literata.

You'll need to download them for this to work. And you'll need to edit the build script to point at the correct CSS files. And you'l need to edit the CSS to point at the correct font files.

@davidbgk
davidbgk / python.md
Created October 20, 2022 02:41 — forked from eyeseast/python.md
How to set up Python in 2022

Python

This is my recommended Python setup, as of Fall 2022. The Python landscape can be a confusing mess of overlapping tools that sometimes don't work well together. This is an effort to standardize our approach and environments.

Tools and helpful links:

@davidbgk
davidbgk / utils.py
Last active August 13, 2022 20:08
Let's start to reference some Python utils
def neighborhood(iterable, first=None, last=None):
"""
Yield the (previous, current, next) items given an iterable.
You can specify a `first` and/or `last` item for bounds.
"""
iterator = iter(iterable)
previous = first
current = next(iterator) # Throws StopIteration if empty.
for next_ in iterator:
@davidbgk
davidbgk / utils.css
Last active August 13, 2022 20:07
Let's start to reference some CSS utils
/* Applying it to the html element will provide smooth scrolling to anchors
https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-behavior */
html {
scroll-behavior: smooth;
}
/* Remove animations for folks who set their OS to reduce motion.
1. Immediately jump any animation to the end point
2. Remove transitions & fixed background attachment
@davidbgk
davidbgk / webmention.py
Last active July 4, 2022 01:22 — forked from karlcow/webmention.py
Using gunicorn
def server_app(environ, start_response):
method = environ.get('REQUEST_METHOD', 'GET')
if method == 'GET':
return get_response(start_response)
elif method == 'POST':
accept_header = environ.get('HTTP_ACCEPT', '*/*')
post_data = environ.get('wsgi.input').read()
return post_response(start_response, accept_header, post_data)
@davidbgk
davidbgk / web-servers.md
Created June 9, 2022 22:47 — forked from willurd/web-servers.md
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000