Skip to content

Instantly share code, notes, and snippets.

View mzpqnxow's full-sized avatar
🙈
Look at all this free stuff!

AG mzpqnxow

🙈
Look at all this free stuff!
View GitHub Profile
@mzpqnxow
mzpqnxow / logging.py
Created August 8, 2021 15:36 — forked from kingspp/logging.py
Python Comprehensive Logging using YAML Configuration
import os
import yaml
import logging.config
import logging
import coloredlogs
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
"""
| **@author:** Prathyush SP
| Logging Setup
@mzpqnxow
mzpqnxow / cachetools-example.py
Created August 1, 2021 15:08
Using cachetools as a convenient short-hand to use Python functiion caching for a specific argument
from cachetools import cached
from cachetools.keys import hashkey
from random import randint
@cached(cache={}, key=lambda db_handle, query: hashkey(query))
@cached(cache={}, key=lambda db_handle, query: hashkey(query))
def find_object(db_handle, query):
saved = query
query = 5
@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