Skip to content

Instantly share code, notes, and snippets.

View rnag's full-sized avatar

Ritvik Nag rnag

View GitHub Profile
@rnag
rnag / .bashrc
Last active April 2, 2023 23:07 — forked from Frechet/git repush tag
# delete and re-create the latest tag on remote
rmtag () {
export TAG=$(git describe --tags --abbrev=0)
git tag -d $TAG
git tag $TAG
git push origin :$TAG
git push origin $TAG
}
@rnag
rnag / conftest.py
Last active February 17, 2024 09:09
pytest: mock builtin file open()
# Prerequisites:
# $ pip install pytest pytest-mock
from functools import cache
from unittest.mock import MagicMock, mock_open
import pytest
from pytest_mock import MockerFixture
@rnag
rnag / json_field_remapping.py
Last active April 6, 2024 11:37
field/key remapping
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 / main.py
Last active August 25, 2022 20:58
dataclass re-decoration timing
# 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 / dump_json_to_term.py
Created August 23, 2022 14:11
measuring dump JSON to terminal times
from timeit import timeit
data = [
{
"eventStatus": "Failure",
"eventType": "Archive",
"objectName": "W12 BO Template",
"time": "2022-08-23T10:09:33.092Z"
},
{
@rnag
rnag / validate_test_1.py
Last active July 28, 2023 06:22
Dataclass Type Validation
# 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 / 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)
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 / ts-compile.ts
Last active February 3, 2023 22:33
TypeScript Compiler API - Example
// 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 / Dockerfile
Last active April 2, 2023 23:35
Dockerfile for `rembg-aws-lambda` in AWS environments
# Note:
# Lock in build date on the tag (ex: 2023.03.29) - otherwise, we rebuild the
# entire Docker image each time a tag is updated (ex: python:3.9-x86_64)
#
# 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 (-> Image tags)
# Date (Timestamp) of the Image to Build
@rnag
rnag / a_instructions
Created January 19, 2024 02:05
VS Code (.vscode) - My Settings
Put below files in `.vscode` folder