Skip to content

Instantly share code, notes, and snippets.

View PeterJCLaw's full-sized avatar

Peter Law PeterJCLaw

  • Marks & Spencer
  • United Kingdom
View GitHub Profile
@PeterJCLaw
PeterJCLaw / remote-setup.sh
Created December 20, 2020 14:24
Running webots on Digial Ocean
#!/bin/sh -ex
### As root
apt update
apt upgrade --yes
wget https://github.com/cyberbotics/webots/releases/download/R2020b-rev1/webots_2020b-rev1_amd64.deb -O /webots_2020b-rev1_amd64.deb
apt install --yes python3-venv xvfb /webots_2020b-rev1_amd64.deb
@PeterJCLaw
PeterJCLaw / crons.py
Created October 9, 2020 12:10
Helper for finding DLQ crons which run at a given time
from django_lightweight_queue.cron_scheduler import get_cron_config
cc = get_cron_config()
crons_then = set()
for x in cc:
for hour in (22, 23, 0):
for minute in range(3):
if (
@PeterJCLaw
PeterJCLaw / aliases.sh
Created July 23, 2020 18:32
Aliases useful for linting changes in a Python project in a git repo
# Python Project things
run-py-branch() { git diff origin/master... --name-only --diff-filter=d | grep '\.py$' | xargs --no-run-if-empty "$@"; }
run-py-local() { git diff --name-only --diff-filter=d | grep '\.py$' | xargs --no-run-if-empty "$@"; }
run-py-cache() { git diff --cached --name-only --diff-filter=d | grep '\.py$' | xargs --no-run-if-empty "$@"; }
isort-branch() { run-py-branch isort; }
isort-local() { run-py-local isort; }
isort-cache() { run-py-cache isort; }
flake8-branch() { run-py-branch flake8; }
@PeterJCLaw
PeterJCLaw / git-fixup
Last active May 21, 2020 22:29
Command to fixup a single historical git commit
#!/bin/sh
# I found this (or something like it) on a blog at one point and now can't find it.
# If you know where this came from feel free to comment and I'll happily add attribution.
# Installation: put this in your $PATH as an executable file.
#
# Usage:
# - stage the changes you want applied
# - run `git fixup $COMMIT`
@PeterJCLaw
PeterJCLaw / README.md
Last active March 6, 2020 23:59
Debug a robot.py using VSCode

Debug a robot.py using VSCode

These instructions will allow you to debug robot code running on a Student Robotics kit using [VSCode][vscode].

  1. Setup [VSCode][vscode] locally
  2. Add the following to the project to debug:
    # Near the top of `robot.py`
    import ptvsd

ptvsd.enable_attach()

@PeterJCLaw
PeterJCLaw / demo.py
Created February 23, 2020 23:36
jedi handling of annotated non-ClassVar class attributes
from typing import ClassVar
class A:
cls_attr = 1
cls_attr_comment = 2 # type: int
cls_attr_classvar_comment = 2 # type: ClassVar[int]
cls_attr_hint: int = 3
cls_attr_classvar_hint: ClassVar[int] = 3
@classmethod
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