Skip to content

Instantly share code, notes, and snippets.

View x's full-sized avatar
🐦

Devon Peticolas x

🐦
View GitHub Profile
@x
x / decode_with_abstract.py
Created April 1, 2021 21:20
Unicode decode with abstract
try:
message.decode('utf-8')
except UnicodeDecodeError as err:
abstract = message[max(err.start - 10, 0):min(err.end + 10, len(message))].decode(encoding, 'backslashreplace')
logging.error(f"Error decoding as UTF-8: {uuid} Problem Area: '{abstract}' Error: {err}")
@x
x / get_in.py
Last active July 12, 2020 01:26
from typing import Any, List, Optional
def get_in(x: Any, ks: Optional[List[Any]], default: Any = None):
"""Recursively get nested values from dicts and lists in python.
A better alternative to .get(...) chains and safe for index errors on lists.
Example:
>>> get_in({"foo": ["a", {"bar": "b"}]}, ["foo", 1, "bar"])
"b"
>>> get_in([1, 2], [7], default="foo")
from __future__ import absolute_import
import argparse
import logging
import re
import apache_beam as beam
from apache_beam.io import ReadFromText, WriteToText
from apache_beam.metrics import Metrics
from apache_beam.metrics.metric import MetricsFilter
@x
x / lazy.clj
Created October 11, 2016 22:46
lazy seq that prints
(def my-lazy-seq
(let [i (volatile! 0)]
(repeatedly
#(let [j (vswap! i inc)]
(println j)
j))))
(defn new-model
[num-clicks ts]
(LiveCpmModel. num-clicks 1.0 0.0 ts))
(defrecord LiveCpmModel
[^double a ^double g ^double v ^long lua]
(estimateCpm
[this ts]
(require '[clojure.string :refer [join]])
(defprotocol ExpressionBuilderProtocol
(ensureExprAttrName [this val])
(ensureExprAttrVal [this val])
(ADD [this attr amnt])
(SET [this attr val])
(toArgs [this]))
(defrecord ExpressionBuilder
@x
x / bash_chartbeat.sh
Last active February 25, 2016 16:49
.bash_chartbeat, some bash functions I wrote that make my job easier
# How To Use
# In order to use .bash_chartbeat, you need to do two things
# 1. set the PATH_TO_CHARTBEAT_REPO environment variable in your ~/.bash_profile or ~/.bash_rc
# 2. source this file in your ~/.bash_profile or ~/.bash_rc
#
# By default, OSX only sources your ~/.bash_profile when you start a new
# terminal session. Add these two things to that file.
# export PATH_TO_CHARTBEAT_REPO=$HOME/chartbeat # Or wherever your repo is
# source $PATH_TO_CHARTBEAT_REPO/.bash_chartbeat
@x
x / burndown.py
Last active November 20, 2019 08:03
Given a CSV export of an asana project, generate a tab-seperated table of burn down numbers that google sheets can easily translate into a burn down chart.
#! /usr/bin/python
"""
usage: burndown [-h] [--start START] [--end END] [--retro] fname
positional arguments:
fname
optional arguments:
-h, --help show this help message and exit
--start START start of sprint (inclusive)
@x
x / cprofile_decorator.py
Created June 21, 2015 22:41
cprofile decorator
import cProfile
def with_profile(fn):
def _profile_fn(*args, **kwargs):
prof = cProfile.Profile()
ret = prof.runcall(fn, *args, **kwargs)
print "profile for:", fn.__name__
prof.print_stats()
return ret
return _profile_fn
@x
x / atomic-cache-example.clj
Created April 22, 2015 16:08
Atomic clojure.cache example
(require '[[clojure.cache :as cache]
[clojure.string :as string]
[cb-statsd :as statsd]])
(def STATSD_SAMPLE_RATE 0.01)
(defn get-statsd-path
[f]
(as-> (str (type f)) fname ;; returns something like "class namespace$foo"
(last (string/split fname #" ")) ;; parse out the "class "