Skip to content

Instantly share code, notes, and snippets.

@AlwxSin
AlwxSin / TrackAttrsMixin.py
Created April 12, 2018 15:16
Mixin for tracking class attributes
class TrackAttrsMixin:
"""
Keep eye on TRACK_ATTRS, so we can know if they changed before doing any actions
TRACK_ATTRS should not be empty
"""
TRACK_ATTRS: Iterable[str] = ()
def __init__(self, *args, **kwargs):
if not self.TRACK_ATTRS:
raise ValueError("TRACK_ATTRS should contain at least one value")
@AlwxSin
AlwxSin / git_commands.sh
Last active July 10, 2018 13:04
useful git commands
# show only file names
git diff --name-only SHA HEAD~1 > diff.txt
# delete old branches
git branch -a > branches.txt # dump all branches to file
&& sed -i '' 's/\/remote\/origin\///g' branches.txt # replace unneeded '/remote/origin/' prefix
&& nano branches.txt # manually delete branches that need to be keeped
&& cat branches.txt | xargs -I {} git push origin :{} # delete unneeded branches
# show added/deleted lines count
@AlwxSin
AlwxSin / AnimatedInteger.vue
Created November 13, 2017 08:54
Vue Animated integer/float component
<template>
<span>{{ tweeningValue }}</span>
</template>
<script>
import TWEEN from 'tween.js';
export default {
name: 'animated-integer',
props: {
@AlwxSin
AlwxSin / views.py
Created October 27, 2017 11:01
Get current user by endpoint `/api/users/me` in DjangoRestFramework
from rest_framework import viewsets
from rest_framework.request import Request
from rest_framework.response import Response
class UserViewSet(viewsets.ModelViewSet):
def retrieve(self, request: Request, *args, **kwargs):
"""
If provided 'pk' is "me" then return the current user.
"""
if kwargs.get('pk') == 'me':
@AlwxSin
AlwxSin / Dockerfile
Last active August 7, 2022 15:47
Sentry docker-compose with telegram integration
FROM sentry:onbuild
@AlwxSin
AlwxSin / UserRequest.py
Created March 21, 2017 07:08
django type hinting classes
class AuthMiddlewareAwareHttpRequest(django.http.HttpRequest):
if 'django.contrib.auth' in django.conf.settings.MIDDLEWARE:
user: django.contrib.auth.models.AbstractUser
def myview(request: AuthMiddlewareAwareHttpRequest):
pass
@AlwxSin
AlwxSin / brief_list.py
Created December 5, 2016 08:25
Django Rest Framework Mixins
class BriefListMixin:
"""
Sometimes front-end needs brief list of models
Need to override brief_fields mapping like
{'frontend_name': 'model_field'}
"""
brief_fields = {}
@decorators.list_route(methods=['GET', ], url_path='brief')
@AlwxSin
AlwxSin / base.py
Created November 24, 2016 08:34 — forked from cwisecarver/base.py
Working server_side_cursors in django 1.9
import uuid
import psycopg2
from django.conf import settings
from django.db.backends import utils
from django.db.backends.postgresql.base import \
DatabaseWrapper as PostgresqlDatabaseWrapper
from django.db.backends.postgresql.base import *
@AlwxSin
AlwxSin / for_each_jquery_get.js
Created October 6, 2016 13:15
Werkzeug and ajax
// Not so usefull
// After failed request do another request in new window
var url = 'endpoint-url-of-your-ajax-request-without-query-params'
$.get(
url + '?param1=false'
).fail(function (jqXHR) {
// open debugger in new window
var endpointUrl = url,
debuggerWindow = window.open('', 'werkzeug debugger');
debuggerWindow.document.open();
@AlwxSin
AlwxSin / context_managers.py
Last active October 5, 2016 17:08
Python profiling
"""Collect profiling statistic into graphite"""
import socket
import time
CARBON_SERVER = '127.0.0.1'
CARBON_PORT = 2003
class Stats(object):