Skip to content

Instantly share code, notes, and snippets.

@mawillcockson
mawillcockson / keybase.md
Created January 26, 2018 00:53
Proof of identity for https://keybase.io/

Keybase proof

I hereby claim:

  • I am mawillcockson on github.
  • I am mawillcockson (https://keybase.io/mawillcockson) on keybase.
  • I have a public key ASDtn4sIGl7SslZu-ioUZwJ0JWd_6HC49F14VxV4YDP7kAo

To claim this, I am signing this object:

@mawillcockson
mawillcockson / as_int.py
Created December 28, 2020 11:56
transforms a dotted quad IPv4 address into a single, unsigned 32-bit integer
#!/bin/env python
"""
https://twitter.com/dave_universetf/status/1342688553264762880
"""
import sys
from typing import List
def split_by_dots(dotted_quad: str) -> List[int]:
"turns '1.2.3.4' into [1,2,3,4]"
@mawillcockson
mawillcockson / twitter.sh
Created December 30, 2020 12:11
Compare Manhattan distance calculations implemented in python
# https://twitter.com/IsntTrivial/status/1344185584013742086
echo "sum(map(abs, map(operator.sub, p, q)))"
python -m timeit \
-s "import operator;p=q=range(-10_000, 10_000, 1);" \
-s "def manhattan_distance(p, q): return sum(map(abs, map(operator.sub, p, q)))" \
"manhattan_distance(p, q)"
echo ""
echo "sum(abs(x - y) for x, y in zip(p, q))"
python -m timeit \
@mawillcockson
mawillcockson / install-virt-bootstrap.sh
Last active April 13, 2021 22:12
Install virt-bootstrap on debian
#!/bin/sh
# This is a very dumb script: run it more than once, and it will mess things up.
#
# This is meant to be run from as a regular user.
#
# At the end, when the command virt-manager is run from bash, virt-manager will
# be launched with access to virt-bootstrap.
#
# To uninstall, remove the following directories and files:
# ~/.local/lib/virt-bootstrap
@mawillcockson
mawillcockson / example.py
Created February 7, 2021 22:23
[Python] subclass pathlib.Path
"""
example of subclassing pathlib.Path
the current downside is repr() shows a PosixPath or WindowsPath
mypy --strict is satisfied by the call to cast(), and as long as the __new__()
method returns an object of type pathlib.Path, everything else should work,
since the created object will be the same as if it were created with pathlib.Path
mypy will complain, as it does not like the Path type on the last line
@mawillcockson
mawillcockson / example.py
Last active February 7, 2021 23:06
[Python] practical subclassing of pathlib.Path
"""
NOTE: This sort of "subclassing" only allows modifying how the object is instantiated.
Other methods added to deriving classes aren't present on the instance.
For example, the following fails with an AttributeError:
class Example(StrictPath, Path):
"example subclass with a method"
def method(self) -> None:
@mawillcockson
mawillcockson / minimum_reproducible.py
Created February 8, 2021 04:18
[Python] linting bug
"""
neither pylint nor mypy catch this error, even with the most stringent checks
"""
def func() -> int:
"raises an exception"
raise RuntimeError("i can't do it :(")
@mawillcockson
mawillcockson / bug.py
Created February 9, 2021 00:44
[Python] bug in black
"""
black --version
black, version 20.8b1
comments indicate how black reformats the above line
all of these are treated as valid in Python 3.6+
"""
from typing import List, Tuple
name: List[Tuple[float]] = []
@mawillcockson
mawillcockson / find_number_phrase_equal.py
Created March 5, 2021 20:39
what binary operations on positive integers result in a value that's equal to the count of characters in the names of the numbers in the calculation
# mypy: allow-any-expr
"""
usage: add_number_phrase [[start] stop]
finds binary operations of addition, subtraction, division, or multiplication
that result in a value that's equal to the number of letters of all of the
names of the numbers used in the calculation
start: number to start at (1 if not specified)
stop: number to stop at
@mawillcockson
mawillcockson / timer.py
Last active March 13, 2021 16:10
Extremely basic cli timer
"""
tells the time difference between input events
"""
import math
from datetime import datetime, timedelta
from typing import List
def now() -> datetime:
"returns a timezone-aware object for the time when it's called"