Skip to content

Instantly share code, notes, and snippets.

View antonagestam's full-sized avatar
🍉

Anton Agestam antonagestam

🍉
View GitHub Profile
@antonagestam
antonagestam / ssn.sql
Created October 28, 2020 09:36
Postgres SE SSN constraint
CREATE OR REPLACE FUNCTION se_ssn_valid (VARCHAR(12))
RETURNS boolean
AS $$
SELECT
char_length($1) = 12
AND date_part('year', age(now(), to_timestamp(substring($1, 1, 8), 'YYYYMMDD'))) > 0
AND (
(select sum(t) from unnest(regexp_split_to_array((substring($1, 3, 1)::int * 2)::text, '')::int[]) as t)
+ (select sum(t) from unnest(regexp_split_to_array((substring($1, 4, 1)::int * 1)::text, '')::int[]) as t)
+ (select sum(t) from unnest(regexp_split_to_array((substring($1, 5, 1)::int * 2)::text, '')::int[]) as t)
@antonagestam
antonagestam / matches.py
Created June 21, 2020 20:16
best matches
# from https://github.com/nemethf/mypy/blob/6e2a5ee1aedba9ec41996aa5a04ec68826a90851/mypy/messages.py#L2028
import difflib
def best_matches(current: str, options: Iterable[str]) -> List[str]:
ratios = {v: difflib.SequenceMatcher(a=current, b=v).ratio() for v in options}
return sorted((o for o in options if ratios[o] > 0.75),
reverse=True, key=lambda v: (ratios[v], v))
pytest -k errors_for_invalid_action_signature
==================================================================== test session starts ====================================================================
platform darwin -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /Users/anton/.pyenv/versions/django-stubs/bin/python3
cachedir: .pytest_cache
rootdir: /Users/anton/projects/django-stubs, inifile: pytest.ini, testpaths: ./test-data
plugins: mypy-plugins-1.2.0
collected 170 items / 169 deselected / 1 selected
test-data/typecheck/contrib/admin/test_options.yml::errors_for_invalid_action_signature
INTERNALERROR> Traceback (most recent call last):
phantom/sized.py:5: error: Module 'typing' has no attribute 'Final' [attr-defined]
from typing import Final
^
phantom/sized.py:61: error: Expression has type "Any" [misc]
and not isinstance(instance, mutable)
^
phantom/re.py:1: error: Module '__future__' has no attribute 'annotations'
[attr-defined]
from __future__ import annotations
^
@antonagestam
antonagestam / example.md
Created May 7, 2020 07:37
Beginner-friendly improvements of git.
$ git checkout dev
Switched to branch 'dev'
Your branch is up to date with 'origin/dev'.

Suggest improving copy here to make it clear that the branch might or might not be up to date with the remote origin/dev. "Your branch is up to date with 'origin/dev'." only means we haven't committed any changes to dev since we last synced.

@antonagestam
antonagestam / filterdict.py
Last active April 6, 2020 09:36
A subclass of dict that filters keys and values
from typing import (
Any,
Callable,
Dict,
Generic,
Hashable,
Iterable,
Mapping,
Optional,
Tuple,
@antonagestam
antonagestam / exports.py
Last active February 24, 2020 15:57
Make bash export statements from docker env file
"""
$ python3 export.py <(echo -e '# this is a comment\nKEY=value123')
export KEY='value123'
$ (. <(python3 export.py <(echo 'KEY=value123')) && echo $KEY)
value123
"""
import re
import fileinput
pattern = re.compile("^(?P<key>[A-z_]+)=(?P<value>.+)$", re.M)
@antonagestam
antonagestam / chain.ts
Last active January 18, 2020 22:48
Variadic chain function for arbitrary number of iterables
export function* chain<A>(...iterables: Iterable<A>[]): Iterable<A> {
for (const iterable of iterables) yield* iterable
}
def inclusive_range(start: int, stop: int) -> range:
"""
>>> list(inclusive_range(0, -1))
[0, -1]
"""
if stop > start:
return range(start, stop + 1)
return range(start, stop - 1, -1)
from typing import Any
from typing import Mapping
import hcl
from immutables import Map
from typing_extensions import Final
class Settings:
__frozen = False