Skip to content

Instantly share code, notes, and snippets.

Avatar
🏡
Playing catch up!

Mahmoud Hashemi mahmoud

🏡
Playing catch up!
View GitHub Profile
@mahmoud
mahmoud / remerge.py
Last active Jun 14, 2021
Recursively merging dictionaries with boltons.iterutils.remap. Useful for @hynek's configs. https://twitter.com/hynek/status/696720593002041345
View remerge.py
"""
This is an extension of the technique first detailed here:
http://sedimental.org/remap.html#add_common_keys
In short, it calls remap on each container, back to front, using the accumulating
previous values as the default for the current iteration.
"""
@mahmoud
mahmoud / scheme_port_map.json
Last active Jun 7, 2021
A big mapping url schemes to their protocols' default ports. See comments below for notes. Painstakingly assembled by crossreferencing https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml and https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml
View scheme_port_map.json
{
"acap": 674,
"afp": 548,
"dict": 2628,
"dns": 53,
"file": null,
"ftp": 21,
"git": 9418,
"gopher": 70,
"http": 80,
@mahmoud
mahmoud / hashtag.py
Last active May 14, 2021
hashtag regex in python
View hashtag.py
import re
# the first group is noncapturing and just ensures we're at the beginning of
# the string or have whitespace before the hashtag (don't want to capture anchors)
# without the fullwidth hashmark, hashtags in asian languages would be tough
hashtag_re = re.compile("(?:^|\s)[##]{1}(\w+)", re.UNICODE)
@mahmoud
mahmoud / conftest.py
Last active Mar 10, 2021
Excerpted tidbits from our Django app's conftest during the Django 1 to 2 and Python 2 to 3 migration ("TR" stands for "Tech Refresh", circa mid 2020), with parts related to wrapping the test session in a DB transaction, and other parts related to bulk-skipping functionality. Tested on py2.7/3.6, Django 1.11/2.0.x, pytest 4.6.11.
View conftest.py
# -*- coding: utf-8 -*-
"""
Common test fixtures for pytest
"""
from __future__ import print_function, unicode_literals
import os
from itertools import groupby
import pytest
@mahmoud
mahmoud / design.py
Last active Jul 28, 2020
improved branched glom error ascii design
View design.py
before = \
'''
Traceback (most recent call last):
File "tmp.py", line 9, in _make_stack
glom(target, spec)
File "/home/mahmoud/projects/glom/glom/core.py", line 2024, in glom
raise err
glom.core.PathAccessError: error raised while processing, details below.
Target-spec trace (most recent last):
@mahmoud
mahmoud / py3_except.py
Created Jun 26, 2020
python3 exception var deleting
View py3_except.py
$ python3
Python 3.7.7 (default, Mar 10 2020, 17:25:08)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> e = 'hi'
>>> try:
... 1/0
... except Exception as e:
... pass
...
@mahmoud
mahmoud / py3_partials.py
Created Mar 17, 2020
TIL py3 partials don't mind multiple values for arguments
View py3_partials.py
>>> def f(a, b):
... return a, b
...
# py27
>>> partial(f, b=1)(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'b'
@mahmoud
mahmoud / basic_argparse_framework.py
Last active May 1, 2019
(based on years of argparse experience, also serves to demonstrate why argparse is not so good)
View basic_argparse_framework.py
def create_parser():
root_parser = ArgumentParser(description='article fetch operations')
root_parser.add_argument('--list_home', help='list lookup directory')
add_subparsers(root_parser.add_subparsers())
return root_parser
def add_subparsers(subparsers):
parser_list = subparsers.add_parser('list')
parser_list.add_argument('target_list', nargs='?',
@mahmoud
mahmoud / migration_checker.sh
Last active Jan 24, 2019
Just a little thing to look for (potentially conflicting) migration changes across all branches. (linux/mac os/bsd compatible)
View migration_checker.sh
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: '{}'"' \;
View list_meets_tuple.py
>>> 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]