Skip to content

Instantly share code, notes, and snippets.

View mahmoud's full-sized avatar
Back on the grid!

Mahmoud Hashemi mahmoud

Back on the grid!
View GitHub Profile
@mahmoud
mahmoud / migration_checker.sh
Last active January 24, 2019 22:05
Just a little thing to look for (potentially conflicting) migration changes across all branches. (linux/mac os/bsd compatible)
find . -type d -name migrations -print0 -exec sh -c 'echo && echo "Checking: '{}'" && git --no-pager log --exit-code --all --not master --stat --pretty=format:"%n%ad - %d - %an: %s" --no-merges --after="one week ago" -- '{}' && echo "No changes for: '{}'" || echo "Finished: '{}'"' \;
>>> x = [1]
>>> y = (2, 3)
>>> x + y
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
>>> x += y
>>> x
[1, 2, 3]
@mahmoud
mahmoud / get_leaf_paths.py
Created December 22, 2018 20:27
By request of @hynek :)
# https://twitter.com/hynek/status/1076048147074478080
from boltons.iterutils import remap
DATA = {"a": {"b": 1, "c": {"d": 2}}}
OUT = ["a.b", "a.c.d"]
def get_leaf_paths(root, with_val=False):
@mahmoud
mahmoud / zpool_replace_history.sh
Created November 19, 2018 08:51
first zpool replace on ubuntu
# needs to be parted'd before replacing.
$ sudo parted /dev/disk/by-id/ata-HGST_HDN724030ALE640_PK2234P9K09E0Y
GNU Parted 3.2
Using /dev/sdh
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel GPT
(parted) quit
# then we need to make the ashift=9 because this is an older zfs pool
$ sudo zpool replace -o ashift=9 liu 18069869553946727791 /dev/disk/by-id/ata-HGST_HDN724030ALE640_PK2234P9K09E0Y
@mahmoud
mahmoud / flatten_dict.py
Created August 20, 2012 08:55
An ok way to flatten nested dictionaries in Python (iteratively, not recursively). Even does basic circularity checks (untested at this point).
def flatten_dict(root, prefix_keys=True):
dicts = [([], root)]
ret = {}
seen = set()
for path, d in dicts:
if id(d) in seen:
continue
seen.add(id(d))
for k, v in d.items():
new_path = path + [k]
>>> class Lol:
... pass
...
>>> Lol()
<__main__.Lol instance at 0x7fb2bde403b0>
>>> x = Lol()
>>> x
<__main__.Lol instance at 0x7fb2bde403f8>
>>> x.__class__
<class __main__.Lol at 0x7fb2bde2ce88>
>>> z.gi_frame
<frame object at 0x7f21213b7548>
>>> z.__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'generator' object has no attribute '__dict__'
>>> z.__slots__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'generator' object has no attribute '__slots__'
$ python example.py sum --help
Usage: cmd sum [FLAGS]
Just a lil fun in the sum
Flags:
--flagfile FLAGFILE (defaults to None)
--help / -h

URLs and IPs

The URL standard is one which builds on IP standards. Operating systems also build on IP standards. Believe it or not, the two diverge in some surprising ways!

Hyperlink aims to do some validation of IPs contained within URLs with IP hosts. URLs support a subset of IP syntax, as well as making affordances for future versions of IPs beyond IPv6 (no matter how unfathomable that may seem).

@mahmoud
mahmoud / mp4_cut.py
Created January 6, 2018 09:13
a little something i whipped up for cleaner Pyninsula recording cuts
#!/usr/bin/env python
"""
./cut_mp4.py --input input.mp4 --start 00:00:01 --end 00:00:02 --no-align-keyframes --output output.mp4
"""
import os
import sys
import argparse
import datetime
import subprocess