Skip to content

Instantly share code, notes, and snippets.

View grihabor's full-sized avatar

Gregory Borodin grihabor

View GitHub Profile
@grihabor
grihabor / attribute-definition-placement-bad.py
Last active November 1, 2017 19:50
Define attributes all over the code on the fly #bad
class X:
def __init__(self):
self.a = 'a'
def run(self, b)
self.b = 'b'
x = X()
x.c = 'c'
x.run('b')
print(x.a, x.b, x.c)
@grihabor
grihabor / attribute-definition-placement-good.py
Last active October 31, 2017 22:41
Define all class attributes inside the constructor
class X:
def __init__(self):
self.a = 'a'
self.b = None
self.c = None
def run(self, b)
self.b = b
x = X()
x.c = 'c'
@grihabor
grihabor / __init__-example.py
Last active November 1, 2017 07:41
Create object instance with custom constructor #example
class Color:
def __init__(color, r, g, b):
color.r = r
color.g = g
color.b = b
white = Color(255, 255, 255)
print(white.r, white.g, white.b)
#|255 255 255
@grihabor
grihabor / __add__-example.py
Last active November 1, 2017 07:40
Implement operator + for a custom class #example
class Color:
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
def __add__(self, other):
return Color(
(self.r + other.r) // 2,
(self.g + other.g) // 2,
(self.b + other.b) // 2,
@grihabor
grihabor / class-definition-example.py
Last active November 1, 2017 07:39
Define python class #example
class Color:
pass
print(Color)
#|<class '__main__.Color'>
@grihabor
grihabor / class-instance-example.py
Last active November 1, 2017 07:38
Create class instance #example
class Color:
pass
empty = Color()
print(empty)
#|<__main__.Color object at 0xf709fff0>
@grihabor
grihabor / track-time-decorator-practical.py
Last active November 1, 2017 07:37
Print function execution time if decorated #practical
from datetime import datetime
import functools
def track_time(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
start = datetime.now()
result = f(*args, **kwargs)
print('Run {}: {}'.format(f.__name__, datetime.now() - start))
return result
@grihabor
grihabor / function-name-decorator-practical.py
Last active November 1, 2017 06:53
Print the name of the function if decorated #practical
from functools import wraps
def print_function_name(f):
@wraps(f)
def wrapper(*args, **kwargs):
print('Call: {}'.format(f.__name__))
return f(*args, **kwargs)
return wrapper
@print_function_name
@grihabor
grihabor / query.sql
Last active February 26, 2018 10:16
Get products which are first products that people buy
select
ProductID,
sum(case
when first_order_datetime is null then 0
else 1
end)
from order_lines
left join (
select
min(Datetime) as first_order_datetime,
@grihabor
grihabor / Makefile.version
Last active May 27, 2024 13:36
Makefile to use for incremental semantic versioning
MAKE := make --no-print-directory
DESCRIBE := $(shell git describe --match "v*" --always --tags)
DESCRIBE_PARTS := $(subst -, ,$(DESCRIBE))
VERSION_TAG := $(word 1,$(DESCRIBE_PARTS))
COMMITS_SINCE_TAG := $(word 2,$(DESCRIBE_PARTS))
VERSION := $(subst v,,$(VERSION_TAG))
VERSION_PARTS := $(subst ., ,$(VERSION))