Skip to content

Instantly share code, notes, and snippets.

View pahaz's full-sized avatar
🛠️
Yo ho ho

Pavel White pahaz

🛠️
Yo ho ho
View GitHub Profile
@pahaz
pahaz / CheckDomainMiddleware.py
Created November 11, 2015 11:19
CheckDomainMiddleware
class CheckDomainMiddleware(object):
"""
If MULTISITE_WRONG_DOMAIN_REDIRECT_URL defined all wrong domain requests
will be redirected to this url. Otherwise Http 404 will be returned.
How-to use:
# 1. add to MIDDLEWARE_CLASSES
# 2. File: settings.py
MULTISITE_WRONG_DOMAIN_REDIRECT_URL = "http://example.com/"
"""
@pahaz
pahaz / index.html
Last active January 9, 2016 12:25
socket-io chat example
<!doctype html>
<html lang="en">
<head>
<title>Chat Example</title>
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
var socket = io.connect();
@pahaz
pahaz / metaclassexample.py
Created March 11, 2016 13:15
EXAMPLE! How to understand the python metaclasses. Less1.
from functools import wraps
def debug(func):
print('debug(', func.__qualname__, ')')
name = func.__qualname__
@wraps(func)
def wrapper(*args, **kwargs):
print('CALL:', name, args, kwargs)
@pahaz
pahaz / less2.py
Created March 18, 2016 09:29
Matrix
#!/usr/bin/env python3
class Matrix:
def __init__(self, m):
self.m = m
def __str__(self):
return "Matrix(%r)" % self.m
def __repr__(self):
def logg(func):
def logged_func(*args, **kwargs):
print(func.__name__ + '() ...',
args, kwargs)
return func(*args, **kwargs)
logged_func.__doc__ = func.__doc__
logged_func.__name__ = func.__name__
return logged_func
@pahaz
pahaz / next-version.sh
Last active July 17, 2016 06:59
Simple python v3 inline script for generating next version number!
echo 1.0.0 | python -c "v = input().strip().split('.'); v[-1] = str(int(v[-1]) + 1); print('.'.join(v))"
#!/usr/bin/env bash
set -o errexit # always exit on error
set -o pipefail # don't ignore exit codes when piping output
set -o nounset # fail on unset variables
set -o xtrace # print command traces before executing command
echo "PWD=$PWD"
# #
@pahaz
pahaz / metaclasses.py
Created August 25, 2016 12:17
Short story about how metaclasses work
print('class Meta(type)')
class Meta(type):
@classmethod
def __prepare__(mcs, name, bases, **kwargs):
print(' Meta.__prepare__(mcs=%s, name=%r, bases=%s, **%s)' % (
mcs, name, bases, kwargs
#!/bin/bash
for v in `docker ps --format '{{.Names}}\t{{.Status}}' | grep postgres | awk '{ print $1 }'`
do
echo $v
NNN=$v
docker exec -it $NNN vacuumdb --all -U postgres --full --analyze
done
@pahaz
pahaz / ssh_tunnel_test.py
Created October 31, 2020 12:42
ssh_tunnel_test.py
import logging
import threading
import time
import sshtunnel
from sshtunnel import HandlerSSHTunnelForwarderError, SSHTunnelForwarder
sshtunnel.DEFAULT_LOGLEVEL = 1
logging.basicConfig(filename='example.log', filemode='w', level=sshtunnel.DEFAULT_LOGLEVEL)
logging.warning('Start example! v %s', sshtunnel.__version__)