Skip to content

Instantly share code, notes, and snippets.

@alexkiro
alexkiro / class_based_template_tag.py
Created January 23, 2024 11:29
Class based template registry
import re
from django import template
class BaseSimpleTag:
def __init__(self, context: dict):
self.context = context
self.request = context["request"]
def __str__(self):
@alexkiro
alexkiro / app.py
Last active April 29, 2020 12:48
Django celery deploy check.
from django.core.checks import register, Warning, Error
@register(deploy=True)
def check_celery_worker(**kwargs):
from celery import current_app
errors = []
try:
assert current_app.control.inspect().ping(), "could not ping"
@alexkiro
alexkiro / block_hosts.py
Created April 2, 2020 10:59
Ad blocker via /etc/hosts
#!/usr/bin/env python3
"""
Enable or disable ad blocker via /etc/hosts file
"""
import os
import sys
import shutil
import argparse
@alexkiro
alexkiro / activate.sh
Created March 30, 2020 06:15
Activate current env on cd
export ENV_PATHS=.venv/bin/activate:venv/bin/activate:env/bin/activate:../.venv/bin/activate:../venv/bin/activate:./bin/activate:.envrc
activate() {
for pos_file in $(echo $ENV_PATHS | tr ":" "\n")
do
if [ -f "$pos_file" ]
then
source "$pos_file"
fi
done
def validate_cif(cif):
# Remove RO if it was added
cif = str(cif).lower().lstrip("ro")
if not cif.isnumeric():
# Must be numeric
return False
if len(cif) >= 10:
# Too long
return False
if len(cif) <= 1:
import MySQLdb
def task():
try:
db = MySQLdb.connect(
db="my_db", user=user, passwd=passwd,
host=host,
ssl={
"key": "/etc/ssl/private/client-key.pem",
"cert": "/etc/ssl/private/client-cert.pem",
@alexkiro
alexkiro / memt.py
Created March 11, 2016 16:55
Measure BTree memory usage
from __future__ import print_function
import sys
import random
import psutil
from BTrees import IIBTree
from BTrees import IBBTree
mem_info = psutil.Process().memory_info()