Skip to content

Instantly share code, notes, and snippets.

@jbasko
jbasko / logbook_example.py
Created November 15, 2016 15:08
Logbook setup example
"""
API request logger:
One record per each API request received
1) Logs to a file in logstash format.
App logger:
Your usual logger that just writes down what the code is doing.
Mostly quiet on production except when errors happen.
Includes stack traces.
import re
class _ValueNotSet(object):
pass
ValueNotSet = _ValueNotSet()
@jbasko
jbasko / list_functions_in_module_ordered.py
Created April 14, 2017 16:29
List names of functions declared in a Python module in the order of declaration
"""
Gets list of functions in a module, preserve declaration order.
This cannot be done with inspect.getmembers() or dir() because they lose the order.
Have to construct AST with ast.
"""
import ast
import importlib
import os.path
@jbasko
jbasko / universal_decorator.py
Created January 12, 2018 11:17
universal decorator
class universal_decorator:
wrappers = []
def __init__(self, method):
self.original_method = method
self.method = self.original_method
self.apply_decorators()
def apply_decorators(self):
for wrapper in self.wrappers:
@jbasko
jbasko / context_entering_decorator.py
Last active January 27, 2018 10:05
Generator-aware decorator factory
def context_entering_decorator(context_manager):
"""
Creates a context-entering decorator that does not fail to do its job
if the underlying function is actually a generator function.
"""
def decorator(func):
if inspect.isgeneratorfunction(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
@jbasko
jbasko / wow.py
Last active April 10, 2018 04:57
This doesn't do what you think it does
def filter1(r):
return r % 3 == 0
def filter2(r):
return r % 5 == 0
def apply_all(records, *filters):
@jbasko
jbasko / pyenv_and_tox.md
Created September 7, 2018 22:56
pyenv and tox
cd ~
pyenv global 3.6.6 3.7.0
pip install tox
cd ~/workspace/xxx/project-name
pyenv local 3.6.6 3.7.0
tox
@jbasko
jbasko / graph_and_topological_sort.py
Created March 30, 2019 12:53
Graph and Topological Sort using Kahn' s algorithm in Python 3
import collections
from typing import Iterable
class Vertex:
def __init__(self, value, graph: "Graph"):
self._graph = graph
self.value = value
def __hash__(self):
@jbasko
jbasko / kivy_recycle_view_scroll_y_helpers.py
Last active October 12, 2019 20:13
Kivy's RecycleView "metrics" - helpers to scroll based on item index
import math
from kivy.base import runTouchApp
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView

Keybase proof

I hereby claim:

  • I am jbasko on github.
  • I am jbasko (https://keybase.io/jbasko) on keybase.
  • I have a public key ASCG6JaPj_Yn7o53c-omTdE900qmWFTal4bHwtRK2OJjlgo

To claim this, I am signing this object: