Skip to content

Instantly share code, notes, and snippets.

View wontonst's full-sized avatar

Roy Zheng wontonst

View GitHub Profile
@wontonst
wontonst / gist:275b21522c63a81393fbd53e866abd3a
Created February 4, 2022 17:31
request logging middleware
class RequestLoggingMiddleware(MiddlewareMixin):
def process_response(self, request, response):
msg = '{} - {}'.format(
request.get_full_path(),
response.status_code)
if response.status_code > 299 and response.status_code < 400:
msg = '{} - {}'.format(msg, response.url)
print(msg)
@wontonst
wontonst / gist:fd2b03dd4b5ac656cc264fe70a7d37bd
Created September 3, 2019 18:05
Find all directories (common prefixes) in an s3 bucket with a root prefix
"""
Readings:
https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html
https://github.com/boto/boto3/blob/develop/boto3/examples/s3.rst#list-top-level-common-prefixes-in-amazon-s3-bucket
To get only leaf directories instead of all: https://gist.github.com/wontonst/c8c06730d13e004de390e67f593be763
"""
import boto3
@wontonst
wontonst / common_prefix.py
Last active September 3, 2019 18:06
Find all leaf node directories (common prefixes) in an s3 bucket with a root prefix
"""
Readings:
https://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html
https://github.com/boto/boto3/blob/develop/boto3/examples/s3.rst#list-top-level-common-prefixes-in-amazon-s3-bucket
To get all subdirectories instead of only leaf: https://gist.github.com/wontonst/fd2b03dd4b5ac656cc264fe70a7d37bd
"""
import boto3
@wontonst
wontonst / mortonize.py
Created August 8, 2019 18:10
interleave mortonize z order curve python bits
# http://code.activestate.com/recipes/577558-interleave-bits-aka-morton-ize-aka-z-order-curve/
def part1by1(n):
n&= 0x0000ffff
n = (n | (n << 8)) & 0x00FF00FF
n = (n | (n << 4)) & 0x0F0F0F0F
n = (n | (n << 2)) & 0x33333333
n = (n | (n << 1)) & 0x55555555
return n
@wontonst
wontonst / idiom.py
Last active April 15, 2017 18:33
Idiomatic Python Code Review Exercise
# This is an exercise written by Roy Zheng based on the "Writing Idiomatic Python" book.
# Take a look at my blog post here [INSERT] for the full exercise.
class Tank:
def __init__(self, armor, penetration, armor_type):
self.armor = armor
self.penetration = penetration
self.armor_type = armor_type
if not (armor_type == 'chobham' or armor_type == 'composite' or armor_type == 'ceramic'):
raise Exception('Invalid armor type %s' % (armor_type))