Skip to content

Instantly share code, notes, and snippets.

View PeterJCLaw's full-sized avatar

Peter Law PeterJCLaw

View GitHub Profile
from typing import Any, Iterable, List, Tuple, Type, TypeVar, Union, Dict, Mapping, Generic
K = TypeVar('K')
T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
V = TypeVar('V')
list_of_string_type = [int] # type: List[Type[int]]
from typing import Any, Iterable, List, Tuple, TypeVar
T = TypeVar('T')
TList = TypeVar('TList', bound=List[Any])
untyped_list_str = ['abc', 'def']
typed_list_str = ['abc', 'def'] # type: List[str]
untyped_tuple_str = ('abc',)
typed_tuple_str = ('abc',) # type: Tuple[str]
@PeterJCLaw
PeterJCLaw / callable-bound.py
Last active January 16, 2020 23:20
Generics comparison
from typing import Any, Callable, TypeVar
R = TypeVar('R')
T = TypeVar('T')
TCallable = TypeVar('TCallable', bound=Callable[..., Any])
def demo(num: int) -> str:
return str(num)
@PeterJCLaw
PeterJCLaw / decorator-signatures.py
Last active January 13, 2020 14:50
Jedi signatures with decorators and type annotations
import functools
from typing import Any, cast, TypeVar, Callable
R = TypeVar("R")
# Roughly as suggested in https://github.com/davidhalter/jedi/issues/1425#issuecomment-565711589,
# though note that mypy complained about the arguments being `[...]` so I've
# changed to just `...`
def annotated_decorator(fn: Callable[..., R]) -> Callable[..., R]:
return fn
@PeterJCLaw
PeterJCLaw / vagrant box add ...
Last active October 5, 2019 21:47
vagrant box add fedora/29-cloud-base when ~/.vagrant.d/boxes is a symlink to an NTFS file system
==> box: Adding box 'fedora/29-cloud-base' (v29.20181024.1) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/fedora/boxes/29-cloud-base/versions/29.20181024.1/providers/virtualbox.box
box: Download redirected to host: download.fedoraproject.org
box: Download redirected to host: www.mirrorservice.org
/opt/vagrant/embedded/lib/ruby/2.4.0/fileutils.rb:1299:in `utime': Operation not permitted @ utime_internal - /home/peter/.vagrant.d/boxes/fedora-VAGRANTSLASH-29-cloud-base/29.20181024.1/virtualbox/metadata.json (Errno::EPERM)
from /opt/vagrant/embedded/lib/ruby/2.4.0/fileutils.rb:1299:in `copy_metadata'
from /opt/vagrant/embedded/lib/ruby/2.4.0/fileutils.rb:419:in `block in copy_entry'
from /opt/vagrant/embedded/lib/ruby/2.4.0/fileutils.rb:1394:in `wrap_traverse'
from /opt/vagrant/embedded/lib/ruby/2.4.0/fileutils.rb:413:in `copy_entry'
from /opt/vagrant/embedded/lib/ruby/2.4.0/fileutils.rb:471:in `rescue in block in mv'
@PeterJCLaw
PeterJCLaw / README.md
Last active November 9, 2019 16:41
Extracting files into a new git repo

Note: I've recently discoverded the git filter-repo tool, which is almost certainly better than the instructions below.


Steps for extracting files from one git repo into another:

  1. Grab a copy of the utils files from this gist (and ensure they're marked as executable)
  2. Clone the original repo to a new location: git clone $ORIGINAL /tmp/new-repo && cd /tmp/new-repo
  3. Break the connection from the original repo: git remote remove origin
  4. Create a branch of the starting point for easy reference
@PeterJCLaw
PeterJCLaw / squashfs-notes.md
Created April 3, 2019 16:32
Notes from updating the SR squashfs in zip-packager.git
@PeterJCLaw
PeterJCLaw / close
Created February 23, 2019 23:36
Schedule for 36 teams over 117 matches at 4 teams per match
Team Min-gap Count Gaps
25 4 1 [32, 8, 11, 10, 6, 8, 9, 10, 10, 7, 10, 4, 13]
23 4 2 [31, 11, 10, 9, 8, 4, 8, 14, 4, 15, 5, 9, 10]
29 4 1 [33, 7, 12, 7, 11, 9, 7, 4, 12, 5, 11, 9, 7]
1 4 1 [26, 10, 9, 9, 11, 11, 9, 11, 10, 6, 4, 12, 9]
3 4 1 [26, 12, 9, 9, 11, 11, 10, 4, 7, 8, 12, 7, 9]
2 4 1 [26, 11, 9, 9, 11, 11, 10, 10, 9, 4, 7, 8, 9]
7 4 1 [27, 11, 10, 9, 9, 10, 8, 11, 7, 13, 7, 4, 16]
6 4 1 [27, 10, 8, 8, 10, 12, 8, 9, 14, 6, 12, 9, 4]
9 4 1 [28, 8, 11, 6, 11, 13, 10, 10, 8, 4, 9, 11, 7]
@PeterJCLaw
PeterJCLaw / author-years.py
Created February 2, 2019 20:46
Script for listing the years each author contributed to a git repo
#!/usr/bin/env python3
import argparse
import functools
import subprocess
from pathlib import Path
from typing import Sequence
def compress(numbers: Sequence[int]) -> str:
@PeterJCLaw
PeterJCLaw / 0-README.md
Last active December 15, 2018 22:29
How to make asynchronous coroutines be concurrent

How to make asynchronous coroutines be concurrent

It took me a couple of attempts to really get async in Python, partly I suspect because I'd encountered it in other languages (notably C#) and already had a mental model of how it should behave.

My initial approach was reading the PEPs and having a go at trying to create some asynchronous functions myself, rather than starting out using a library. My aim was to understand the internals, rather than learn a library, as I prefer to understand the nuts & bolts of things than just learn a particular API over the