Skip to content

Instantly share code, notes, and snippets.

View gwax's full-sized avatar
🤘
metal

George Leslie-Waksman gwax

🤘
metal
View GitHub Profile
@gwax
gwax / update_dns.py
Created March 29, 2017 21:18
script to update a google cloud dns record for a host
#!/usr/bin/env python3
"""Update Google cloud dns records for a machine."""
# if testing locally, make sure that you have authenticated: https://googlecloudplatform.github.io/google-cloud-python/stable/google-cloud-auth.html#overview
from google.cloud import dns
def update_dns_a_record(zonename, hostname, ip_address):
"""Update the A record for a given host on a target zone."""
client = dns.Client()
zone = client.zone(zonename)
zone.reload() # call api to populate zone attributes
@gwax
gwax / setup_haskell.sh
Last active January 16, 2018 19:38
Haskell on OSX
brew cask install haskell-platform
echo 'export PATH="$PATH/Library/Haskell/bin:$PATH"' >> ~/.bash_profile
cabal update
cabal install -j8 happy
cabal install -j8 stylish-haskell
cabal install -j8 hlint
cabal install -j8 hsdev hasktags
cabal install -j8 ghc-mod
# Sublime: install SublimeHaskell package
# VS Code: install "Haskell ghc-mod", "Haskell Syntax Highlighting", "haskell-linter", "stylish-haskell" extensions
@gwax
gwax / data_paths.md
Last active April 20, 2023 12:11
handling data file paths in python

A couple pointers for dealing with files on the filesystem from inside python:

  1. don't modify sys.path
  2. don't use relative paths unless they are relative to something
  3. always use os.path.join
  4. don't rely on the environment

Now for a pattern that I strong suggest:

Start with a code tree where your files are set aside but inside your code tree:

@gwax
gwax / profiling.py
Created May 11, 2016 17:47
Profiling stuff
"""Profiling code."""
import cProfile
import io
import pstats
import contextlib
def start():
"""Start and return profiler."""
@gwax
gwax / batched_results.py
Created May 10, 2016 17:35
Sqlalchemy result batching for inserts
def batch_results(results, batch_size=10000):
batch = results.fetchmany(batch_size)
while batch:
yield batch
batch = results.fetchmany(batch_size)
for batch in batch_results(conn.execute(query)):
new_rows = [dict(row) for row in batch]
# Modify new_rows here, if needed
@gwax
gwax / upgrade_python.sh
Last active February 19, 2016 02:12
Upgrade all the things!
#!/bin/bash
# Upgrade applications on system
brew update
brew upgrade --all
# Install latest python versions
pyenv install 2.7.11
pyenv install 3.5.1
# Make sure pip is up to date in new python versions
@gwax
gwax / s3_csv_list.py
Created February 9, 2016 19:56
Find everything on s3
import csv
import boto3
s3 = boto3.resource('s3')
with open('s3_catalog.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for s3_bucket in s3.buckets.all():
print(s3_bucket.name)
try:
for s3_obj in s3_bucket.objects.all():
@gwax
gwax / execute_from_iterable.py
Created January 9, 2016 01:50
Simple way to use an interable rather than a list for executing queries across many values.
import itertools
def execute_from_iterable(conn, query, values_iterable, batch_size=1000):
values_iterator = iter(values_iterable)
while True:
batch = list(itertools.islice(values_iterator, batch_size))
if not batch:
return
conn.execute(query, batch)
@gwax
gwax / .pullapprove.yml
Created December 2, 2015 17:58
A proposal for PullApprove configuration
vote_method:
comment:
approve:
- ":+1:"
- ":thumbsup:"
- approve
reject:
- ":-1:"
- ":thumbsdown:"
- reject
@gwax
gwax / db_timeing_tests.py
Created December 1, 2015 17:36
Comparing Schema dropping to DB dropping with testing.postgresql
import timeit
setup = """
import testing.postgresql
import sqlalchemy
postgres = testing.postgresql.Postgresql()
engine = sqlalchemy.create_engine(postgres.url())
conn = engine.connect()
conn.connection.connection.set_isolation_level(0)
"""