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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Generic, TypeVar, get_args | |
import factory | |
from factory.base import FactoryMetaClass | |
def _override_auto_now_add_fields(obj, kwargs): | |
""" | |
That's a built-in "feature" of Django: when a field has auto_now_add=True, Django will override any provided value | |
with django.utils.timezone.now() ; this happens "after" factory_boy's code runs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def as_list(func: Callable[..., Iterator[T]]) -> Callable[..., list[T]]: | |
"""Decorator that casts the output of the function to a list. | |
Example: | |
>>> @as_list | |
... def foo(): | |
... yield 123 | |
... | |
>>> foo() | |
[123] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}[]") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
NewerOlder