Skip to content

Instantly share code, notes, and snippets.

View asfaltboy's full-sized avatar

Pavel Savchenko asfaltboy

View GitHub Profile
@asfaltboy
asfaltboy / css_path.js
Created March 28, 2015 14:19
Get Element CSS Selector
/*
* Copyright (C) 2015 Pavel Savshenko
* Copyright (C) 2011 Google Inc. All rights reserved.
* Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
* Copyright (C) 2009 Joseph Pecoraro
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@asfaltboy
asfaltboy / argparse_email_type.py
Created October 9, 2016 17:48
EmailType: a custom argparse type to validate email addresses
"""
A custom type for argparse, to facilitate validation of email addresses.
Inspired by this SO question: http://stackoverflow.com/questions/14665234/argparse-choices-structure-of-allowed-values
and this gist: https://gist.github.com/gurunars/449edbccd0de1449b71524c89d61e1c5
"""
import re
import argparse
@asfaltboy
asfaltboy / req_to_curl.py
Last active November 23, 2022 14:58
Django HTTP Request to curl command (for replay)
"""
Convert a Django HTTPRequest object (or dictionary of such a request) into a
cURL command.
"""
import json
import sys
required_fields = ['META', 'META.REQUEST_METHOD', 'META.SERVER_NAME', 'META.PATH_INFO']
@asfaltboy
asfaltboy / conftest.py
Last active March 30, 2022 23:52
A pytest fixture to test Django data migrations
# based on https://gist.github.com/blueyed/4fb0a807104551f103e6
# and on https://gist.github.com/TauPan/aec52e398d7288cb5a62895916182a9f (gistspection!)
from django.core.management import call_command
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
import pytest
@asfaltboy
asfaltboy / clip_magic.py
Last active September 20, 2021 22:23 — forked from nova77/clip_magic.py
"""
Add copy to clipboard from IPython!
To install, just copy it to your profile/startup directory, typically:
~/.ipython/profile_default/startup/
Example usage:
%clip hello world
# will store "hello world"
@asfaltboy
asfaltboy / callable_bug.py
Last active March 22, 2021 21:49
pyright: Callable not a callable bug
from typing import Callable, NamedTuple, Tuple
# class RuleDefinition(NamedTuple):
# pattern: str
# path: str
# check: Callable
RuleDefinition = Tuple[str, str, Callable]
@asfaltboy
asfaltboy / my_app.py
Last active November 28, 2019 11:09
A reproduction of worker starvation on result backend failure (see https://github.com/celery/celery/issues/5642)
import time
from celery import Celery
app = Celery(
'my_app',
broker='pyamqp://guest@localhost:5673//',
backend='redis://localhost',
)
app.conf.worker_prefetch_multiplier = 1
@asfaltboy
asfaltboy / log_test12.py
Last active August 24, 2019 05:50 — forked from anonymous/log_test11.py
Test harness for the logging module. Tests BufferingSMTPHandler, an alternative implementation SMTPHandler. This fork includes credential and secure support (as implemented in newer versions of logging library). Additionaly a flood check is implemented to make sure not more than capacity per minute is sent, otherwise the capacity (buffer) is rai…
#!/usr/bin/env python
#
# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of Vinay Sajip
# not be used in advertising or publicity pertaining to distribution
@asfaltboy
asfaltboy / responses-mixin.py
Last active February 22, 2019 14:24
A responses TestCase Mixin
"""
A unittest.TestCase mixin that allows using the
responses (https://pypi.python.org/pypi/responses/) package in tests.
Usage
-----
Install responses with `pip install responses`.
Add `ResponsesMixin` to your `TestCase` parent classes instead of using
@asfaltboy
asfaltboy / mousemap_wrapper.py
Last active January 28, 2019 21:21
A simply SublimeText plugin to run a command if matches a selector
"""
A simply SublimeText plugin to run a command if matches a selector.
Usage example - my `Default (OSX).sublime-mousemap`:
[
// for Python we use Anaconda's goto command, for go we use go_guru,
// for others we use built-in goto command
{ "button": "button1", "modifiers": ["ctrl"], "command": "mousemap_wrap",
"press_command": "drag_select", "args": { "commands": [
{