Skip to content

Instantly share code, notes, and snippets.

View x's full-sized avatar
🐦

Devon Peticolas x

🐦
View GitHub Profile
(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
(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]
@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))))
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 / 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")
@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 / decode_anything_probably.py
Created April 1, 2021 22:28
Decode anything probably
In [1]: import chardet
In [2]: s = b'200 \xb5l Ultra Po'
In [3]: chardet.detect(s)
Out[3]: {'encoding': 'ISO-8859-1', 'confidence': 0.73, 'language': ''}
In [4]: s.decode(chardet.detect(s)['encoding'])
Out[4]: '200 µl Ultra Po'
@x
x / inner_join.py
Created April 30, 2021 07:10
pandas range inner join
In [3]: df1 = pd.DataFrame({"start": [1,2,6], "end": [2,6,9]})
...: df2 = pd.DataFrame({"foo": [1,2,3,4,5,6,7,8,9]})
...: dfm = pd.merge(df1, df2, how="cross")
...: dfm.loc[(dfm.foo >= dfm.start) & (dfm.foo < dfm.end)]
Out[3]:
start end foo
0 1 2 1
10 2 6 2
11 2 6 3
12 2 6 4
@x
x / windows.java
Created August 1, 2021 22:03
Apache Beam Summit - Windowing Example
package io.oden.laser.common.transforms;
import org.apache.beam.sdk.transforms.windowing.*;
import org.apache.beam.sdk.transforms.windowing.Window.OnTimeBehavior;
import org.joda.time.Duration;
public class Windows {
/*
* The Window described attempts to both be prompt but not needlessly retrigger.
* It's designed to account for the following cases...
@x
x / petscii.sh
Created August 16, 2021 16:47
PETSCII Maze in One-Line Python
python3 -c "while 1: print(chr(int(9585.5 + __import__('random').random())), end='')"