Skip to content

Instantly share code, notes, and snippets.

View avalanchy's full-sized avatar

Mariusz Osiecki avalanchy

View GitHub Profile
@avalanchy
avalanchy / ipython.py
Created March 15, 2024 05:56
python date datetime isinstance
$ ipython
Python 3.12.2 (main, Mar 5 2024, 10:30:14) [GCC 13.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.20.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from datetime import datetime, date
In [2]: dt = datetime.now()
In [3]: d = dt.date()
@avalanchy
avalanchy / druid_nulls.md
Created February 7, 2022 10:45
Apache Druid differences in aggregation results when uising default value for null is disabled (druid.generic.useDefaultValueForNull = False)
aggregator type op before op after res before res after same res
longSum, doubleSum regular 0 + 0 0 + null 0 0 TRUE
longSum, doubleSum regular 0 + 0 null + null 0 null FALSE
longLast, doubleLast and longLastTimestamp regular Pick 0 Pick null 0 null FALSE
longMax regular [0, -1] [0, -1] 0 0 TRUE
longMax regular [0, -1] [null, -1] 0 -1 FALSE
doubleMean regular [0, 1] [0, 1] 0,5 0,5 TRUE
doubleMean regular [0, 1] [null, 1] 0,5 0,5 TRUE
@avalanchy
avalanchy / product_of_pairs.py
Created January 31, 2022 08:09
python function that returns cartesian prodict of two lists containing only pairs
def product_of_pairs(iterable1, iterable2, item_sorting_key=None):
"""Function that returns cartesian product of two lists. Output is filtered to contain only pairs, where pair
means item cannot be with itself and AB == BA.
Example:
>>> product_of_pairs('ab', 'ab')
{('a', 'b')}
"""
return {
tuple(sorted((item1, item2), key=item_sorting_key))
for item1 in iterable1
@avalanchy
avalanchy / cast_to.py
Last active March 29, 2024 09:43
python cast function output to specifc type. most commonly decorate generator to return a list.
import functools
def cast_to(cast_func):
"""Cast function's output to a specified type.
Example:
>>> @cast_to(list)
... def foo():
... yield 123
@avalanchy
avalanchy / schema.py
Last active March 22, 2021 20:13
python dict list schema
import json
def schema(obj, parent="obj"):
if isinstance(obj, list):
if obj:
last = len(obj) - 1
schema(obj[last], f'{parent}[{last}]')
else:
print(f"{parent}[]")
@avalanchy
avalanchy / chunk_dict.py
Last active November 9, 2020 13:41
python 2/3 chunk_dict utility
from types import GeneratorType
import six
def chunk_dict(dict_, size):
chunk = {}
for i, (key, val) in enumerate(six.iteritems(dict_), start=1):
chunk[key] = val
if i % size == 0:
@avalanchy
avalanchy / siepomaga.js
Last active July 10, 2020 08:14
Ile wplacono poprzez X na siepomaga.pl
let amount = parseFloat(document.querySelector(".amount").innerText.replace(" ", "").replace(",", "."))
let payments = document.querySelectorAll(".sp-payment-amount");
let paymentSum = 0;
for (let payment of payments) {
const paymentFloat = parseFloat(payment.innerText);
if (isNaN(paymentFloat))
continue;
paymentSum += paymentFloat;
}
@avalanchy
avalanchy / factories_lookup.py
Last active February 8, 2019 06:29
List all factory boy factories in a way expected by shell_plus command from django-extensions. Python 3.7.
import pathlib
import re
def factories_lookup(base_dir):
"""Yields all model factories in a way expected by shell_plus command.
Returned generator is evaluated only when running shell_plus. Evaluation
itself on my machine takes ± 3.63 ms.
@avalanchy
avalanchy / serializers.py
Last active July 31, 2018 08:10
django rest framework: object out - primary key in field
from rest_framework import relations
from rest_framework.serializers import ModelSerializer
class ObjectOutPkInField(relations.RelatedField):
"""Field serializer wrapper which as normal returns related object
but as the input accepts object's PK (primary key).
Example usage:
>>> class TypicalModelSerializer(ModelSerializer):
@avalanchy
avalanchy / makemigrations.py
Last active July 20, 2018 12:45
Overridden Django 2 command that always creates a migration with the name "m", by which we can catch conflicts at the level of the Pull Request
from django.core.management.commands import makemigrations
class Command(makemigrations.Command):
def handle(self, *args, **options):
self.stdout.write(self.style.HTTP_NOT_FOUND(
'This is the overridden command that always creates a migration '
'with the name "m", by which we can catch conflicts at the level '
'of the Pull Request.'