Skip to content

Instantly share code, notes, and snippets.

View niwinz's full-sized avatar

Andrey Antukh niwinz

View GitHub Profile
@niwinz
niwinz / output.txt
Last active December 10, 2015 18:29
Memory leak when you create alias methods once the class has been instantiated.
[3/5.0.2]niwi@localhost:~/tmp-memory-leak> python3 simple-test.py
Memory leak:
KK.__init__ 139987115392208
Foo.__init__ 139987115392016
No memory leak:
KK.__init__ 139987115392400
Bar__init__ 139987115392272
Bar.__del__ 139987115392272
KK.__del__ 139987115392400
@niwinz
niwinz / test.py
Created January 19, 2013 23:44
Support for patch method to django client.
from django.test import client
# Call this function on head of your test file.
def patch_request_factory():
def _method(self, path, data='', content_type='application/octet-stream', follow=False, **extra):
response = self.generic("PATCH", path, data, content_type, **extra)
if follow:
response = self._handle_redirects(response, **extra)
return response
@niwinz
niwinz / uuid.js
Last active December 12, 2015 05:48 — forked from jcxplorer/uuid.js
function uuid() {
var uuid = "", i, random;
for (i = 0; i < 32; i++) {
random = Math.random() * 16 | 0;
if (i == 8 || i == 12 || i == 16 || i == 20) {
uuid += "-"
}
uuid += (i == 12 ? 4 : (i == 16 ? (random & 3 | 8) : random)).toString(16);
}
@niwinz
niwinz / example-http.cpp
Last active December 13, 2015 19:28
libinet with c++11, some code style fixings and without global variables (first and inacurate refactor with a lot of bugs)
#include <iostream>
#include <string>
#include <vector>
#include "../src/http.h"
using namespace std;
void
progress(size_t length, size_t received_length) {
cout << (received_length * 100) / length << "%" << endl;
@niwinz
niwinz / pfanalyze.py
Last active November 20, 2017 08:23
PF realtime network monitor (by ip) tool
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from subprocess import Popen, PIPE
import argparse
import shlex
import datetime
import re
import ipaddress as ip
import functools
@niwinz
niwinz / rt53.py
Last active December 14, 2015 15:49
Simple command for automatic update of current ip of ec2 instance in route53.
# -*- coding: utf-8 -*-
from __future__ import print_function
import argparse
import socket
import fcntl
import struct
import os
import sys
@niwinz
niwinz / middleware.py
Last active December 15, 2015 15:18
Cors middleware.
CORS_ALLOWED_ORIGINS = getattr(settings, 'CORS_ALLOWED_ORIGINS', '*')
CORS_ALLOWED_METHODS = getattr(settings, 'CORS_ALLOWED_METHODS',
['POST', 'GET', 'OPTIONS', 'PUT', 'DELETE', 'PATCH'])
CORS_ALLOWED_HEADERS = getattr(settings, 'CORS_ALLOWED_HEADERS',
['Content-Type', 'X-Requested-With',
'X-Session-Token', 'Accept-Encoding'])
CORS_ALLOWED_CREDENTIALS = getattr(settings, 'CORS_ALLOWED_CREDENTIALS', True)
class CorsMiddleware(object):
"""
A file lock implementation that tries to avoid platform specific
issues. It is inspired by a whole bunch of different implementations
listed below.
- https://bitbucket.org/jaraco/yg.lockfile/src/6c448dcbf6e5/yg/lockfile/__init__.py
- http://svn.zope.org/zc.lockfile/trunk/src/zc/lockfile/__init__.py?rev=121133&view=markup
- http://stackoverflow.com/questions/489861/locking-a-file-in-python
- http://www.evanfosmark.com/2009/01/cross-platform-file-locking-support-in-python/
- http://packages.python.org/lockfile/lockfile.html
@niwinz
niwinz / gist:6461676
Last active December 22, 2015 10:49
Why I cannot bind some class to one variable on groovy?
[3/5.0.2]niwi@localhost:~/Downloads/groovy-2.2.0-beta-2> ./bin/groovysh
Groovy Shell (2.2.0-beta-2, JVM: 1.7.0_40)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
groovy:000> class Foo {}
===> true
groovy:000> instance1 = new Foo()
===> Foo@6f24bcc6
groovy:000> cls = Foo
===> class Foo
@niwinz
niwinz / monkey.py
Created September 9, 2013 17:53
Monkey patching for enable http401 to rest-framework.
from __future__ import print_function
import sys
from rest_framework import views
from rest_framework import status, exceptions
from rest_framework.response import Response
def patch_api_view():
from django.views.generic import View