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 / add_python_to_WINDOWS_WSL_ubuntu.md
Created February 13, 2019 13:19
Add python to Windows Subsystem for Linux (WSL) [ubuntu]
@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 / Ubuntu1604py36Dockerfile
Last active June 14, 2023 20:31
Base Docker image for ubuntu-16.04 & Python3.6
# docker build -t ubuntu1604py36
FROM ubuntu:16.04
RUN apt-get update && \
apt-get install -y software-properties-common && \
add-apt-repository ppa:jonathonf/python-3.6
RUN apt-get update
RUN apt-get install -y build-essential python3.6 python3.6-dev python3-pip python3.6-venv
RUN apt-get install -y git
@monkut
monkut / argparse_date_datetime_custom_types.py
Last active May 10, 2023 15:47
Custom date/datetime type validators for python's argparse module
def valid_date_type(arg_date_str):
"""custom argparse *date* type for user dates values given from the command line"""
try:
return datetime.datetime.strptime(arg_date_str, "%Y-%m-%d")
except ValueError:
msg = "Given Date ({0}) not valid! Expected format, YYYY-MM-DD!".format(arg_date_str)
raise argparse.ArgumentTypeError(msg)
def valid_datetime_type(arg_datetime_str):
"""custom argparse type for user datetime values given from the command line"""
@monkut
monkut / PrepareAWSClientVPN.md
Created June 14, 2019 06:58
Prepare AWS Client VPN for connecting to a VPC without NAT/IGW

Prepare AWS Client VPN README

Prerequisites

  • AWS Account
  • Linux(like) System
  • awscli
@monkut
monkut / serving-sphinx-docs-on-s3.md
Created May 8, 2018 05:35
Serving Sphinx Documentation on s3

Build your sphinx document

Assumes you already have a sphinx project installed and a project created

make html

By default this command will build documentation to _build/html

@monkut
monkut / hexcolor2grayscale.py
Created February 17, 2015 05:45
Used this script to minimalize osm-bright mml colors to grayscale for creating a data base layer map in tilemill.
"""
Script to replace color #RRGGBB formated values to grayscale values inplace.
usage:
python hexcolor2grayscale.py filename1 filename2
"""
import fileinput
import sys
import re
import colorsys