Skip to content

Instantly share code, notes, and snippets.

View mikegrima's full-sized avatar

Mike Grima mikegrima

View GitHub Profile
mikegrima /
Created June 22, 2016 19:19
Base64 Encoding and Decoding to Strings in Python.
import base64
# Convert this to a b64 string:
some_str = "Some string..."
b64string = base64.b64encode(some_str.encode("utf-8")).decode("utf-8")
# ^^ Without this, it's a byte array.
# /nomorehairpulling
mikegrima /
Last active October 14, 2016 22:14
Quick and dirty recursive dict lowercaser
import json
This is NOT extensive in any way -- just something quick and dirty. It doesn't handle all datatypes -- use at your own risk.
def lowercase(original):
if isinstance(original, str):
return original.lower()
elif isinstance(original, list):
mikegrima /
Created October 14, 2016 22:15
Quick and dirty dict key lowercaser
import json
This is NOT extensive in any way -- just something quick and dirty.
def lowercase_keys(original, in_lists=False):
if in_lists and isinstance(original, list):
new_list = []
for item in original:
mikegrima /
Last active September 14, 2017 17:53
Boto3 Read Object from S3
import boto3
client = boto3.client("s3")
result = client.get_object(Bucket=BUCKET, Key=KEY)
compressed = False
mikegrima / OLD
Last active February 28, 2018 02:09
Assume Role Client
View OLD
# from botocore.session import Session
# from botocore.credentials import AssumeRoleProvider, CredentialResolver, AssumeRoleCredentialFetcher, \
# DeferredRefreshableCredentials, _local_now
# """
# # Where much of this is referenced:
# """
mikegrima /
Last active February 4, 2019 20:15
Strip timezone and microseconds in Python
from datetime import datetime
# Prints a string that looks like: 2017-08-30T21:19:30 (this is a good standard ISO format)
current_time = datetime.utcnow().replace(tzinfo=None, microsecond=0).isoformat()
# 2019-02-04T19:44:04
# To read in a string like above
date_obj = datetime.strptime(current_time, "%Y-%m-%dT%H:%M:%S")
mikegrima / instructions.txt
Last active March 19, 2019 00:02
S3 only permit specific bucket object access
View instructions.txt
Documented here:
Yes, it's really shitty.
Example Policy to ONLY allow specific IAM Roles access to the objects (denies all the other roles access to the objects):
"Version": "2008-10-17",
"Statement": [
mikegrima /
Created March 19, 2019 22:00
Python snake_case to camelCase
def snake_to_camels(data: object) -> object:
"""Function that recursively converts snake_case to camelCase in a dict."""
if isinstance(data, dict):
data_copy = dict(data)
for key, value in data.items():
# Send the value back:
new_value = snake_to_camels(value)
# In case the key isn't a string (like an Int):
mikegrima / event.json
Created August 20, 2019 18:55
SNS Lambda Sample Invocation Event
View event.json
"Records": [
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:REGION:ACCOUNT:TOPICNAME:SOME-ID",
"Sns": {
"Type": "Notification",
"MessageId": "SOME-ID",
"TopicArn": "arn:aws:sns:REGION:ACCOUNT:TOPICNAME",
mikegrima /
Created December 12, 2019 20:21
Strip unicode from Python 2 Dictionary
# Strip out the unicode keys from a Python 2 dictionary (also includes nested Lists, Dicts, and Sets):
def py2_strip_unicode_keys(blob):
"""For Python 2 Only -- this will convert unicode keys in nested dictionaries to standard strings."""
if type(blob) == unicode:
return str(blob)
elif type(blob) == dict:
for key in list(blob.keys()):
value = blob.pop(key)
blob[str(key)] = py2_strip_unicode_keys(value)