This file contains hidden or 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, Any, Sequence | |
def fib_list(n=0): | |
# type: (int) -> Sequence[str] | |
fib_numbers: List[int] = [0, 1] | |
for _ in range(n): | |
fib_numbers.append(fib_numbers[-1] + fib_numbers[-2]) | |
return "adf" |
This file contains hidden or 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 Protocol | |
class SupportsClose(Protocol): | |
def close(self) -> None: | |
... | |
def finish_it(obj: SupportsClose): | |
obj.close() |
This file contains hidden or 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 pydantic import BaseModel | |
class GitlabUser(BaseModel): | |
id: int | |
username: str | |
class GitlabMr(BaseModel): | |
id: int |
This file contains hidden or 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 Optional | |
from dataclasses import dataclass | |
@dataclass | |
class Position: | |
longitude: float | |
latitude: float | |
address: Optional[str] = None |
This file contains hidden or 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
# Old style, before Python 3.7 | |
from collections import namedtuple | |
attribute_names = ["longitude", "latitude", "address"] | |
Position = namedtuple("Position", attribute_names, defaults=(None,)) | |
# Python 3.7 and later: | |
from typing import NamedTuple | |
class Position(NamedTuple): | |
longitude: int |
This file contains hidden or 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
@dataclass | |
class Position: | |
longitude: float | |
latitude: float | |
address: Optional[str] = None | |
@property | |
def latitude(self) -> float: | |
"""Getter for latitude.""" | |
return self._latitude |
This file contains hidden or 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 __post_init__(self): | |
if not (-180 <= self.longitude <= 180): | |
v = self.longitude | |
raise ValueError(f"Longitude was {v}, but must be in [-180, +180]") | |
if not (-90 <= self.latitude <= 90): | |
v = self.latitude | |
raise ValueError(f"Latitude was {v}, but must be in [-90, +90]") |
This file contains hidden or 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 Optional | |
from pydantic import validator | |
from pydantic.dataclasses import dataclass | |
@dataclass(frozen=True) | |
class Position: | |
longitude: float | |
latitude: float | |
address: Optional[str] = None |
This file contains hidden or 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 Optional | |
import attr | |
@attr.s | |
class Position: | |
longitude: float = attr.ib() | |
latitude: float = attr.ib() | |
address: Optional[str] = attr.ib(default=None) |
This file contains hidden or 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 Optional | |
class Position: | |
MIN_LATITUDE = -90 | |
MAX_LATITUDE = 90 | |
MIN_LONGITUDE = -180 | |
MAX_LONGITUDE = 180 | |
def __init__( |