Skip to content

Instantly share code, notes, and snippets.

View WomB0ComB0's full-sized avatar
πŸ˜΅β€πŸ’«
I need an Advil

Mike Odnis WomB0ComB0

πŸ˜΅β€πŸ’«
I need an Advil
View GitHub Profile
@WomB0ComB0
WomB0ComB0 / db.py
Created June 30, 2024 03:35
db-sqlite3 crud methods within a class (class Database)
from flask import g
import sqlite3
from typing import Dict, List
class Database:
def __init__(self, db_path: str):
self.db_path = db_path
def get_connection(self):
if "conn" not in g:
@WomB0ComB0
WomB0ComB0 / debounce.ts
Created July 1, 2024 02:54
Debounce function in Typescript
/**
* Debounces a function by delaying its execution until a certain amount of time has passed
* since the last time it was invoked. Only the last invocation within the delay period will be executed.
*/
export function debounce(fn: Function, time: number = 300): Function {
let timeoutId: ReturnType<typeof setTimeout>;
return function (this: any, ...args: any[]) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
fn.apply(this, args);
@WomB0ComB0
WomB0ComB0 / linkedlists.py
Created July 8, 2024 21:17
LikedList methods
class Node: # Creating a node
def __init__(self, item):
self.item = item
self.next = None
class LinkedList: # Creating a linked list
def __init__(self):
self.head = None
def print_list(self):
cur_node = self.head
@WomB0ComB0
WomB0ComB0 / mysql_db.py
Created July 9, 2024 23:36
Arbitrary MySQL database class in Python
class MySQLDatabase:
def __init__(self, database, user, password, host, port):
self.database = database
self.user = user
self.password = password
self.host = host
self.port = port
def connect(self):
return MySQLDatabase(self.database, self.user, self.password, self.host, self.port)
@WomB0ComB0
WomB0ComB0 / fetch.ts
Created July 10, 2024 19:54
TypeSafe http requests with zod validation
import { z } from "zod";
import { PostSchema } from "@/schema/posts";
import { CommentsSchema } from "@/schema/comments";
export function createAPIClient() {
async function _fetch<T extends z.ZodTypeAny>(
input: RequestInfo,
init: RequestInit,
type: T
): Promise<z.infer<T>> {
@WomB0ComB0
WomB0ComB0 / utils.ts
Created July 31, 2024 03:31
Custom Typescript Type Utils
type DeepReadonly<T extends Record<string, unknown>> = {
readonly [K in keyof T]: T[K] extends Record<string, any>
? T[K] extends (...args: Array<unknown>) => unknown
? T[K]
: DeepReadonly<T[K]>
: T[K];
};
type ReadonlyArray<T> = DeepReadonly<Array<T>>;
@WomB0ComB0
WomB0ComB0 / memoize.py
Created July 31, 2024 05:15
Meoize class Python
class Memoize:
def __init__(self, func: Callable) -> None:
self.func = func
self.cache = {}
def __call__(self, *args, **kwargs) -> Any:
if args not in self.cache:
self.cache[args] = self.func(*args, **kwargs)
return self.cache[args]
@WomB0ComB0
WomB0ComB0 / scraper.ts
Last active July 31, 2024 16:14
Simple text content web scraper with puppeteer
import puppeteer from "puppeteer";
import axios from "axios";
export const scraper = async (url: Readonly<string>): Promise<string[]> => {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(url, {
timeout: 0,
waitUntil: "domcontentloaded"
@WomB0ComB0
WomB0ComB0 / binarysearch_sting.ts
Created August 1, 2024 15:13
Binary search text in Typescript
const binarySearch = (source_text: string, target: string): [number, string] | null => {
const source_text_segments: string[] = (source_text.match(/[^.!?]+[.!?]+/g) || [source_text]).map(s => s.trim());
const target_segment = target.trim();
let result: [number, string] | null = null;
let left: number = 0;
let right: number = source_text_segments.length - 1;
while (left <= right) {
const mid = left + Math.floor((right - left) / 2);
const segment = source_text_segments[mid];
@WomB0ComB0
WomB0ComB0 / scraper-filtration.ts
Last active August 2, 2024 17:02
Advanced, general, website scraper
import puppeteer from 'puppeteer-extra';
import { LaunchOptions } from 'puppeteer';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
import AdblockerPlugin from 'puppeteer-extra-plugin-adblocker';
interface Row {
text: string;