Skip to content

Instantly share code, notes, and snippets.

View pschanely's full-sized avatar

Phillip Schanely pschanely

View GitHub Profile
@pschanely
pschanely / main.py
Created February 5, 2020 12:36
Shared via mypy Playground
def make_bigger(n: int) -> int:
'''
post: __return__ != 100
'''
return 2 * n + 10
@pschanely
pschanely / main.py
Created February 7, 2020 19:37
Shared via CrossHair Playground
class HasConsistentHash:
'''
A mixin to enforce that classes have hash methods that are consistent
with thier equality checks.
'''
def __eq__(self, other: object) -> bool:
'''
post: implies(__return__, hash(self) == hash(other))
'''
raise NotImplementedError
@pschanely
pschanely / main.py
Created February 7, 2020 22:42
Shared via CrossHair Playground
class HasConsistentHash:
'''
A mixin to enforce that classes have hash methods that are consistent
with thier equality checks.
'''
def __eq__(self, other: object) -> bool:
'''
post: implies(__return__, hash(self) == hash(other))
'''
raise NotImplementedError
@pschanely
pschanely / main.py
Created February 8, 2020 05:09
Shared via CrossHair Playground
from typing import List
def average(numbers: List[float]) -> float:
'''
pre: len(numbers) >= 0
post: min(numbers) <= __return__ <= max(numbers)
'''
return sum(numbers) / len(numbers)
@pschanely
pschanely / main.py
Created February 8, 2020 05:19
Shared via CrossHair Playground
from typing import List
def csv_last_column(lines: List[str]) -> List[str]:
'''
Gets you the last column from a csv.
We use CrossHair to confirm our index arithmetic is correct.
pre: all(',' in line for line in lines)
post: __return__ == [line.split(',')[-1] for line in lines]
'''
@pschanely
pschanely / main.py
Created February 9, 2020 19:52
Shared via CrossHair Playground
import dataclasses
from typing import List
@dataclasses.dataclass
class AverageableStack:
'''
A stack of numbers with a O(1) average() operation.
inv: self._total == sum(self._values)
'''
_values: List[int]
@pschanely
pschanely / main.py
Created February 9, 2020 19:55
Shared via CrossHair Playground
import dataclasses
from typing import List
@dataclasses.dataclass
class AverageableQueue:
'''
A queue of numbers with a O(1) average() operation.
inv: self._total == sum(self._values)
'''
_values: List[int]
@pschanely
pschanely / main.py
Created February 9, 2020 20:06
Shared via CrossHair Playground
def make_bigger(n: int) -> int:
'''
post: __return__ != 0
'''
return 2 * n + 10
@pschanely
pschanely / main.py
Created February 11, 2020 09:46
Shared via CrossHair Playground
from typing import Final
class Cat:
legs: Final[int] = 4
def walk(cat: Cat, strides: int) -> int:
'''
pre: strides > 0
post: __return__ >= 4
'''
@pschanely
pschanely / main.py
Created February 11, 2020 10:02
Shared via CrossHair Playground
from typing import TypedDict
class Movie(TypedDict):
name: str
year: int
def age(movie: Movie) -> int:
'''
pre: move.age < 2020
post: __return__ >= 0