Skip to content

Instantly share code, notes, and snippets.

@jim-my
jim-my / cy.html
Last active March 23, 2025 10:26
Cytoscape.js - Dependency Graph. Cytoscape.js can handle thousands of nodes easily(choice of layout is important). E.g. if viz.js or d3-graphviz doesn't work.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dependency Graph Visualizer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.26.0/cytoscape.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dagre/0.8.5/dagre.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape-dagre/2.5.0/cytoscape-dagre.min.js"></script>
<style>
@jim-my
jim-my / format-sql-sqlglot.py
Created March 23, 2025 01:59
Format SQL query using sqlglot library(sqlfluff might be a better option sometimes?)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Format SQL query using sqlglot library."""
import argparse
import sys
import typing as t
import sqlglot
import sqlglot.expressions as exp
@jim-my
jim-my / format-sql-sqlparse.py
Last active March 23, 2025 01:38
SQL formatter using sql-parse(because it doesn't do strict validation, it can also work with template files)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlparse
def format_sql(sql):
"""Format SQL query using sqlparse library."""
return sqlparse.format(
sql,
reindent=True,
import pandas as pd
from typing import Any, Optional
def discrete_median(series: pd.Series) -> Optional[Any]:
"""Compute the discrete median for any sortable Series (numeric, string, datetime, categorical).
- For numbers, returns the middle value (without interpolation).
- For strings, returns the lexicographically middle element.
- For datetime, returns the middle timestamp.
- For categorical, uses category order if available.
@jim-my
jim-my / python-view-module.py
Last active February 2, 2025 13:58
Open given python module's source code in VSCode
#!/usr/bin/env python
import importlib
import re
import os.path
import site
import sys
from typing import List, Optional
def print_usage() -> None:
@jim-my
jim-my / saml2aws.py
Last active January 21, 2024 04:57
(code) How to Implement a General Solution for Federated API/CLI Access Using SAML 2.0
#!/usr/bin/python3
"""
Note:
* This snippet is copied from https://awsiammedia.s3.amazonaws.com/public/sample/SAMLAPICLIADFS/0192721658_1562696767_blogversion_samlapi_formauth_python3.py
* It's part of this AWS blog: https://aws.amazon.com/blogs/security/how-to-implement-a-general-solution-for-federated-apicli-access-using-saml-2-0/
* saml2aws is based on this blog, but saml2aws is implemented in Golang(see https://github.com/Versent/saml2aws/blob/11b0c262d857b582e5a62a7b4aaa5314fcdbcbd2/README.md?plain=1#L9C1-L10C1).
Summary:
@jim-my
jim-my / dict_namedtuple.py
Created September 22, 2021 21:04 — forked from href/dict_namedtuple.py
Convert any dictionary to a named tuple
from collections import namedtuple
def convert(dictionary):
return namedtuple('GenericDict', dictionary.keys())(**dictionary)
"""
>>> d = dictionary(a=1, b='b', c=[3])
>>> named = convert(d)
>>> named.a == d.a
True
>>> named.b == d.b
@jim-my
jim-my / .zshrc lazy_load()
Last active September 19, 2021 04:52 — forked from QinMing/.zshrc
.zshrc lazy_load(): (lazy loading shell functions)
# Copyright (c) 2016-2018 Ming Qin (覃明) <https://github.com/QinMing>
# Open source under MIT LICENSE.
lazy_load() {
# Act as a stub to another shell function/command. When first run, it will load the actual function/command then execute it.
# E.g. This made my zsh load 0.8 seconds faster by loading `nvm` when "nvm", "npm" or "node" is used for the first time
# $1: space separated list of alias to release after the first load
# $2: file to source
# $3: name of the command to run after it's loaded
# $4+: argv to be passed to $3
@jim-my
jim-my / _aws_golang_examples.md
Created September 3, 2020 11:42 — forked from eferro/_aws_golang_examples.md
golang aws: examples

AWS Golang SDK examples

@jim-my
jim-my / web-servers.md
Created August 24, 2020 12:10 — forked from willurd/web-servers.md
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000