Skip to content

Instantly share code, notes, and snippets.

View rkhullar's full-sized avatar

Rajan Khullar rkhullar

View GitHub Profile
@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):
@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,
#!/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 / 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]):
@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 / k8s_client.py
Last active April 4, 2023 14:30
custom python kubernetes client
from collections.abc import AsyncIterator
from dataclasses import dataclass, field
from httpx_util import BearerAuth, async_httpx
# https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/CoreV1Api.md
# https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/BatchV1Api.md
@dataclass(frozen=True)
@rkhullar
rkhullar / httpx_util.py
Created April 4, 2023 14:31
httpx util
from dataclasses import dataclass
import httpx
async def async_httpx(method: str, *args, verify: bool = True, **kwargs):
async with httpx.AsyncClient(verify=verify) as client:
fn = getattr(client, method)
return await fn(*args, **kwargs)
@rkhullar
rkhullar / python-iter-batch-v2.py
Created April 4, 2023 14:32
python iter batch v2
from typing import Iterator, TypeVar, List
T = TypeVar('T')
def iter_batch(stream: Iterator[T], batch_size: int) -> Iterator[List[T]]:
assert batch_size > 0
buffer, item_count = [None] * batch_size, 0
for item in stream:
buffer_index = item_count % batch_size
@rkhullar
rkhullar / python-iter-batch-v1.py
Created April 4, 2023 14:33
python iter batch v1
from typing import Iterator, List, TypeVar
T = TypeVar('T')
def iter_batch(sequence: Iterator[T], size: int) -> Iterator[List[T]]:
count, buffer = 0, [None] * size
for item in sequence:
idx = count % size
buffer[idx] = item
@rkhullar
rkhullar / frozen-dataclass-with-mutable-field.py
Created April 25, 2023 22:16
frozen dataclass with mutable field
from dataclasses import dataclass, field
from typing import Generic, TypeVar
T = TypeVar('T')
@dataclass
class Proxy(Generic[T]):
_data: T