Skip to content

Instantly share code, notes, and snippets.

View corentinbettiol's full-sized avatar

Corentin Bettiol corentinbettiol

View GitHub Profile
@gregorynicholas
gregorynicholas / .bash_profile
Created January 8, 2013 23:44
autocomplete functionality for python library command line interfaces (CLI): Fabric & Paver.
#!/usr/bin/env bash
. paver_autocomplete
. fabric_autocomplete
@linkdd
linkdd / hackernews-new-comms.js
Last active September 20, 2021 22:52
Add a bell emoji to unread comments on HackerNews
// Can be used with https://github.com/xcv58/Custom-JavaScript-for-Websites-2
// This snippet is released under the terms of the CC0 license: https://creativecommons.org/publicdomain/zero/1.0/deed.en
const cache_key = 'hn_comments_views'
const cache = JSON.parse(localStorage.getItem(cache_key) || '{}')
document.querySelectorAll('.athing.comtr').forEach(comm => {
if (!cache[comm.id]) {
const span = document.createElement('span')
span.innerHTML = '🔔' // :bell: emoji
@cb109
cb109 / visualize_django_template_include_hierarchy_with_graphviz.py
Last active December 21, 2021 17:06
Visualize Django Template Include Hierarchy with Graphviz
"""Point script at Django template name and output DOT text describing the includes.
Starting template name must be something relative like
'myapp/mysubfolder/template_1.html'. The script will follow the inputs and output a text like:
digraph G {
node [shape="rectangle"];
"myapp/mysubfolder/template_1.html" -> "myapp/mysubfolder/template_2.html";
"myapp/mysubfolder/template_2.html" -> "myapp/mysubfolder/template_3.html";
...
@lambdamusic
lambdamusic / Snipplr-50835.py
Created February 7, 2013 21:28
Django: Strip/Remove HTML tags (django utils)
# To strip/remove HTML tags from an existing string we can use the strip_tags function.
# import the strip_tags
from django.utils.html import strip_tags
# simple string with html inside.
html = '<p>paragraph</p>'
print html # will produce: <p>paragraph</p>
stripped = strip_tags(html)
@elidickinson
elidickinson / admin.py
Created November 20, 2011 01:16
Using CKEditor with Flatpages
from django.contrib import admin
from django.contrib.flatpages.models import FlatPage
# Note: we are renaming the original Admin and Form as we import them!
from django.contrib.flatpages.admin import FlatPageAdmin as FlatPageAdminOld
from django.contrib.flatpages.admin import FlatpageForm as FlatpageFormOld
from django import forms
from ckeditor.widgets import CKEditorWidget
#!/usr/bin/env bash
# journal.sh
# ==========
#
# One daily text file to rule them all.
#
# Copyright: 2022 Tyler Cipriani <tyler@tylercipriani.com
# License: GPLv3
set -euo pipefail
@yakky
yakky / 0001_initial.py
Last active September 15, 2023 11:12
Migrate a plugin from one type to another
from django.db import migrations
def migrate_to_new_plugin(apps, schema_editor):
OldPluginModel = apps.get_model("myapp", "PluginModel")
NewPluginModel = apps.get_model("myapp", "PluginModel")
for obj in OldPluginModel.objects.all():
#
new_obj = NewPluginModel()
new_obj.id = obj.id
@p4bl0-
p4bl0- / Pure HTML TicTacToe.md
Last active November 15, 2023 15:31
Pure HTML playable TicTacToe game (no CSS nor JavaScript) in 367KB

This is an attempt to see how small a pure HTML playable TicTacToe game can be. The first script (ttt.py) generates a 560KB HTML file.

I made it in reaction to this Hacker News post: Implementing Tic Tac Toe with 170MB of HTML – No JavaScript or CSS, because my reaction to "170MB" was "wait, WAT?".

The second script (ttt_smaller.py) is an attempt to go further, by shortening IDs to maximum 2 chars rather than using 9 characters for full board descriptions as IDs. It does so by using a custom base74, because 74 + 74 × 74 = 5550 which is just above the 5478 game states that have to be represented.

This optimization allows to get down to 412KB, that is a saving of 148Ko, i.e., a 26% size-reduction!

@drhoden
drhoden / getContext.py
Last active December 7, 2023 16:18
Obtain context from within Django custom template filter
"""
What if you suddenly need to have access to the context object from within
certain filters, and it would be costly to alter its syntax? Well, that
just happend to me. Here is my solution.
"""
def get_context(max_depth=4):
"""A simple (and perhaps dangerous) way of obtaining a context. Keep
in mind these shortcomings:
1. There is no guarantee this returns the right 'context'.