Skip to content

Instantly share code, notes, and snippets.

View rkhullar's full-sized avatar

Rajan Khullar rkhullar

View GitHub Profile
@rkhullar
rkhullar / traverse_prev_next.py
Last active March 15, 2023 14:37
traverse iterable with prev and next context
from collections.abc import Iterable, Iterator
from typing import TypeVar
T = TypeVar('T')
def _scan(items: Iterable[T]) -> Iterator[tuple[T, T]]:
"""iterator to traverse previous and current items from iterable"""
prev, curr, stream = None, None, iter(items)
@rkhullar
rkhullar / generator_util.py
Created October 28, 2022 18:18
captured iteration - yield and return
from collections.abc import Iterator
from dataclasses import dataclass, field
from typing import Generic, Optional, TypeVar
T = TypeVar('T')
V = TypeVar('V')
@dataclass
class CapturedIteration(Generic[T, V]):
#!/usr/bin/env python3
import os
import re
import subprocess
from argparse import ArgumentParser, Namespace
from dataclasses import dataclass, field
def build_parser() -> ArgumentParser:
@rkhullar
rkhullar / test.js
Created August 25, 2021 13:24
javascript-dictionary-comprehension
function pipe(data, fn) {
if (typeof data === 'object' && data != null) {
return Object.fromEntries(
Object.keys(data).map(key => [key, fn(key, data[key])])
);
}
}
const example = {
hello: 1,
@rkhullar
rkhullar / python-decorator-tutorial-01.py
Last active July 3, 2021 20:08
python benchmarking example using decorator
import functools
import time
class timeit:
"""decorator for benchmarking"""
def __init__(self, fmt='completed {:s} in {:.3f} seconds'):
# there is no need to make a class for a decorator if there are no parameters
self.fmt = fmt
def __call__(self, fn):