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
import unicodedata
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
@dankkom
dankkom / cubic_interpolation_1d.py
Created July 16, 2020 16:42
1D Cubic Interpolation in Python w/o SciPy
from math import sqrt
import numpy
# https://stackoverflow.com/a/48085583
def cubic_interp1d(x0, x, y):
"""
Interpolate a 1-D function using cubic splines.
x0 : a float or an 1d-array
x : (N,) array_like
@dankkom
dankkom / package_version.py
Created September 7, 2020 16:26
Single-sourcing the package version
import codecs
import os.path
def read(rel_path):
here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, rel_path), 'r') as fp:
return fp.read()
@dankkom
dankkom / utm2latlon.py
Created November 9, 2020 14:23
[Python] Convert geographic coordinates from UTM to latitude/longitude
from typing import Tuple
from pyproj import Proj
def convert_utm_to_longlat(x: int, y: int, zone: int, south: bool = False) -> Tuple[float, float]:
longlat_proj = Proj(proj="utm", zone=zone, ellps="WGS84", south=south)
long, lat = longlat_proj(x, y, inverse=True)
return long, lat
@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"
@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 / 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 / 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 / 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 / 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: