Skip to content

Instantly share code, notes, and snippets.

Avatar

Illia Sukonnik Sukonnik-Illia

View GitHub Profile
@Sukonnik-Illia
Sukonnik-Illia / alembic_click.py
Created Jan 13, 2022
Run alembic cli from Python Click
View alembic_click.py
import sys
import click
from alembic.config import main
@click.group()
def cli():
"""Hello cli app"""
@cli.command(
@Sukonnik-Illia
Sukonnik-Illia / my_json_dums.py
Last active Sep 9, 2022
Python 3 Json with 2 decimals after comma
View my_json_dums.py
# We need to ensure that c encoder will not be launched
@patch('json.encoder.c_make_encoder', None)
def json_dumps_with_two_digit_float(some_object):
# saving original method
of = json.encoder._make_iterencode
def inner(*args, **kwargs):
args = list(args)
# fifth argument is float formater which will we replace
args[4] = lambda o: '{:.2f}'.format(o)
return of(*args, **kwargs)
@Sukonnik-Illia
Sukonnik-Illia / builder.py
Created Sep 21, 2020
builder with or without db saving and relational objects
View builder.py
# with relational objects and with sawing to database
(
ChargeBuilder(database)
.with_customer(CustomerBuilder(database).build())
.with_delivery_date(DeliveryDateBuilder(database).build())
.build()
)
# simple builder
View keybase.md

Keybase proof

I hereby claim:

  • I am sukonnik-illia on github.
  • I am isukonnik (https://keybase.io/isukonnik) on keybase.
  • I have a public key ASAfSYx5nDpmqS2A1Wq3o-5Ya9l_V66oohkaU0Snjj2c-Ao

To claim this, I am signing this object:

View emacs-80-width.el
(defun set-window-width (n)
"Set the selected window's width."
(adjust-window-trailing-edge (selected-window) (- n (window-width)) t))
(defun set-80-columns ()
"Set the selected window to 80 columns."
(interactive)
(set-window-width 80))
(global-set-key (kbd "C-x ~") 'set-80-columns)
@Sukonnik-Illia
Sukonnik-Illia / DictNoNone.py
Created Jan 25, 2016
Python ictionary without empty fields
View DictNoNone.py
class DictNoNone(dict):
"""Delete empty fields from dictionary """
def __init__(self, initial_dict={}):
for key in initial_dict:
self.__setitem__(key, initial_dict[key])
def __setitem__(self, key, value):
if key in self or value:
dict.__setitem__(self, key, value)
@Sukonnik-Illia
Sukonnik-Illia / fast_iter.py
Created Oct 5, 2015
low memory lxml.etree.iterparse
View fast_iter.py
from lxml import etree
def fast_iter(context, func, *args, **kwargs):
""" call func on each xml element chosen when create context
context = etree.iterparse('path to file', tag='your tag', events)
"""
for event, elem in context:
func(elem, *args, *kwargs)
elem.clear()