Skip to content

Instantly share code, notes, and snippets.

View Hassan-Naeem-code's full-sized avatar
🎯
Focusing

Hassan Naeem Hassan-Naeem-code

🎯
Focusing
View GitHub Profile
@Hassan-Naeem-code
Hassan-Naeem-code / openai_streaming.py
Created April 21, 2026 01:28
Python: OpenAI streaming chat with live token printing and usage tracking
"""
Streaming chat example with live token printing and usage tracking.
Install:
pip install openai
Env:
export OPENAI_API_KEY=sk-...
"""
@Hassan-Naeem-code
Hassan-Naeem-code / openai_starter.py
Created April 21, 2026 01:28
Python: OpenAI API starter with a stable system prompt — first-request pattern for chatbots, extractors, and agents
"""
OpenAI API starter with a stable system prompt.
Simple first-request pattern you can build on for chatbots, extractors, and agents.
Install:
pip install openai
Env:
export OPENAI_API_KEY=sk-...
"""
@Hassan-Naeem-code
Hassan-Naeem-code / ab-testing.ts
Created April 21, 2026 01:14
TypeScript: tiny A/B testing helper — deterministic variant assignment, zero dependencies
// Tiny A/B testing helper — deterministic variant assignment, no external service.
// Hash the user ID + experiment key, map to a variant based on weights.
// Same user always gets the same variant; safe for SSR and client.
type Weights = Record<string, number>;
export interface Experiment<V extends string = string> {
key: string;
variants: Record<V, number>; // weights, any positive numbers; they're normalized
// Optional: target only a subset of users (e.g. beta cohort).
@Hassan-Naeem-code
Hassan-Naeem-code / ValidatedField.dart
Created April 21, 2026 01:14
Flutter: reusable form field with composable validators (required, email, min/max length, pattern, matches)
// Reusable Flutter form field with consistent styling, validation, and error display.
// Works with any StatefulWidget that uses Form + GlobalKey<FormState>.
//
// Usage:
// final _formKey = GlobalKey<FormState>();
//
// Form(
// key: _formKey,
// child: Column(children: [
// ValidatedField(
@Hassan-Naeem-code
Hassan-Naeem-code / oidc-node.js
Created April 21, 2026 01:14
Node.js: OIDC Authorization Code + PKCE flow in ~100 lines — works with any compliant provider
/*
* OIDC Authorization Code flow + PKCE in ~100 lines of Node.
* Demonstrates the three endpoints you need:
* GET /login -> redirect to the provider's authorize URL
* GET /callback -> exchange the auth code for tokens
* GET /userinfo -> call the provider's userinfo endpoint
*
* Works with any OIDC-compliant provider (Auth0, Okta, Google, Keycloak, Cognito...).
*
* Install:
@Hassan-Naeem-code
Hassan-Naeem-code / playwright_starter.py
Created April 21, 2026 01:14
Python: Playwright automation patterns — iframe traversal, humanlike drag, canvas screenshots
"""
Playwright browser automation starter — patterns I keep reusing.
Handles common real-world issues: iframes, nested iframes, canvas screenshots,
humanlike dragging, and graceful install-on-first-run.
Install:
pip install playwright
python -m playwright install
"""
@Hassan-Naeem-code
Hassan-Naeem-code / nextjs-api-route.ts
Created April 21, 2026 01:14
Next.js App Router API route template — auth, Zod validation, typed error responses
// Next.js App Router API route template — auth, Zod validation, typed errors.
// File: app/api/posts/route.ts
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { headers } from "next/headers";
// --- Validation schemas ---
const CreatePostSchema = z.object({
title: z.string().min(1).max(200),
@Hassan-Naeem-code
Hassan-Naeem-code / opencv_recipes.py
Created April 21, 2026 01:14
Python: OpenCV recipes — I/O, color spaces, resize, thresholding, edges, contours, masking, drawing
"""
OpenCV recipes I keep reaching for.
Every function is self-contained — copy the ones you need.
Install:
pip install opencv-python numpy
"""
import cv2
import numpy as np
@Hassan-Naeem-code
Hassan-Naeem-code / prompt-engineering-patterns.md
Last active April 21, 2026 01:28
Prompt engineering patterns I actually use — 12 practical techniques for shipping with LLMs

Prompt engineering patterns I actually use

Not a comprehensive list — just the ones that give me real lift on real projects. Each pattern is a tool; pick the one that fits the problem.

1. Role + task + constraints, in that order

You are a senior SQL engineer.
Your task: rewrite the query below to use a window function instead of a self-join.
Constraints: preserve the exact column order. Do not change column names.
@Hassan-Naeem-code
Hassan-Naeem-code / token_cost.py
Last active April 21, 2026 01:27
Python: Token counting + cost estimation for OpenAI API calls
"""
Token counting + cost estimation for OpenAI API calls.
Update the PRICING dict when prices change.
Install:
pip install tiktoken openai
"""
from dataclasses import dataclass
import tiktoken