Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
nathan-cruz77 / flatten.py
Created December 16, 2023 18:40
Flattens nested lists/tuples into a single flat list.
# Flattens nested lists/tuples into a single flat list. Other typles are
# returned as-is.
#
#
# Sample usage:
#
# >>> flatten([1, 2, 3, 4])
# [1, 2, 3, 4]
#
# >>> flatten([1, [2, 3], 4])
@nathan-cruz77
nathan-cruz77 / enumerate_n.py
Created December 8, 2023 06:23
Enumerate iterables recursively
from collections.abc import Iterable
# Enumerate iterable containing iterables recursively.
#
# Like `enumerate` but can recursively go into nested iterables yielding each
# entry's index as a tuple.
#
#
# Sample usage:
#
@nathan-cruz77
nathan-cruz77 / transcode.sh
Created September 21, 2022 21:24
FFMpeg transcoding with Nvidia acceleration
INPUT=input.mp4
OUTPUT=output.mp4
ffmpeg \
-hwaccel cuda \
-hwaccel_output_format cuda \
-i $INPUT \
-vf 'hwdownload,format=nv12,hwupload' \
-c:a copy \
-c:v h264_nvenc \
@nathan-cruz77
nathan-cruz77 / uri_parser.ex
Created June 7, 2020 22:15
Parse query params and extract UUIDs from URIs
defmodule URIParser do
@doc """
Extracts path, query params and UUIDs from the given URI and return them as a map.
## Examples
iex> URIParser.parse("https://example.com/resource/3776a9b5-5f69-4d9e-be5e-74e574df02d6?page[number]=5&page[size]=3")
%{
path: "/resource/:id",
query: %{"page[number]" => "5", "page[size]" => "3"},
@nathan-cruz77
nathan-cruz77 / zip.ex
Last active November 12, 2022 17:56
Equivalent to python's zip_longest function for elixir.
defmodule Zip do
@doc """
Zips corresponding elements from a finite collection of enumerables into one list of tuples.
The zipping finishes when the longest enumerable is finished. Default padding value is `nil`.
Usage:
iex> Bla.zip_longest([1, 2, 3], [1, 2])
[{1, 1}, {2, 2}, {3, nil}]
@nathan-cruz77
nathan-cruz77 / hashing.py
Created October 4, 2019 12:17
Simple custom object that can be used inside sets.
class Hashable:
def __init__(self, arg):
self.arg = arg
def __hash__(self):
return hash(self.arg)
def __eq__(self, other):
return self.__hash__() == other.__hash__()
@nathan-cruz77
nathan-cruz77 / hashing.py
Created October 4, 2019 12:17
Simple custom object that can be used inside sets.
class Hashable:
def __init__(self, arg):
self.arg = arg
def __hash__(self):
return hash(self.arg)
def __eq__(self, other):
return self.__hash__() == other.__hash__()
@nathan-cruz77
nathan-cruz77 / phone_validator.py
Last active September 7, 2019 03:04
E.164 phone number validator
import re
# Validates whether a phone number is in E.164 (https://en.wikipedia.org/wiki/E.164)
# format.
#
# Sample usage:
#
# >>> is_e164('55 (12) 98154-1281')
# True
#
@nathan-cruz77
nathan-cruz77 / fold.py
Created August 22, 2019 03:13
Fold iterable
from itertools import chain
# Fold iterable into itself.
#
#
# Sample usage:
#
# >>> list(fold([1, 2, 3]))
# [1, 2, 3, 2, 1]
#
@nathan-cruz77
nathan-cruz77 / proxy_pass.ex
Last active July 8, 2019 13:15
Dynamic proxy pass in elixir
defmodule ProxyPass do
@docs """
Dynamically delegate function calls to another module.
Usage:
defmodule RepoA do
def insert(), do: IO.puts("Called from RepoA")
end