Skip to content

Instantly share code, notes, and snippets.

View wolkenarchitekt's full-sized avatar

Ingo Weinmann wolkenarchitekt

View GitHub Profile
@wolkenarchitekt
wolkenarchitekt / ndict.py
Last active March 15, 2022 14:24
A defaultdict like dictionary that allows accessing nested keys without getting KeyErrors or TypeErrors
# Required: boltons, nested_dict
from boltons.iterutils import remap
from nested_dict import nested_dict
def ndict(data):
"""
Create a defaultdict-like dictionary that allows accessing nested keys without getting
KeyErrors or TypeErrors.
Regular dict:
@wolkenarchitekt
wolkenarchitekt / git_url.py
Last active December 8, 2017 12:05
Get git url of file (works for Github and Gitlab)
#!/usr/bin/env python
#
# Show URL of file in git, optionally open URL in browser.
# In contrast to 'hub' this works to Github *and* Gitlab.
#
# Requirements: GitPython, click
import logging
import os
import sys
import webbrowser
@wolkenarchitekt
wolkenarchitekt / ssh_tunnel.py
Last active December 8, 2017 13:11
Establish SSH forwarding tunnel in background
import subprocess
logger = logging.getLogger(__name__)
def ssh_tunnel(jumphost, remote_server, local_port, remote_port,
control_socket='/tmp/ssh_socket'):
"""
Establish SSH tunnel
:param jumphost: jump host
@wolkenarchitekt
wolkenarchitekt / parse_icinga_object_cache.py
Created August 9, 2017 14:30
Parse Icinga object cache file using Python and regex
#!/usr/bin/env python
# Parse Icinga object cache file into proper data structure
import re
import logging
logger = logging.getLogger(__name__)
def parse_config(cache_file):
"""
@wolkenarchitekt
wolkenarchitekt / argparselibs.md
Last active February 22, 2016 09:45
Command line argument parsing libraries

argcomplete

https://github.com/kislyuk/argcomplete

argcomplete - Bash tab completion for argparse Tab complete all the things!

Argcomplete provides easy, extensible command line tab completion of arguments for your Python script.

It makes two assumptions:

@wolkenarchitekt
wolkenarchitekt / ipythonsync.sh
Last active December 23, 2015 00:19
Monitor IPython SQLite history; save into plain textfile on every update
#!/usr/bin/env bash
# Saves todays ipython history to textfile.
# Needs inotify-tools, sqlite3
HISTORY_SQLITE="${HOME}/.config/ipython/profile_default/history.sqlite"
HISTORY_FILE="${HOME}/.config/ipython/profile_default/history.py"
if [ ! -f "${HISTORY_FILE}" ]; then
touch "${HISTORY_FILE}"
fi
@wolkenarchitekt
wolkenarchitekt / ipythonhist.py
Last active August 22, 2023 10:00
Access complete IPython history
#!/usr/bin/env python
"""Extract a session from the IPython input history.
"""
import sys
import codecs
from IPython.core.history import HistoryAccessor
class MyHistoryAccessor(HistoryAccessor):
""" Modified HistoryAccessor to fetch the whole ipython history across all sessions """