Skip to content

Instantly share code, notes, and snippets.

@monkut
monkut / truncate-table-command.json
Created November 8, 2023 06:22
django/lambda/zappa table truncate test command
{
"raw_command": "from django.db import connection;c = connection.cursor();table = 'TABLENAME';sql = f'TRUNCATE TABLE {table} CASCADE';c.execute(sql)"
}
@monkut
monkut / widgets.py
Created August 14, 2023 01:46
Django admin widget to allow proper multiple selection of selected checkbox entries for CheckboxSelectMultiple
class ArrayFieldCheckboxSelectMultiple(CheckboxSelectMultiple):
"""A CheckboxSelectMultiple widget for use with ArrayField which fixes the Multiple items not selected issue"""
def format_value(self, value):
"""Return selected values as a list."""
if value is None and self.allow_multiple_selected:
return []
elif self.allow_multiple_selected:
value = [v for v in value.split(",")]
@monkut
monkut / prepare_drop_table_commands.py
Last active July 26, 2023 00:54
create drop tables commands for django (zappa raw_command) for resetting database
def create_drop_tables_raw_command(tables: list[str]) -> str:
"""Get list of tables using the 'manage sqlflush' command. This will create the DROP TABLE command for resetting the database"""
django_migrations_table = "django_migrations"
if django_migrations_table not in tables:
print(f"-- '{django_migrations_table}' not given, adding to tables!")
tables.append(django_migrations_table)
statements = ["from django.db import connection"]
statements.append("cursor = connection.cursor()")
@monkut
monkut / lambdaenvars.py
Last active June 3, 2021 01:06
Command-line script to get/set environment variables and list existing aws lambda functions
"""
Get/Set lambda environment variables
"""
import os
import pprint
from pathlib import Path
from typing import Tuple, List
from enum import Enum
import boto3
@monkut
monkut / connectmidicontroller.py
Created January 4, 2021 05:10
auto connect midi controller script for fluidsynth, rasbian
from time import sleep
from subprocess import check_output, CalledProcessError
controller_identifier = 'microKEY2'
configured = False
while True:
aconnect_list_command = ('aconnect', '-o')
output_bytes = check_output(aconnect_list_command)
output_str = output_bytes.decode('utf8')
controller_attached = False
@monkut
monkut / autostartsynth.sh
Created January 4, 2021 05:09
an fluidsynth autostart script for raspberrypi
#! /bin/sh
# rasbian
# add the following line to /etc/rc.local above the "exit 0" line to start on boot
# /home/pi/autostartsynth.sh &
killall fluidsynth
# set sound
export DESIRED_SOUNDFONT=/usr/share/sounds/sf2/FluidR3_GM.sf2
# start fluidsynth with desired sound
@monkut
monkut / iterate_s3_csv.py
Created November 28, 2020 13:24
iterate a csv file in S3 from memory
import boto3
S3_CLIENT = boto3.client("s3")
def iterate_s3_csv(bucket: str, key: str) -> Generator:
with BytesIO() as bytesin:
S3_CLIENT.download_fileobj(bucket, key, bytesin)
bytesin.seek(0)
stringin = StringIO(bytesin.read().decode("utf8"))
@monkut
monkut / lambdaenvars.py
Created November 17, 2020 06:50
cli tool to get/set aws lambda environment variables
"""
Get/Set lambda environment variables
"""
import os
import pprint
from pathlib import Path
from typing import Tuple, List
from enum import Enum
import boto3
@monkut
monkut / update_awslambda_envars.bash
Created January 11, 2020 05:11
update aws lambda environment variables via awscli and jq
# the `update-function-configuration` overwrites the existing set envars.
# In order to *ADD* variables we need to read the existing envars and add to that.
# This command uses `jq` to read and transform the json result to an envar then update the lambda configuration
# create the updated envar set
export YOUR_FUNCTION_NAME={populate this}
export UPDATED_ENVIRONMNET_VARIABLES=$(aws lambda get-function-configuration --function-name ${YOUR_FUNCTION_NAME} | \
jq --compact-output ".Environment + {\"Variables\": (.Environment.Variables + {\"NEW_ENVAR_NAME\": \"NEW_ENVAR_VALUE\"})}")
# check
import json
import boto3
secretsmgr_endpoint_url = 'http://localhost:4584'
SM_CLIENT = boto3.client('secretsmanager', endpoint_url=secretsmgr_endpoint_url)
# Set Secret
sample_secret_id = 'some:id:2'
secret_data = json.dumps({'key1': 'value1', 'key2': 'value2'})