Skip to content

Instantly share code, notes, and snippets.

View konradhalas's full-sized avatar

Konrad Hałas konradhalas

View GitHub Profile
from unittest import mock
# Approach #1 - hidden dependency
def func_1():
pass
# ugly
def register_user(user_data):
...
save_user_in_db(
email=user_data['email'],
first_name=user_data['first_name'],
...
)
<!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>
@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)
@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)

Bardzo krótki poradnik pisania dobrego kodu HTML/CSS

Założenia

Kod HTML/CSS napisany w poprawny sposób powinnien być:

  • semantyczny
  • zorientowany na komponenty
  • łatwy w modyfikacji
# return double of first number from collection which is greater than 3 and even
# this is Python 3 srcipt, in Python 2 you have to use itertools.imap and itertools.ifilter
def is_gt_3(number):
print('is_gt_3 called...')
return number > 3
def is_even(number):
print('is_even called...')
return number % 2 == 0
var LICENSE = 'License: For reuse of this video under a more permissive license please get in touch with us. The speakers retain the copyright for their performances.';
var WEBSITE = 'http://summit.pywaw.org';
var result = [];
$(".speaker-modal").each(function() {
var $speakerModal = $(this);
var $talkDetails = $speakerModal.find('.speaker-modal__talk-title');
var speakerName = $speakerModal.find('.speaker-modal__name').text();
var speakerBio = $speakerModal.find('p:not(.speaker-modal__twitter)').first().text();
var mixedTalksDescription = $speakerModal.find('p:not(.speaker-modal__twitter)').slice(1).text();
@konradhalas
konradhalas / extract_function.py
Created July 22, 2013 19:44
Simple solution how to extract function object from bound method.
def view_example(request):
print(request)
class ViewTestCase(object):
view_function = view_example
def __init__(self):
self.view = self.__class__.__dict__['view_function']
@konradhalas
konradhalas / utils.py
Created April 19, 2013 07:51
Model instance refresh (reload from DB) function, especially useful in tests.
def refresh(instance):
return instance.__class__._default_manager.get(pk=instance.pk)