Skip to content

Instantly share code, notes, and snippets.

View brydavis's full-sized avatar

Bryan Davis brydavis

View GitHub Profile
@brydavis
brydavis / logify.py
Created August 27, 2019 17:41
Set logging levels for every function using a "decontextor" (decorator + context manager)
import argparse
import logging, sys
# https://docs.python.org/3/howto/logging-cookbook.html
logger = logging.getLogger('app')
logify_level = logging.ERROR
help_statement = """
Accepts integers of corresponding logging levels
@brydavis
brydavis / find_files.py
Created August 27, 2019 17:36
Finding files within subfolders without using `os.walk` or `glob` libraries
import os
from pprint import pprint
data = {}
def find_files(folder, file_extension):
for item in os.listdir(folder):
item = "/".join([folder, item])
if os.path.isdir(item):
data[item] = []
@brydavis
brydavis / mongo_context_manager.py
Last active August 27, 2019 17:34
Using a context manager to work with Mongo
from pymongo import MongoClient
# https://medium.com/@ramojol/python-context-managers-and-the-with-statement-8f53d4d9f87
class MongoDBConnection(object):
"""MongoDB Connection"""
def __init__(self, host='localhost', port='27017'):
self.host = f"mongodb://{host}:{port}"
self.connection = None
def __enter__(self):
@brydavis
brydavis / decorator_plus_context_manager.py
Created August 27, 2019 17:31
Logging example that combines a decorator and context manager into one class
import logging, sys
logger = logging.getLogger('example')
# changing this impacts the decorator
logify_level = logging.ERROR
class logify:
def __init__(self, logger, level=None, handler=None, close=True):
self.logger = logger
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import boto3
from boto3.dynamodb.conditions import Key, Attr
import csv
import json
# Get the service resource.
session = boto3.Session(profile_name="default")
dynamodb = session.resource('dynamodb', region_name='us-west-2')
FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y golang
RUN apt-get install -y python3
CMD tail -f /dev/null
@brydavis
brydavis / docker_wasteland.sh
Last active August 14, 2019 22:27
Removes all Docker images and containers
# Destroy all your images and containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q)
# Deleting those images that are referenced in repositories
docker rmi $(docker images -q) --force
import json
import boto3
from boto3.dynamodb.conditions import Key
session = boto3.Session()
dynamodb = session.resource('dynamodb', region_name='us-west-2')
def lambda_handler(event, context):
# {
# "product_id": "P000041"
"""Dynamo Example"""
import csv
from pprint import pprint
import boto3
import threading
session = boto3.Session(profile_name="default")
dynamodb = session.resource('dynamodb', region_name='us-west-2')