This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Must[T any](obj T, err error) T { | |
if err != nil { | |
panic(err) | |
} | |
return obj | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
from typing import Callable, TypeVar, Generic, Any, Dict, Tuple, List | |
T = TypeVar("T") | |
class TTLCache(Generic[T]): | |
def __init__( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def stopwatch(): | |
def decorator(func): | |
def wrapper(*args, **kwargs): | |
start_time = time.time() | |
result = func(*args, **kwargs) | |
end_time = time.time() | |
elapsed_time = end_time - start_time | |
print(f"time taken {elapsed_time: 2f}") | |
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as crypto from 'crypto' | |
const delimiter = '\0' | |
export function getFilesHash(files: { name: string; content: string }[]) { | |
const shasum = crypto.createHash('sha1') | |
files.forEach(({ name, content }) => { | |
shasum.update(name) | |
// Add delimiter to hash to prevent collisions between files where the join of the name and content is the same |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
characters = string.ascii_letters + string.digits | |
def id(length: int): | |
return "".join(random.choices(characters, k=length)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import json | |
from typing import TypeVar, Generic, List | |
T = TypeVar("T") | |
class DeferredFuture(Generic[T]): | |
def __init__(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from 'react' | |
export function useAsyncResource<T>(fetchResource: () => Promise<T>) { | |
const [data, setData] = useState<T>() | |
const [error, setError] = useState<any>() | |
useEffect(function getResource() { | |
fetchResource() | |
.then(setData) | |
.catch(setError) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function wait(ms: number) { | |
return new Promise(resolve => setTimeout(resolve, ms)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRouter } from 'next/router' | |
import { useCallback } from 'react' | |
type Base = Record<string, string | number> | |
interface SetTab<T> { | |
(tab: keyof T): void | |
} | |
function compareTabsCaseInsensitive<T extends Base>(tabs: T, tab?: string) { |
NewerOlder