Skip to content

Instantly share code, notes, and snippets.

View grihabor's full-sized avatar

Gregory Borodin grihabor

View GitHub Profile
@grihabor
grihabor / golang.lark
Created November 11, 2020 20:51
Lark ebnf grammar for golang
go_file: decl_package [imports] ([decl_type] [decl_func])*
decl_package: "package" ident
imports: "import" single_import
| "import" multiple_imports
single_import: ["("] ESCAPED_STRING [")"]
multiple_imports: "(" ESCAPED_STRING+ ")"
stmt: stmt_var
| stmt_for
| stmt_if
@grihabor
grihabor / client.py
Last active October 5, 2018 13:56
Simple python client-server
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 50010))
msg_to_send = 'Hello world!'
bytes_to_send = msg_to_send.encode('utf-8')
print('Send to the server:', msg_to_send)
s.send(bytes_to_send)
@grihabor
grihabor / Makefile.version
Last active April 24, 2024 18:18
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))
@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 / 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 / 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 / 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 / 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 / __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 / __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