Skip to content

Instantly share code, notes, and snippets.

@Skyross
Skyross / orderer.py
Last active November 7, 2019 09:01
Django: reorder by model IDs
from typing import Type, List
from django.db import models, connection
class OrdererByID:
"""Helper class to update rows in table according to order of list of IDs provided."""
def __init__(self, *, model: Type[models.Model], field_name: str) -> None:
self.reorder_query = f"""
@Skyross
Skyross / postgresql_types.sql
Last active February 19, 2018 09:43
PostgreSQL enums testing
CREATE TYPE MOOD AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person_with_type (
name TEXT,
current_mood MOOD DEFAULT 'ok'
);
INSERT INTO person_with_type (name, current_mood)
SELECT
name,
@Skyross
Skyross / django_chunk_fetch.py
Last active September 28, 2017 21:53
Django fetching by chunks
from django.db.models import Count, Min, Max
from django.db.models.functions import Coalesce
def chunk_fetch(queryset, chunk_size=100):
"""
:type chunk_size: int
:type queryset: django.db.models.QuerySet
"""
aggregation = queryset.aggregate(
min_id=Coalesce(Min('pk'), 0),
@Skyross
Skyross / task_with_lock.py
Last active February 5, 2024 05:51
Celery Task with lock
from celery import Task
from django.conf import settings
from django.core.cache import caches
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
# noinspection PyAbstractClass
class TaskWithLock(Task):
"""
@Skyross
Skyross / cutter.au3
Created May 22, 2016 18:34
Dota 2 AutoIt tree cutter
#include <AutoItConstants.au3>
HotKeySet("{PAUSE}", "LoopFlagToggle")
$loopflag = False
$random = False
$only_clicks = False
$dx = 50
$dy = 50
@Skyross
Skyross / attribute.controller.js
Last active August 29, 2015 14:22
Sample coding of ImporterApp
'use strict';
var _ = require('lodash');
var Attribute = require('./attribute.model.js');
// Get list of mappings
exports.index = function (req, res) {
Attribute.find(req.query, function (err, mappings) {
if (err) {
return handleError(res, err);