This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_bigger(n: int) -> int: | |
''' | |
post: __return__ != 100 | |
''' | |
return 2 * n + 10 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_bigger(n: int) -> int: | |
''' | |
post: __return__ != 0 | |
''' | |
return 2 * n + 10 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Final | |
class Cat: | |
legs: Final[int] = 4 | |
def walk(cat: Cat, strides: int) -> int: | |
''' | |
pre: strides > 0 | |
post: __return__ >= 4 | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import TypedDict | |
class Movie(TypedDict): | |
name: str | |
year: int | |
def age(movie: Movie) -> int: | |
''' | |
pre: move.age < 2020 | |
post: __return__ >= 0 |
OlderNewer