Skip to content

Instantly share code, notes, and snippets.

View dankkom's full-sized avatar
🐢
I'm busy

dankkom dankkom

🐢
I'm busy
View GitHub Profile
@dankkom
dankkom / read_shp.R
Created April 4, 2024 16:48
read_shp(): R function to read zipped shapefiles
read_shp <- function(filepath) {
# If zipped
if (stringr::str_ends(filepath, ".zip")) {
temp1 <- tempfile()
unzip(zipfile = filepath, exdir = temp1)
d <- sf::read_sf(temp1)
unlink(temp1, recursive = TRUE)
return(d)
}
# If not zipped
@dankkom
dankkom / html_colors.tsv
Created November 20, 2023 18:23
HTML colors, names and RGB
color_name hex_rgb decimal_rgb type
aliceblue #F0F8FF 240,248,255 extended
antiquewhite #FAEBD7 250,235,215 extended
aqua #00FFFF 0,255,255 basic
aquamarine #7FFFD4 127,255,212 extended
azure #F0FFFF 240,255,255 extended
beige #F5F5DC 245,245,220 extended
bisque #FFE4C4 255,228,196 extended
black #000000 0,0,0 basic
blanchedalmond #FFEBCD 255,235,205 extended
@dankkom
dankkom / forecast_to_dataframe.R
Last active June 16, 2023 17:58
Functions to extract a data.frame from some forecast objects
ts_to_df <- function(ts_data) {
dates <- xts:::as.Date.POSIXct(zoo::yearmon(time(ts_data)))
values <- zoo::coredata(ts_data) |>
tibble::as_tibble()
tibble::tibble(date = dates) |>
dplyr::bind_cols(values)
}
HoltWinters_to_df <- function(fcst) {
xs <- ts_to_df(fcst$model$x) |>
@dankkom
dankkom / backup-database.py
Last active May 1, 2023 21:03
Script para fazer backup/dump de base de dados PostgreSQL
import argparse
import datetime as dt
import getpass
import os
import subprocess
import zlib
from pathlib import Path
def compute_crc32(filepath: Path) -> dict[str, str]:
@dankkom
dankkom / excel_deblock.py
Created March 31, 2023 13:08
Microsoft Excel deblocker: removes sheet protections from any spreadsheet
import argparse
import re
import shutil
import zipfile
from pathlib import Path
def decompress(filepath: Path) -> Path:
dest_filepath = Path(filepath.stem)
with zipfile.ZipFile(filepath, "r") as zf:
@dankkom
dankkom / powerbi_extractor.py
Created March 9, 2023 18:46 — forked from svavassori/powerbi_extractor.py
PowerBi Extractor in Python
import sys
import csv
import json
# Converts the JSON output of a PowerBI query to a CSV file
def extract(input_file, output_file):
input_json = read_json(input_file)
data = input_json["results"][0]["result"]["data"]
dm0 = data["dsr"]["DS"][0]["PH"][0]["DM0"]
@dankkom
dankkom / carnaval_date.py
Created January 8, 2023 20:51
Calculate Easter and Carnaval dates
import datetime as dt
def carnaval_date(year: int) -> tuple[dt.date]:
# https://www.vivaolinux.com.br/script/Calcular-a-data-do-Carnaval-e-da-Pascoa
x = 24
y = 5
a = year % 19
b = year % 4
c = year % 7
@dankkom
dankkom / decodificar_datasus_idade.R
Created July 20, 2022 11:42
Decodifica a coluna de idade dos microdados do DATASUS
decodificar_datasus_idade <- function(nu_idade_n) {
nu_idade_n <- as.character(nu_idade_n)
nu_idade_n[nu_idade_n == "000" | nu_idade_n == "999"] <- NA
unidade <- substr(nu_idade_n, 1, 1)
idade <- ifelse(
unidade == 1,
as.numeric(substr(nu_idade_n, 2, 3)) / (24*365), # horas
ifelse(
unidade == 2,
as.numeric(substr(nu_idade_n, 2, 3)) / 365, # dias
@dankkom
dankkom / convert_brl.py
Created October 27, 2021 19:01
Funções para convertes moedas brasileiras antigas.
import datetime
def convert_brl_date(value: float, date: datetime.date) -> float:
"""Convert brazillian currency to Real (BRL), given the date of the value.
Works with dates from 1942-11-01 to today.
"""
if date >= datetime.date(1994, 7, 1): # R$
return value
elif date >= datetime.date(1993, 8, 1): # CR$
@dankkom
dankkom / install-python-from-source.sh
Last active May 15, 2022 14:28
Install Python from source code
apt update
apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev liblzma-dev
PYTHON_VERSION=3.10.1
N_PROCESSORS=$(nproc)
wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz
tar -xf "Python-$PYTHON_VERSION.tgz"
cd "Python-$PYTHON_VERSION"