Skip to content

Instantly share code, notes, and snippets.

View Avantol13's full-sized avatar
🎹

Alexander VanTol Avantol13

🎹
  • CTDS - University of Chicago
View GitHub Profile
# Python 2
from mock import patch, mock_open
creds_file = "This text is not really in a file."
mocked_open = patch("__builtin__.open", mock_open(read_data=creds_file))
mocked_open.start()
with open("any_file_name") as mocked_file:
print(mocked_file.read())
@Avantol13
Avantol13 / dictionaries_match.py
Created October 24, 2018 15:40
Check if two dictionaries in Python have the same key structure/schema
def dictionaries_match(d1, d2):
# both are dicts, need to check values
if isinstance(d1, dict) and isinstance(d2, dict):
d1_keys = d1.keys()
d1_keys.sort()
d2_keys = d2.keys()
d2_keys.sort()
valid = d1_keys == d2_keys and all(
dictionaries_match(d1[k], d2[k]) for k in d1.keys()
@Avantol13
Avantol13 / g_sign.py
Created September 28, 2018 14:28
generating signed url for requester pays bucket, bill to project that owns the bucket
# NOTE: need to install oauth2client
# Signed URL generates correctly but I keep getting:
# <Error>
# <Code>SignatureDoesNotMatch</Code>
# <Message>
# The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
# </Message>
# ...
# </Error>
@Avantol13
Avantol13 / nested_dict_replace.py
Last active June 13, 2018 22:53
Replace nested value in dict from another nested value in a different dict
import copy
def replace(data, path_to_key, replacement_value):
"""
Will replace a nested value in a dict with the given value.
Args:
data (dict): a dictionary
path_to_key (str): nested/path/to/key. The value of this key will be
replaced
replacement_value (str): Replacement value for the key from
# other stuff ...
# open git cola (https://git-cola.github.io/) easier
alias gc='git-cola &'
# enter friendly interactive shell easier (https://fishshell.com/)
alias f='fish'
# create and checkout new branch easier, stash stuff before if needed
gitbranch(){
@Avantol13
Avantol13 / connection_example.py
Last active April 30, 2018 20:44
Issues with Listing Groups in Google Cloud Identity by use of Admin Directory v1 API and Domain Wide Delegation of a Service Account in GCP
from config import GOOGLE_APPLICATION_CREDENTIALS
from config import GOOGLE_APPLICATION_CREDENTIALS_P12
from config import GOOGLE_CLOUD_IDENTITY_ADMIN_EMAIL
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
def connection_test():
"""
NATURAL LOG LOOKUP TABLE GENERATOR
Generates a lookup table for the natural log function given a lower and upper bound
Points chosen ensure that linear approximation between the points minimize error
within the MAX_PERCENT_ERROR provided.
You can adjust the MAX_PERCENT_ERROR, realizing that a smaller error will require
more data points. The length of the resulting list should be printed to standard output.
import argparse
import sys
import os
def main():
'''
Entry point of script
'''
# Parse arguments into script
# Redirect print output to null device on operating system
print_redirect = open(os.devnull, 'w')
sys.stdout = print_redirect
# See this: http://stackoverflow.com/a/6735958/4175042
Purpose
--------------------------------------------------------------------------------------------
Change Windows default name for copied files. Is usually OriginalFileName - Copy.ext
Directions
-------------------------------------------------------------------------------------------
In Windows registry: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer
- Add key : NamingTemplates
- Add new String Value : CopyNameTemplate
- Add Data for CopyNameTemplate: <New default here>