Skip to content

Instantly share code, notes, and snippets.

defmodule Foo.Helpers do
@doc """
Validates that all keys in the input params are defined in the schema.
Adds an error to the changeset if any unexpected keys are found.
`validate_no_unused_attributes/2` accepts a list of permitted keys that might not be in the schema.
## Examples
iex> params = %{"bar" => "bar", "baz" => "baz", "unexpected" => "wow"}
defmodule SecureSandbox do
@forbidden_modules [File, System, Code, Process, :erlang]
@forbidden_functions [:eval, :apply, :spawn, :send, :exit]
def new do
Agent.start_link(fn -> %{binding: [], env: __ENV__} end)
end
def eval(pid, code) do
Agent.get_and_update(pid, fn state ->

state

SELECT
  queue,
  count(1) FILTER (WHERE state = 'available'),
  count(1) FILTER (WHERE state IS NULL OR state != 'available')
FROM oban_jobs
GROUP BY queue;
@vhf
vhf / avg.exs
Created April 11, 2024 15:48
oban job average duration
Oban.Job
|> where([j], j.completed_at > ago(5, "minute"))
|> select([j], fragment("EXTRACT(epoch FROM avg(? - ?))::int", j.completed_at, j.attempted_at))
|> Repo.one()
iex(foo@10.12.165.73)29> Node.list |> Enum.map(fn node -> Enum.map(1..100, fn _ -> {us, :pong} = :timer.tc(Node, :ping, [node])
...(foo@10.12.165.73)29> us/2 end)|> Enum.reduce(fn x, sum -> sum + x end) end) |> Enum.map(fn x -> x / 100 end)
[413.585, 436.995]
import chess
import chess.pgn
import chess.engine
from chess import BLACK, WHITE
engine = chess.engine.SimpleEngine.popen_uci("./stockfish_20090216_x64_avx2")
import bz2
path = "lichess_db_standard_rated_2017-04.pgn.bz2"
f = bz2.open(path, "rt", encoding="ascii")
@vhf
vhf / foo.ex
Created September 25, 2020 11:04
Number of weekdays between two dates (elixir)
defmodule Foo do
def bar(first_day, second_day) do
days_between = Date.diff(second_day, first_day) + 1
dow1 = Date.day_of_week(second_day)
dow2 = Date.day_of_week(second_day)
weekend_days =
Kernel.floor((days_between + dow2) / 7 * 2) +
if(dow1 == 7, do: 1, else: 0) - if(dow2 == 6, do: 1, else: 0)
@vhf
vhf / fix-svg.js
Last active March 31, 2018 14:24
rehype svg attributes fix
// complete list of camelCase attributes
// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute
const camelAttributes = [
'allowReorder', 'attributeName', 'attributeType', 'autoReverse',
'baseFrequency', 'baseProfile', 'calcMode', 'clipPathUnits', 'contentScriptType',
'contentStyleType', 'diffuseConstant', 'edgeMode', 'externalResourcesRequired',
'filterRes', 'filterUnits', 'glyphRef', 'gradientTransform', 'gradientUnits',
'kernelMatrix', 'kernelUnitLength', 'keyPoints', 'keySplines', 'keyTimes', 'lengthAdjust',
'limitingConeAngle', 'markerHeight', 'markerUnits', 'markerWidth', 'maskContentUnits',
'maskUnits', 'numOctaves', 'pathLength', 'patternContentUnits', 'patternTransform',
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vhf
vhf / example.js
Created July 13, 2017 16:39 — forked from Mayeu/example.js
Remark fix new lines for GitLab
const fixNewLine = require('./index.js')
const remark = require('remark')
text = "lol\nlol\n```\nlol\nlol\n```\n> lol\n> lol"
processed = remark()
.use(fixNewLine)
.processSync(text)
console.log(processed)