Skip to content

Instantly share code, notes, and snippets.

Avatar

Ritvik Nag rnag

View GitHub Profile
@rnag
rnag / Dockerfile
Last active March 13, 2023 17:50
Dockerfile for `rembg-aws-lambda` in AWS environments
View Dockerfile
# Refs:
# - https://github.com/peterheb/rembg-lambda/issues/1#issue-1621934443
# - https://docs.aws.amazon.com/lambda/latest/dg/python-image.html
# - https://gallery.ecr.aws/lambda/python
FROM --platform=linux/amd64 public.ecr.aws/lambda/python:3.9-x86_64
LABEL maintainer="My Team"
ARG PYPI_URL
ARG PYPI_USER
@rnag
rnag / ts-compile.ts
Last active February 3, 2023 22:33
TypeScript Compiler API - Example
View ts-compile.ts
// TypeScript code to programatically run the equivalent of `tsc`
//
// With Slight modifications, such as *Colorized* output being displayed.
//
// However, the nice one-line Error Summary that `tsc` gives you (e.g. Errors in X files) is currently missing.
//
// Refs:
// - https://github.com/microsoft/TypeScript-wiki/blob/main/Using-the-Compiler-API.md
// - https://github.com/Microsoft/TypeScript/issues/6387#issuecomment-169739615
@rnag
rnag / cleanupEmptyFolders.ts
Last active February 3, 2023 22:20 — forked from arnoson/cleanupEmptyFolders.js
ts/node: remove empty directories recursively. `exclude` is a list of directories to not traverse (optimization)
View cleanupEmptyFolders.ts
import { readdirSync, rmdirSync, statSync } from 'node:fs';
import { basename, join } from 'node:path';
export const cleanupEmptyFolders = (
folder: string,
exclude: string[] = ['node_modules']
) => {
if (!statSync(folder).isDirectory()) return;
const folderName = basename(folder);
@rnag
rnag / validate_test_1.py
Last active September 2, 2022 14:55
Dataclass Type Validation
View validate_test_1.py
# dataclass type validation for fields on class instantiation -and- on assignment.
from dataclasses import dataclass
from validators import TypeValidator
@dataclass
class FooDC:
# alternatively, like:
@rnag
rnag / dump_json_to_term.py
Created August 23, 2022 14:11
measuring dump JSON to terminal times
View dump_json_to_term.py
from timeit import timeit
data = [
{
"eventStatus": "Failure",
"eventType": "Archive",
"objectName": "W12 BO Template",
"time": "2022-08-23T10:09:33.092Z"
},
{
@rnag
rnag / main.py
Last active August 25, 2022 20:58
dataclass re-decoration timing
View main.py
# See comment below for an explanation of what this script is testing!
from dataclasses import dataclass, is_dataclass
from timeit import timeit
@dataclass
class A:
a: int = 10
@rnag
rnag / json_field_remapping.py
Last active August 22, 2022 16:14
field/key remapping
View json_field_remapping.py
from dataclasses import dataclass
from .remappers import Alias, remapper # see module `remappers.py` below
@dataclass
class DiveSpot(metaclass=remapper):
id: str = Alias('_id')
name: str = Alias('divespot')
# stub to satisfy external linters and type checkers
@rnag
rnag / conftest.py
Last active September 22, 2022 04:40
pytest: mock builtin file open()
View conftest.py
# Prerequisites:
# $ pip install pytest pytest-mock
from functools import cache
from unittest.mock import MagicMock, mock_open
import pytest
from pytest_mock import MockerFixture
View git repush tag
1. While still on any branch
git tag -d v1.1
2. Create the tag again: This will "move" the tag to point to your latest commit on that branch
git tag v1.1
3. Delete the tag on remote
@rnag
rnag / trim_space.py
Last active April 14, 2022 14:42
trim_extra_whitespace_times
View trim_space.py
import re
import string
from time import time
def main():
# Rust version run with `--release` (for reference), using `only_one_string` from here:
# https://stackoverflow.com/a/71864244/10237506
# trim_whitespace: 30ms