Skip to content

Instantly share code, notes, and snippets.

View mzpqnxow's full-sized avatar
🙈

AG mzpqnxow

🙈
View GitHub Profile
@mzpqnxow
mzpqnxow / python-to-yaml.py
Created July 27, 2021 18:42
Python module variables to YaML
"""Enumerate and export a bunch of structured or scalar objects from a Python module to YaML
This enumerates everything in the module in the globals namespace automatically, skipping
a few things (like things starting with `__`)
Useful when you have a ton of constants (including some that are lists, tuples, dicts, etc) in a
Python module and you want to put them into YaML to maintain separately from the Python code. It's
not a difficult task, but it becomes painful when there are 10-20 (or more) different objects. This
just automates it
@mzpqnxow
mzpqnxow / igrep.sh
Last active July 22, 2021 17:47
In place grep - why would anyone want to do such a thing!?
#!/bin/bash
#
# WARNING: YOU WILL LOSE THE FILE IF YOU SCREW UP BE CAREFUL
# If UNSAFE is set to YES, no backup will occur; you better not screw up!
# grep args or pattern because your file will be gone :>
#
# - AG
set -Eu
declare -r UNSAFE=YES
@mzpqnxow
mzpqnxow / bash-run-framework.sh
Created July 7, 2021 16:28
Configurable Bash logging, tracing and exception handling with optional JSON logging support
#!/bin/bash
#
# (C) 2021, AG ([email protected])
# BSD 3-Clause License
#
# Logging and tracing skeleton for Bash shell scripts
#
# Features
# --------
# - Verbose execution tracing to stderr or to a file; optional, via start_trace()
@mzpqnxow
mzpqnxow / nonechainmap.py
Last active September 12, 2021 00:31
Python ChainMap that treats items with None as the value the same as if the associated key was not set
from collections import ChainMap
class NoneChainMap(ChainMap):
"""Modified ChainMap to allow items to be treated as unset if they have a specific value (e.g. None)
(C) 2021, [email protected], BSD-3-Clause
LICENSE: https://opensource.org/licenses/BSD-3-Clause
Problem
-------
@mzpqnxow
mzpqnxow / hardening_usbarmory.md
Created April 24, 2021 15:57 — forked from yann2192/hardening_usbarmory.md
Hardening USB Armory

Hardening the USB Armory

As a good crypto nerd, I usually use an entirely encrypted linux FS: / but also /boot using grub LUKS support. It's a good setup but it's not perfect, the BIOS and the bootloader are not protected.

I recently got a USBArmory and I wanted to apply the same (or a better) setup.

I found some useful links but no clear howto. So this is my setup.

@mzpqnxow
mzpqnxow / subprocess.py
Created April 15, 2021 18:40 — forked from thomasballinger/subprocess.py
Using a pseudo-terminal to interact with interactive Python in a subprocess
from subprocess import Popen, PIPE
import pty
import os
from select import select
import sys
import tty
master, slave = pty.openpty()
p = Popen(['python'], stdin=slave, stdout=PIPE, stderr=PIPE)
pin = os.fdopen(master, 'w')
@mzpqnxow
mzpqnxow / python-config.py
Created April 13, 2021 13:23
Print the the variables associated with a Python build (configure params/autoconf params, basically)
# Call with /path/to/python python-config.py to check a specific version of Python, obviously
import sysconfig
for k, v in sysconfig.get_config_vars().items():
print('%s = %s' % (k, v))
@mzpqnxow
mzpqnxow / psycopg2_exception.py
Last active March 21, 2021 19:05
Do basic processing on psycopg2 exceptions to print or map the Diagnostic object
def psycopg2_diag_summarize(err: psycopg2.Error, formatted_lines: bool = False) -> Union[Dict, List[str]]:
"""Return a dict representation of a psycopg2.extensions.Diagnostics object
- Dynamically produce a dict for all public members of the object using dir() to get the
attribute names
- Filter out some undesirables (None, callables, non-localized versions of strings)
- Rename a fixed list of keys to make it more conducive to pretty-printing
Uses f-strings, so needs Python >= 3.6
psycopg2 started exposing the Diagnostic object some time >= 2.5
@mzpqnxow
mzpqnxow / description.md
Created March 13, 2021 14:52 — forked from mangecoeur/description.md
Pandas PostgresSQL support for loading to DB using fast COPY FROM method

This small subclass of the Pandas sqlalchemy-based SQL support for reading/storing tables uses the Postgres-specific "COPY FROM" method to insert large amounts of data to the database. It is much faster that using INSERT. To acheive this, the table is created in the normal way using sqlalchemy but no data is inserted. Instead the data is saved to a temporary CSV file (using Pandas' mature CSV support) then read back to Postgres using Psychopg2 support for COPY FROM STDIN.

@mzpqnxow
mzpqnxow / sql.py
Created March 9, 2021 01:29 — forked from jorisvandenbossche/sql.py
Patched version of pandas.io.sql to support PostgreSQL
"""
Patched version to support PostgreSQL
(original version: https://github.com/pydata/pandas/blob/v0.13.1/pandas/io/sql.py)
Adapted functions are:
- added _write_postgresql
- updated table_exist
- updated get_sqltype
- updated get_schema