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 / stdlib_modules_random.py
Created April 22, 2022 11:58
Keep opening random stdlib modules' documentation in browser while user wants to continue
import sys
import random
import webbrowser
mods = list(sys.stdlib_module_names)
random.shuffle(mods)
for mod in mods:
if mod[0] == '_': # Skip Internal Modules
continue
webbrowser.open(f'https://docs.python.org/3/library/{mod}.html')
@sohang3112
sohang3112 / py-snippets.code-snippets
Last active August 4, 2022 05:00
My Custom Code Snippets in VS Code
{
// Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
@sohang3112
sohang3112 / cookies-as-object.js
Created June 29, 2022 04:30
Get cookies as an Object
Object.fromEntries(document.cookie.split('; ').map(s => s.split('=')))
@sohang3112
sohang3112 / random_manpages.sh
Created June 29, 2022 04:35
Repeatedly show random Manpages (forever!)
#!/usr/bin/sh
# A Random Manpage shows, you read it, then press "q"
# ... and then you get the next random manpage
# Exit using Ctrl+Z (SIGKILL)
for cmd in `man -k . | cut -d ' ' -f1 | shuf`; do
man $cmd;
done
@sohang3112
sohang3112 / gui-from-shell.md
Last active October 8, 2022 01:15
Open GUI program, detached from shell
#!/bin/bash
if [ $# -gt 0 ] ; then
  ($@ &) &>/dev/null
else
  cat <<-EOM
    Usage: gui [COMMAND] [ARG]...
    
    Runs a command in GUI mode, i.e., detached completely from the executing shell and terminal.
 If no command is given, this help message is shown.
@sohang3112
sohang3112 / hide_all_morphs.md
Created June 29, 2022 04:46
Delete from view all instances of a `Morph` class

Delete all open instances of class CircleMorph:

CircleMorph allInstances do: [ :circle | circle delete ]

Or, a slightly shorter way:

CircleMorph allInstancesDo: [ :circle | circle delete ]
@sohang3112
sohang3112 / resolve_html_props.md
Created June 29, 2022 04:57
Resolve HTML Properties into a `Map`, while also dealing with missing / default property values

Description

In HTML, while defining elements, property values can be omitted. Here, the htmlProps function takes a list of property keys or values, and resolves it into a Map of keys to values by the following rules: - A Key followed by a Val associates the key with the val in result map. - A Key followed by another Key means the first key has value Nothing in result map. - A Val without a Key before it indicates an error.

Code

@sohang3112
sohang3112 / render_html.py
Last active November 1, 2023 10:34
Render an HTML string in the default web browser
import webbrowser
from tempfile import NamedTemporaryFile
def view_html(html: bytes, **kwargs) -> bool:
"""
Open HTML input string as a webpage in default browser
@return - True if page opened successfully, False otherwise
"""
with NamedTemporaryFile(suffix='.html', delete=False, **kwargs) as f:
f.write(html)
@sohang3112
sohang3112 / try_keys_in_order.py
Created June 29, 2022 05:41
Try keys in order on a dict-like object
from typing import Mapping
def try_keys(obj: Mapping, *keys, default_value=None):
"""
Tries each given key (in order) on obj (a dict-like object) and
returns value of first key found in object. If no key is found in object,
then it returns default_value.
>>> try_keys({'a': 1}, 'a', 'b', default_value=0)
1
@sohang3112
sohang3112 / init-with-setters.clj
Created June 29, 2022 05:44
Declaratively initialize an object having setter methods
(defmacro with-attrs [obj-exp & args]
`(doto ~obj-exp
~@(for [[key val] (partition 2 args)]
(list (->> key
name
capitalize
(str ".set")
symbol)
val))))