Skip to content

Instantly share code, notes, and snippets.

Avatar

Paul Durivage angstwad

View GitHub Profile
@angstwad
angstwad / dict_merge.py
Last active Feb 22, 2021
Recursive dictionary merge in Python
View dict_merge.py
# Recursive dictionary merge
# Copyright (C) 2016 Paul Durivage <pauldurivage+github@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
@angstwad
angstwad / retry.py
Last active Feb 3, 2021
Python Retry Decorator
View retry.py
import time
import functools
def retry(wait, retries=3, reraise=True):
""" Decorator retries a function if an exception is raised during function
invocation, to an arbitrary limit.
:param wait: int, time in seconds to wait to try again
:param retries: int, number of times to retry function. If None, unlimited
retries.
@angstwad
angstwad / .bash_profile
Last active Jan 9, 2021
Start over work
View .bash_profile
export PATH="$HOME/.local/bin:$HOME/Projects/go/bin:$PATH"
export HISTSIZE=25000
export HISTCONTROL=ignoreboth
export CLICOLOR=1
export LSCOLORS=exfxcxdxbxexexabagacad
export PROMPT_COMMAND='history -a'
export GOPATH=$HOME/Projects/go
alias ls='ls -G'
alias ll='ls -lahG'
@angstwad
angstwad / signals.py
Created Oct 14, 2020
A simple script to catch any valid OS signals and simulate an action in response to a signal
View signals.py
import argparse
import functools
import logging
import os
import signal
import sys
import time
_LOG_FORMAT = "%(levelname)s:%(asctime)s:%(name)s:%(message)s"
logging.basicConfig(stream=sys.stdout, format=_LOG_FORMAT)
@angstwad
angstwad / git-lg.sh
Created Jan 12, 2016
Better Git Log (git lg)
View git-lg.sh
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
@angstwad
angstwad / argparse_lazy_file_type.py
Created Apr 3, 2020
argparse LazyFileType – prevents creating files unintentionally by merely initializing args and parsing
View argparse_lazy_file_type.py
# Copyright 2020 Paul Durivage <pauldurivage+github@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@angstwad
angstwad / set_cover_10.txt
Created Mar 25, 2020
Snippets for Blog: Solving a Set Cover Problem in Cloud IAM on GCP
View set_cover_10.txt
roles/axt.admin
roles/billing.admin
roles/billing.creator
roles/compute.xpnAdmin
roles/container.hostServiceAgentUser
roles/datacatalog.categoryFineGrainedReader
roles/datafusion.serviceAgent
roles/iam.serviceAccountTokenCreator
roles/iap.httpsResourceAccessor
roles/orgpolicy.policyAdmin
@angstwad
angstwad / set_cover_9.py
Created Mar 25, 2020
Snippets for Blog: Solving a Set Cover Problem in Cloud IAM on GCP
View set_cover_9.py
while remaining:
for perm in remaining:
# get set of roles which contain permission
satisfy = perms_to_roles[perm]
# sort roles by the number of permissions they contain, select role
# with the most
sorted_roles = sorted((role, perms_counts[role])
for role in satisfy)
selected = sorted_roles[-1][0]
@angstwad
angstwad / set_cover_8.py
Created Mar 25, 2020
Snippets for Blog: Solving a Set Cover Problem in Cloud IAM on GCP
View set_cover_8.py
remaining = unique_perms - roles_to_perms['roles/owner']
@angstwad
angstwad / set_cover_7.py
Created Mar 25, 2020
Snippets for Blog: Solving a Set Cover Problem in Cloud IAM on GCP
View set_cover_7.py
selected_roles = {role_with_most_perms,}