Skip to content

Instantly share code, notes, and snippets.

View rnag's full-sized avatar

Ritvik Nag rnag

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / .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 / trim_space.py
Last active April 14, 2022 14:42
trim_extra_whitespace_times
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
@rnag
rnag / config.toml
Last active April 8, 2022 22:26
~/.cargo/config.toml
[alias]
d = "doc --no-deps --open"
rr = "run --release"
@rnag
rnag / jest.config.js
Created January 7, 2022 15:51
jest config
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
// Added so that jest prefer to run tests on files
// with the *.ts extension instead (see below).
// https://stackoverflow.com/a/50145833/10237506
@rnag
rnag / parameterStore.ts
Last active August 3, 2023 15:48 — forked from cbschuld/parameterStore.ts
Create or obtain values from the AWS Parameter store with Typescript/node
import { SSM } from 'aws-sdk';
/**
* Get the value for a parameter in SSM Parameter Store. By default, decrypt
* the value as we assume it is stored as a "SecretString"
*
* Ref: https://gist.github.com/cbschuld/938190f81d00934f7a158ff223fb5e02
*
* @param ssm The SSM client
* @param name Name of the parameter