Skip to content

Instantly share code, notes, and snippets.

View Brick85's full-sized avatar

Vital Belikov Brick85

View GitHub Profile
import socket
def get_lock(process_name):
get_lock._lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
get_lock._lock_socket.bind('\0' + process_name)
return True
except socket.error:
return False
# http://stackoverflow.com/questions/21986602/fixed-length-int-obfuscator-does-anyone-know-how-to-do-this
class IntObfuscator(object):
m = 1000000000
x = 387420489
inverseX = 513180409
def encode_int(self, value):
return value * self.x % self.m
def decode_int(self, encoded):
@Brick85
Brick85 / email_as_username.py
Created December 3, 2015 14:24
Django email as username
### models.py
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.core.mail import send_mail
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
class UserManager(BaseUserManager):
use_in_migrations = True
@Brick85
Brick85 / admin_methods.py
Last active December 1, 2015 12:42
django admin methods
def get_actions(self, request):
actions = super(type(self), self).get_actions(request)
if 'delete_selected' in actions:
del actions['delete_selected']
return actions
def has_delete_permission(self, request, obj=None):
return False
@Brick85
Brick85 / somagic_rec_and_play.sh
Created July 21, 2014 11:50
somagic record and play
#!/bin/sh
PIPE=/tmp/somagic-pipe
OUTFILEDIR=~/dirs/Videos/
LOGDIR=~/.somagic-log/
NOW=`date +"%m_%d_%Y_%H_%M_%S"`
OUTFILE=${OUTFILEDIR}fpv_video_${NOW}.mp4
mkdir $LOGDIR
@Brick85
Brick85 / gist:7883799
Last active December 30, 2015 20:49
kohctpyktop
http://www.zachtronics.com/kohctpyktop/kohctpyktop.htm
KT229:
eNrtmVsOgyAQRYHhxzV0C/3vWrr/jTQ8tLYylo7hpRdjTLyOcxjjBdTe7W166umh
rZJsCDxT4Bys/ws01u1qOZbPqJZMgoyiQOM3tRxFfdS5/dxWVUurqqXFqRH4WdUa
fURVUVVU9RRVfW/ZpnyJodxP5wzfvEpEyf246ppVbOJ2MsXGqLEbslgfyMaGW5eI
BTOY+2RO3D3kne3i+4IKtpS2wyy/hLpS3XOjBrFgBjOYG6qwyitYNJjBPCIzwSoH
fohuRZFeVZQdo3kRzGA+HXN0SoJVDjzeHVLjFxqJmsNMJdTwRUmk7s8NwAxmGscZ
YJV/qryflV0oZeRlbbZj5hHrDOZKzDTeArzMH6VuzXD3v57hJ3+//wk2UsEM5g6Z
DTOzDFTx9MaNmzlSplWm9+Nqn/NGYtYNhcf3jK83RWLBDOYWzP6CxJu2ypt4FVs5
0gsHIMls
@Brick85
Brick85 / globalrequests.py
Created October 8, 2013 10:04
Global requests for Django
import inspect
def get_request():
"""Walk up the stack, return the nearest first argument named "request"."""
frame = None
try:
for f in inspect.stack()[1:]:
frame = f[0]
code = frame.f_code
if code.co_varnames[:1] == ("request",):
# forms.py
class UserProfileForm(forms.ModelForm):
class Meta:
model = User
fields = ("email", "last_name", "first_name", "middle_name", 'avatar', 'age', 'hobbies', 'about', 'project_interes')
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
super(UserProfileForm, self).__init__(*args, **kwargs)
for fname in ("email", "last_name", "first_name", 'age'):
@Brick85
Brick85 / encrypt.py
Created July 16, 2013 17:09
python crypt and encrypt
from Crypto.Cipher import AES
from Crypto import Random
import base64
AESKEY = "jFa8*!9uiAS0!385&(lla$1dqae8#!da"
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s: s[0:-ord(s[-1])]
@Brick85
Brick85 / ubuntu
Last active December 16, 2015 19:19
Ubuntu hints
Resize terminal:
resize -s 25 80
Test HDD speed:
dd if=/dev/zero of=zeros bs=1M count=500 oflag=direct; unlink zeros
dd if=/dev/zero of=zeros bs=64k count=16k conv=fdatasync; unlink zeros