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 / group_equal.py
Last active December 13, 2023 02:48
Group consecutive equal items of iterable
# Group equal consecutive items of iterable.
#
#
# Sample usage:
#
# >>> group_equal('aaabbcd')
# [['a', 'a', 'a'], ['b', 'b'], ['c'], ['d']]
#
def group_equal(iterable):
subgroups = []
@nathan-cruz77
nathan-cruz77 / sliding_window.py
Last active December 9, 2023 18:41
Sliding window for iterables.
from itertools import islice
# Slides through iterable each `n` elements.
#
# Sample usage:
#
# >>> list(sliding_window([1, 2, 3, 4]))
# [(1, 2), (2, 3), (3, 4)]
#
# >>> list(sliding_window([1, 2, 3]))
@nathan-cruz77
nathan-cruz77 / grouper.py
Last active December 9, 2023 18:31
Group each N elements of the iterable.
from itertools import zip_longest
# Group each `n` elements of the iterable, filling with `fillvalue` if needed.
#
#
# Sample usage:
#
# >>> list(grouper([1, 2, 3, 4, 5, 6], 2))
# [(1, 2), (3, 4), (5, 6)]
#
@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 / 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 / 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 / text_to_whatsapp.js
Last active August 26, 2022 02:08
Whatsapp text formatting
// Regex to format text to whatsapp format
function whatsToHtml(msg) {
if (!msg) return;
return msg
.replace(/~(~*[^~\n]+~*)~/g, '<del>$1</del>')
.replace(/_(_*[^_\n]+_*)_/g, '<em>$1</em>')
.replace(/\*(\**[^*\n]+\**)\*/g, '<strong>$1</strong>');
}
@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 / util.ex
Last active May 18, 2020 18:58
Elixir's equivalent of Rails blank? and present? methods
defmodule Util do
defmacro __using__(_) do
quote do
require Util
# Should it be imported?
# import Util
end
end
defmacro blank?(x) when is_binary(x) do