Skip to content

Instantly share code, notes, and snippets.

View sohang3112's full-sized avatar
:octocat:

Sohang Chopra sohang3112

:octocat:
View GitHub Profile
@sohang3112
sohang3112 / git_squash_merge_commits.py
Created December 4, 2025 19:16
Shrink .git folder size by converting merge commits into normal squash commits
# 'Squash Merge' video by anthonywritescode, this is script shown at timestamp 16:50 :
# https://youtu.be/5_8FTivl8Vs?si=krcdLDevrAC_DLuo&t=1013
# Uses https://github.com/newren/git-filter-repo (python script)
# Explanation: `git cat-file commit COMMIT_ID` shows parent commit id
# normal commits have 1 parent, merge commits have 2 parents
# this script simply deletes 1 parent (keeping only parent id of main branch),
# converting merge commit into normal (squash) commit
# An impressive example of this script is shown,
@sohang3112
sohang3112 / gpg_sign_commits_github.md
Created December 3, 2025 12:09
GitHub: Create GPG key and sign commits with it (required by some open source projects)
$ gpg --full-generate-key
$ gpg --list-secret-keys --keyid-format=long
$ gpg --armor --export GPG_KEY_ID    # get key id from prev list command - see details in above link

Copy output of last command (gpg armor export). Go to Github > Settings > SSH and GPG Keys > New GPG Key and paste it.

@sohang3112
sohang3112 / pdf2ipynb.py
Created November 17, 2025 12:53
Convert PDF files to Jupyter Notebook .ipynb file
#! /usr/bin/env python3
import os
import fitz # PyMuPDF
import nbformat
# Source: https://www.reddit.com/r/learnpython/comments/1ji0qwz/comment/mjblesy/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
def pdf_to_ipynb(pdf_path: os.PathLike, ipynb_path: os.PathLike) -> None:
"""Convert PDF -> Jupyter Notebook .ipynb file."""
doc = fitz.open(pdf_path)
@sohang3112
sohang3112 / compress_consecutive_chars.hs
Last active April 13, 2025 08:33
(Haskell) Compresses a string by counting consecutive repeating characters.
{-# LANGUAGE BangPatterns #-}
import Data.List (foldl')
-- | Compresses a string by counting consecutive repeating characters.
--
-- Returns a list of tuples where each tuple consists of a character and the
-- number of its consecutive occurrences in the string.
--
-- This function uses 'reverse', 'foldl'' and bang patterns for strict accumulation.
@sohang3112
sohang3112 / git_fork.sh
Created April 8, 2025 21:10
Fork repo to a different Git platform.
# Example of forking ziglings repo from CodeBerg to Github
UPSTREAM=https://codeberg.org/ziglings/exercises.git
[email protected]:sohang3112/ziglings.git # mk empty Github repo & copy SSH url (as Github doesn't support push with HTTPS)
git clone $UPSTREAM ziglings/ # (optional argument) used custom cloned folder name ziglings/
cd ziglings/ # go to cloned repo
git remote set-url origin $FORK
git remote add upstream $UPSTREAM
git push
@sohang3112
sohang3112 / default_dict_implementation.py
Last active February 25, 2025 21:50
Re-implementing DefaultDict (from Python standard library) using __missing__ dunder method
"""Using dict __missing__() to implement DefaultDict."""
from typing import Any, Callable
class DefaultDict(dict):
"""Implementing typing.DefaultDict.
A dict that returns a default value (instead of raising KeyError) when a key is missing.
>>> d = DefaultDict(list, {'a': 1, 'b': 2})
>>> d['a']
@sohang3112
sohang3112 / indian_population_estimate_regression.ipynb
Created December 4, 2024 03:45
Regression (assuming exponential population growth) Indian population size as of 2021 (since last census was in 2011)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sohang3112
sohang3112 / github_push_hanged.md
Created October 11, 2024 02:49
Bugfix: Github push (ssh) hanged

Problem

git push and git pull hanged (using SSH url).

Investigation

  • Enable tracing in git to check what's wrong:
$ GIT_TRACE=1 git push
trace: built-in: git push
trace: run_command: unset GIT_PREFIX; ssh [email protected] 'git-receive-pack '\''sohang3112/machine-learning-practice.git'\'''
trace: start_command: /usr/bin/ssh [email protected] 'git-receive-pack '\''username/repo.git'\'''
@sohang3112
sohang3112 / intset.py
Last active August 22, 2024 07:02
Efficient set of integers, maintained using bit shift operations on an integer.
from __future__ import annotations
from typing import Iterable
class IntSet:
"""
Efficiently store integers in a set - internally uses bit shift operations.
NOTE: len() isn't supported - instead cast to list first: len(list(intset))
>>> IntSet(0b10011) # Set of all the bits which are set to 1 (using 1-based bit indexing)
@sohang3112
sohang3112 / html_attributes.hs
Created August 22, 2024 05:59
Simple Haskell function to handle HTML attributes that may or may not have a value specified.
-- Simple function to handle HTML attributes that may or may not have a value specified.
import qualified Data.Map as Map
import Data.Map (Map)
-- HTML Property Key | Value
type Prop k v = Either k v
key = Left
val = Right