Skip to content

Instantly share code, notes, and snippets.

@emakarov
emakarov / admin.py
Last active December 10, 2016 15:53
Automatic django admin classes generation snippet
from django.contrib import admin
from django.apps import apps
from django.contrib.admin.sites import AlreadyRegistered
def register_all_models(models):
for m in models:
m_admin_name = m.__module__.replace(".","") + m.__name__ + 'Admin'
ModelAdminClass = type(m_admin_name, (admin.ModelAdmin,), {'set_x': lambda x,y: x.y})
ModelAdminClass.model = m
@emakarov
emakarov / gist:5a76573c22e52b74e10390779106f4ce
Last active April 27, 2018 07:54
Delete untagged docker images (with force)
# this removes <none> docker images
docker rmi -f $(docker images -f "dangling=true" -q)
@emakarov
emakarov / sample.sql
Created April 3, 2019 15:56
clickhouse user cpu calculation
create temporary table cputime (dt Date, ts Int8, cpu Int8, user_id Int8);
insert into cputime values
('2019-01-01', 1, 0, 1), ('2019-01-01', 2, 1, 1), ('2019-01-01', 3, 1, 1),
('2019-01-01', 4, 0, 1), ('2019-01-01', 5, 2, 1), ('2019-01-01', 6, 3, 1),
('2019-01-01', 1, 0, 2), ('2019-01-01', 2, 4, 2), ('2019-01-01', 3, 5, 2),
('2019-01-01', 4, 0, 2), ('2019-01-01', 5, 8, 2), ('2019-01-01', 6, 15, 2),
('2019-01-01', 7, 5, 2), ('2019-01-01', 8, 8, 2), ('2019-01-01', 9, 8, 2);
select
@emakarov
emakarov / get_last_migration.py
Created December 16, 2016 14:47
How to get name of last migration in django
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.loader import MigrationLoader
# code from command showmigrations is used and adopted as a function
def get_migrations(connection, app_names=None):
"""
@emakarov
emakarov / hello_world.rb
Created August 18, 2019 08:15
Hello World Examples
Hello world;
@emakarov
emakarov / update_helm.sh
Last active June 17, 2020 06:52
How to install or update helm
# Installing/updating helm on Ubuntu
# Usage: ./update_helm.sh v2.16.9
VERSION=$1
echo "Installing Helm ${VERSION}"
rm helm-*-linux-amd64.tar.gz
rm -r linux-amd64
echo "Download will start"
wget https://get.helm.sh/helm-${VERSION}-linux-amd64.tar.gz
tar -zxvf helm-${VERSION}-linux-amd64.tar.gz
mv linux-amd64/helm /usr/local/bin/helm
@emakarov
emakarov / delete_kubernetes_pods_by_mask
Created December 14, 2018 15:26
How to delete all kubernetes pods by mask from bash command line
kubectl get pods | grep enter_your_mask_here | while read line ; do echo "$line" | awk '{print $1}' | sort | while read line2; do kubectl delete pod "$line2" ; done; done
@emakarov
emakarov / test_coding_style.py
Created July 11, 2018 07:51
How to check python coding style automatically via flake8 and isort in django project with unit test
"""
Coding styles test.
You need to install flake8 and isort via pip.
pip install flake8
pip install isort
Add this test to any of your django apps tests.
"""
import subprocess
@emakarov
emakarov / merger.py
Last active July 26, 2023 13:29
Python script that can resolve all git conflicts in some file marked with conflicts based on chosen strategy
accept = 'ours' # ours for 'HEAD' #theirs for other's - choose strategy here for all conflicts
filename = 'enter your filename here'
fresult = '{}.{}'.format(filename, 'merged') # result will be near your file with name `original_filename.ext.merged`
with open(filename) as f:
content = f.readlines()
res = open(fresult, 'w')
to_write = True
merging = False
@emakarov
emakarov / celerycleanup.py
Last active March 5, 2024 14:17
celery tasks cleanup
from proj.celery import app
from celery.app import control
# remove pending tasks
app.control.purge()
# remove active tasks
i = app.control.inspect()
jobs = i.active()
for hostname in jobs: