Skip to content

Instantly share code, notes, and snippets.

View pavelpatrin's full-sized avatar

Pavel Patrin pavelpatrin

View GitHub Profile
@pavelpatrin
pavelpatrin / flatten_wsdl.py
Created May 20, 2015 07:45
Нерабочий вариант функции для схлопывания wsdl-схем
def _get_flatten_wsdl(self, url):
"""
Выполняет загрузку wsdl-схемы и зависимых файлов и объединяет их
в единый файл.
Ускоряет загрузку wsdl-схемы для клиента.
TODO: реализовать.
:type url: unicode
@pavelpatrin
pavelpatrin / line_profiler_decorator.py
Last active September 6, 2023 14:49
Python line profiler decorator
def profile(func):
from functools import wraps
@wraps(func)
def wrapper(*args, **kwargs):
from line_profiler import LineProfiler
prof = LineProfiler()
try:
return prof(func)(*args, **kwargs)
finally:
#!/bin/sh
LOCKFILE="/tmp/script.lock"
LOGFILE="/tmp/script.log"
COMMAND="/usr/bin/php7 /home/rinat/myscript.php"
(
if flock -n 200
then
echo "["$(date)"] Running command $COMMAND" | tee -a $LOGFILE
@pavelpatrin
pavelpatrin / test_generators.py
Created July 2, 2017 22:34
Event loop with hands
import select
import socket
from urllib.parse import urlsplit
class Loop:
ACTION_READ = 'read'
ACTION_WRITE = 'write'
ACTION_READY = 'ready'
@pavelpatrin
pavelpatrin / test_states.py
Created July 2, 2017 22:35
Event loop with hands
import select
import socket
import typing
from urllib.parse import urlsplit
class Task:
def __init__(self, url):
self.split = urlsplit(url)
import select
import socket
from urllib.parse import urlsplit
class Poll:
ACTION_READ = 'read'
ACTION_WRITE = 'write'
class RawJoin(Join):
u"""Позволяет внедрить в QuerySet произвольный JOIN.
banners_qs = Banner.objects.extra(where=['my_join_table.status = 123'])
Join('LEFT JOIN (XXX) b1 ON b1.id = abcd ', ()).inject(banners_qs)
"""
def __init__(self, raw_sql, params):
# Используемые опции.
def matrix(side_width):
for y in xrange(side_width):
for x in xrange(side_width):
ring_number = min(x, y, side_width - 1 - x, side_width - 1 - y)
ring_serial = x + y - 2 * ring_number
if x >= y:
ring_base = 4 * ring_number * (side_width - ring_number)
result = ring_base + ring_serial
else:
@pavelpatrin
pavelpatrin / Flame-Graph-Middleware.py
Last active October 22, 2018 21:32
Flamegraph middleware that tracks requests
class FlamegraphMiddleware(object):
def __init__(self):
import os, socket, datetime
arguments = os.getpid(), socket.gethostname()
filename = '/tmp/flamegraph.%s.%s.log' % arguments
self.profiler = self.create_profiler(filename)
print('FlameGraph started: %s' % filename)
def __del__(self):
@pavelpatrin
pavelpatrin / Line-Profiler-Decorator.py
Last active October 22, 2018 21:31
Line profiler decorator
def profile(func):
from line_profiler import LineProfiler
def wrapper(*args, **kwargs):
profiler = LineProfiler(func)
try:
return profiler.runcall(func, *args, **kwargs)
finally:
profiler.print_stats()