Skip to content

Instantly share code, notes, and snippets.

View href's full-sized avatar
🦀

Denis Krienbühl href

🦀
View GitHub Profile
@href
href / dict_namedtuple.py
Created October 27, 2011 12:00
Convert any dictionary to a named tuple
from collections import namedtuple
def convert(dictionary):
return namedtuple('GenericDict', dictionary.keys())(**dictionary)
"""
>>> d = dictionary(a=1, b='b', c=[3])
>>> named = convert(d)
>>> named.a == d.a
True
>>> named.b == d.b
@href
href / README.md
Last active October 17, 2022 07:18
Self-signed certificates support for macOS keychain with any CPython

When trying to connect to an endpoint with a certificate of a custom certificate authority, stored in the macOS keychain, Python generally raises errors like SSL: CERTIFICATE_VERIFY_FAILED.

They can be avoided by exporting the certs locally, and pointing modules like urllib2 or requests to it:

security export -t certs -f pemseq -k keychain.login > ~/keychain.pem
security export -t certs -f pemseq -k /Library/Keychains/System.keychain >> ~/keychain.pem
security export -t certs -f pemseq -k /System/Library/Keychains/SystemRootCertificates.keychain >> ~/keychain.pem

export REQUESTS_CA_BUNDLE="$HOME/keychain.pem"
@href
href / generate-form.py
Last active July 9, 2020 12:55
Generates an S3 HTML Upload Form for cloudscale.ch Object Storage
#!/usr/bin/env python
# This script generates an example HTML form that can directly upload files to
# an S3 bucket at cloudscale.ch.
#
# Based on the following work:
#
# - http://archive.is/fZdSu
# - http://archive.is/NXQno
#
# Requires Python 3 and boto3 to work:
@href
href / install-suitable-with-mitogen.sh
Created May 25, 2018 08:15
Installs Suitable with Mitogen using Python 2.7 for Testing
#!/usr/bin/env bash
path="$1"
set -eu
if [ -z "$path" ]; then
echo "Please pass an install directory"
exit 1
fi
@href
href / migrate_puppet.py
Created May 24, 2018 07:35
Example of our internal use of Suitable
import click
from suitable import Api
from textwrap import dedent
@click.command()
@click.argument('hostname')
def upgrade_puppet(hostname):
""" Upgrades a puppet client from puppet 3.8 to 4.x. """
@href
href / yubikey_otp_to_serial.py
Created August 30, 2016 11:27
Calculate the Yubikey serial number from the OTP
# adapted from Java:
# https://github.com/Yubico/yubikey-salesforce-client/blob/
# e38e46ee90296a852374a8b744555e99d16b6ca7/src/classes/Modhex.cls
ALPHABET = 'cbdefghijklnrtuv'
def yubikey_otp_to_serial(otp):
""" Takes a Yubikey OTP and calculates the serial number of the key.
@href
href / app.py
Last active September 28, 2017 09:13
Internal redirects in Morepath
""" Simple redirects for renamed paths using a generic redirect model.
For static paths:
@App.path('/old-path')
class OldPathRedirect(Redirect):
to = '/new-path'
For wildcard paths (e.g. /old-pages/my-page to /new-pages/my-page):
@href
href / cloudbleed.py
Last active February 24, 2017 08:37
Checks exported 1password urls against the domains affected by Cloudbleed.
#!/usr/bin/python3
""" Checks exported 1password urls against the domains affected by Cloudbleed.
Usage:
1. Export the 1password urls (as csv containing *only* the url column!!).
2. Store the exported file in the current directory as 'export.csv'.
3. Run python3 ./cloudbleed.py
@href
href / integration.py
Created December 9, 2016 09:32
An exploration of transaction integration for PonyORM
import transaction
from pony.orm import Database, PrimaryKey, Required
from pony.orm.core import DBSessionContextManager
from pony.orm.core import commit, flush, rollback
class CustomDBSessionContextManager(DBSessionContextManager):
def _enter(self, *args, **kwargs):
@href
href / united.py
Created September 9, 2013 09:57
Simple function / class to put records into groups if they belong together
class United(object):
""" Puts items added through 'append' into the same group as the last
item which was appended, as long as the matchfn which is passed the last
and the current item returns true.
e.g.
united = United(lambda last, current: last == current)
united.append(1)