Skip to content

Instantly share code, notes, and snippets.

View avalanchy's full-sized avatar

Mariusz Osiecki avalanchy

  • Poland
View GitHub Profile
@avalanchy
avalanchy / serializers.py
Last active July 31, 2018 08:10
django rest framework: object out - primary key in field
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):
@avalanchy
avalanchy / makemigrations.py
Last active July 20, 2018 12:45
Overridden Django 2 command that always creates a migration with the name "m", by which we can catch conflicts at the level of the Pull Request
from django.core.management.commands import makemigrations
class Command(makemigrations.Command):
def handle(self, *args, **options):
self.stdout.write(self.style.HTTP_NOT_FOUND(
'This is the overridden command that always creates a migration '
'with the name "m", by which we can catch conflicts at the level '
'of the Pull Request.'
curl -q 'http://watchout4snakes.com/wo4snakes/Random/RandomPhrase' -H 'Origin: http://watchout4snakes.com' -X POST --data 'Pos1=a&Level1=35&Pos2=n&Level2=35' | trans en:pl
"""
Sloppy but colorful thesaurus.com for CLI
Requires Python 3.6
Usage:
pip install ansicolors pyquery
python thesaurus.py private
"""
@avalanchy
avalanchy / fetchUntil.js
Created June 23, 2017 05:52
node fetchUntil.js
var request = require('request');
console.log('With callbacks');
var browser = 'Selenium Wrapper';
function getIp (callback) {
request('https://api.ipify.org?format=json', function (error, response, body) {
if (error) {
throw `Cannot connect: ${error}`;
import sys
import pyquery
import requests
SYNONIMY_URL = 'https://www.synonimy.pl/synonim/{}/'
WORDS_PER_LINE = 5
if len(sys.argv) < 2:
class InstanceOf:
"""Wrapper to compare only type, not whole instance.
Example usage:
def test_ok(m_ted):
...
m_ted.assert_called_once_with(InstanceOf(NotificationRequest))
"""
// ==UserScript==
// @name 1337x.to search results have filmweb links
// @include http://1337x.to/*
// @version 0.1
// @description Add new column in search results that target to Filmweb's movie page.
// @author avalanchy
// ==/UserScript==
(function() {
@avalanchy
avalanchy / upper.py
Last active September 29, 2016 09:04
python. Convert dict keys to uppercase. If you want to use it - don't. This mean that you should change your code.
def _uppercase_for_dict_keys(lower_dict):
upper_dict = {}
for k, v in lower_dict.items():
if isinstance(v, dict):
v = _uppercase_for_dict_keys(v)
upper_dict[k.upper()] = v
return upper_dict
def test():
@avalanchy
avalanchy / upper.py
Created September 28, 2016 12:21
covert all dict keys to uppercase in python.
def _uppercase_for_dict_keys(lower_dict):
upper_dict = {}
for k, v in lower_dict.items():
if isinstance(v, dict):
v = _uppercase_for_dict_keys(v)
upper_dict[k.upper()] = v
return upper_dict