Skip to content

Instantly share code, notes, and snippets.

View konradhalas's full-sized avatar

Konrad Hałas konradhalas

View GitHub Profile
@konradhalas
konradhalas / namedtuple_example.py
Last active March 15, 2017 18:02
This is a short example of a new "typed" named tuple syntax (Python 3.6). It's a very clean and easy way to define data classes with a type hinting and inheritance support.
from typing import NamedTuple # >= Python.3.6.0
class Employee(NamedTuple):
name: str
department: str
salary: int
is_remote: bool = False # >= Python.3.6.1
bob = Employee(name='Bob', department='IT', salary=10000, is_remote=True)
@konradhalas
konradhalas / money.py
Last active February 20, 2018 16:36
Mypy shows the error of an unsupported comparison operator but this class has `@functools.total_ordering`, `==` and `<`, so `<=` is implemented for this type.
import decimal
import functools
import typing
@functools.total_ordering
class Money:
def __init__(self, amount: str) -> None:
self.amount = decimal.Decimal(amount)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{ person.name }} - {{ person.email }} <- autocompletion works like a charm
</body>
</html>
# ugly
def register_user(user_data):
...
save_user_in_db(
email=user_data['email'],
first_name=user_data['first_name'],
...
)
from unittest import mock
# Approach #1 - hidden dependency
def func_1():
pass