Skip to content

Instantly share code, notes, and snippets.

@tubaman
tubaman / walkdata.py
Created October 11, 2023 00:29
walk_data - os.walk but for json-like data
from unittest import TestCase
def walk_data(root):
"""Walk a nested data structure like os.walk"""
data = root[-1]
root = root[:-1]
if hasattr(data, 'items'):
for key, value in data.items():
yield root + (key,), value
@tubaman
tubaman / asblockkitbuilderurl.py
Created June 8, 2023 18:58
Automatically post markdown to Slack
#!/usr/bin/env python3
import sys
import argparse
import json
from urllib.parse import quote
def main(argv=None):
if argv is None:
argv = sys.argv
@tubaman
tubaman / chatgptformatter.py
Created April 2, 2023 02:16
Print out all the chatgpt responses in a conversation Use the devtools in your browser to download the json file from: https://chat.openai.com/backend-api/conversation/nnnnnnnnnnnnnn
#!/usr/bin/env python3
"""Print out all the chatgpt responses in a conversation
Use the devtools in your browser to download the json file from:
https://chat.openai.com/backend-api/conversation/nnnnnnnnnnnnnn
"""
import sys
import json
@tubaman
tubaman / sqlite_decimal_field.py
Last active July 26, 2022 23:36
DecimalField workaround for SQLite's lack of a real fixed-point field
import logging
from decimal import Decimal
from django import db
from django.db.models import DecimalField as DjangoDecimalField
logger = logging.getLogger(__name__)
#logger.setLevel(logging.DEBUG)
@tubaman
tubaman / models.py
Created May 4, 2022 21:34
Django Annotate Age from Birth Date
from django.db import models
from django.db.models import Count, ExpressionWrapper, F, Case, When
from django.utils import timezone
class PersonQuerySet(models.QuerySet):
def with_age(self):
now = timezone.now()
age_expr = ExpressionWrapper(
@tubaman
tubaman / slowtelnet.py
Created June 24, 2021 15:35
Use this in place of `import telnet` for telnet servers that can't handle fast interactions
import sys
import os,re,telnetlib,time
class SlowTelnet(telnetlib.Telnet):
"""Spoon feed slow telnet servers"""
def __init__(self, *args, delay=0.2, **kwargs):
super().__init__(*args, **kwargs)
self.delay = delay
@tubaman
tubaman / 90-restart_shorewall
Last active June 23, 2021 21:44
dhcpcd hook to restart shorewall whenever the IP address changes (put in /lib/dhcpcd/dhcpcd-hooks)
if [ "$interface" = eth1 ]; then
if [ "$if_up" = true ]; then
IP=`ip add show eth1|grep global |awk '{print $2}'`
PREVIOUS_IP=`cat /tmp/previous_ip`
if [ "$PREVIOUS_IP" != "$IP" ]; then
echo "$IP" > /tmp/previous_ip
systemctl restart shorewall.service
fi
fi
fi
@tubaman
tubaman / cacheddict.py
Created December 4, 2020 00:29
A python dictionary where the items timeout after self.timeout seconds
import time
class CachedDict(object):
"""A dictionary where the items timeout after self.timeout seconds
We've implemented just enough of a real dictionary to work as a
replacement for the dicts in django.template.loader.cached.Loader
"""
@tubaman
tubaman / timedcachedloader.py
Created December 4, 2020 00:21
Timed Cached Template Loader for Django
from django.template.loaders.cached import Loader as CachedLoader
from django.conf import settings
from .cacheddict import CachedDict
class Loader(CachedLoader):
"""A cached template loader where the templates expire after self.timeout
seconds.
@tubaman
tubaman / remotetemplateloader.py
Created December 4, 2020 00:19
Remote Template Loader for Django
from urllib.request import urlopen
from django.conf import settings
from django.template import Origin, TemplateDoesNotExist
from django.template.loaders.base import Loader as BaseLoader
from django.utils.module_loading import import_string
class Loader(BaseLoader):