Skip to content

Instantly share code, notes, and snippets.

View Krakenus's full-sized avatar

Milan Vlasák Krakenus

View GitHub Profile
@Krakenus
Krakenus / meta.py
Created April 19, 2020 11:18
Python metaclasses
class AutoIncrementIDMeta(type):
__counter = {}
def __call__(cls, *args, **kwargs):
if cls.__name__ not in cls.__counter:
cls.__counter[cls.__name__] = 0
cls.__counter[cls.__name__] += 1
instance = super().__call__(*args, **kwargs)
instance.id = cls.__counter[cls.__name__]
return instance
@Krakenus
Krakenus / vscode_export.sh
Last active July 20, 2020 07:26
vscode - export extensions to install script
#!/bin/bash
code --list-extensions | xargs -L 1 echo code --install-extension > install_extensions.sh
@Krakenus
Krakenus / seconds_to_midnight.py
Created August 14, 2020 13:55
Get seconds remaining to midnight from current time
def seconds_to_midnight() -> int:
now = datetime.now()
midnight = now + timedelta(days=1)
midnight = midnight.replace(hour=0, minute=0, second=0, microsecond=0)
return (midnight - now).seconds
@Krakenus
Krakenus / qs_iterator.py
Created March 29, 2021 08:58
Django QuerySetBatchIterator
class QuerysetBatchIterator:
def __init__(self, qs, batch_size=1000):
self.qs = qs
self.batch_size = batch_size
self._total = None
def __iter__(self):
for start in range(0, self.total, self.batch_size):
end = min(start + self.batch_size, self.total)