Skip to content

Instantly share code, notes, and snippets.

@wrgoldstein
wrgoldstein / psql_wire_protocol.exs
Last active February 16, 2023 01:26
A simple tcp server that speaks postgres, ported from https://gist.github.com/eatonphil/079f8654174b9b9c1953b76008d024ae
"""
Elixir server that partially speaks the PG wire protocol. Run:
```
mix run psql_wire_protocol.exs
```
and run psql at a separate terminal prompt:
```

Simple physics:

  • velocity = distance / time
  • momentum = mass x velocity

An analogy:

  • The distance traveled by a single developer in a set amount of time is "velocity"
  • The movement of the organization as a whole is "momentum" (the sum of individual velocities)
  • If you add more people but reduce velocity then momentum decreases (this is why small teams are sometimes more productive than big teams).
"""
A cli to populate a local database with production
data. Expects a companion file called tables.py to exist
with the following structure:
```python
# tables.py
# any tables that should be downloaded in their entirety
small_tables = [
g_poisson = fn lambda ->
# generate poisson random variables according to
# the Knuth method
l = :math.exp(-lambda)
Stream.unfold({0, 1}, fn
{k, p} ->
u = p * :random.uniform()
{{k, u}, {k+1, u}}
end)
|> Stream.drop_while(fn {k, p} -> p > l end)
# imagine each of these is of the form {uuid, timestamp}
# can definitely be refactored but wanted to learn about `Flow.emit_and_reduce/3`
data = [
{:a, 1},
{:a, 2},
{:b, 2},
{:a, 5},
{:a, 11},
{:a, 12},
# thanks https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.s status
alias kx=kubectx
alias kn=kubens
alias k=kubectl
alias u=ultralist
alias ez="code ~/.zshrc"
alias z="source ~/.zshrc"
export EDITOR="code --wait"
eval "$(pyenv init --path)"

Sometimes in a large project you may have many scripts using the same python package, and want to upgrade that package for only some of them.

PIP makes this easy: install the older (or newer) version in its own directory, and import it explicitly.

pip install -r requirements-prev.txt --target prev
from prev import pandas as pd
defmodule Exstuf do
@doc """
Illustration of the philosopher dining problem.
Reimplementation of https://www.golangprograms.com/go-language/concurrency.html
## Examples
iex> Exstuf.start()
for _ <- 1..5, do: :satisfied
delete from schema.table using (
select *,
row_number() over (partition by id order by created_at desc) as row
from schema.table
) duplicates
where table.id = duplicates.id
and table.created_at = duplicates.created_at
and row > 1