Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf8 -*-
from __future__ import absolute_import, print_function
import six
class BaseFieldDao(object):
_value = None
_entity = None
@w495
w495 / cyrillic2latin_file_renamer.py
Created August 13, 2017 00:48 — forked from etrushkin/cyrillic2latin_file_renamer.py
Cyrillic to Latin File Changer
#!/usr/bin/python
# -*- coding: utf-8 -*-
# http://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python
from __future__ import print_function
import sys
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
import errno
import logging
class OneLineHandler(logging.StreamHandler):
def emit(self, record):
try:
msg = self.format(record)
stream = self.stream
stream.write('\u001b[1000D' + msg)
@w495
w495 / pyav_rtmp_example.py
Last active June 15, 2019 21:15
Пример работы с видео в Python. Есть RTMP-поток и RTMP-сервер, на который нужно отправить этот поток. Создано по мотивам https://ru.stackoverflow.com/a/664973/203032
# -*- coding: utf8 -*-
import av
# Откроем ресурс на чтение
input_resource = av.open(
'rtmp://src_stream:1935/play'
)
# Откроем ресурс на запись.
output_resource = av.open(
@w495
w495 / aggregate_concat.py
Last active April 18, 2017 17:09 — forked from ludoo/aggregate_concat.py
Django aggregates GROUP_CONCAT support for MySQL
"""
From http://harkablog.com/inside-the-django-orm-aggregates.html
with a couple of fixes.
Usage: MyModel.objects.all().annotate(new_attribute=GroupConcat('related__attribute', separator=':')
"""
from django.db.models import Aggregate
from django.db.models.sql.aggregates import Aggregate as SQLAggregate
@w495
w495 / dns_resolver_middleware.py
Last active June 21, 2017 00:19
Scrapy downloader middleware class for handling IPv6-only hosts.
# -*- coding: utf8 -*-
from __future__ import absolute_import, division, print_function
import socket
from urlparse import urlparse
class DnsResolverMiddleware(object):
"""
from apps.genapi import models
class RegionTreeManager(models.Manager):
def handle_qs(self, qs):
not_detailed_query = "not find_in_set('detailed', Options)"
not_deleted_query = "not find_in_set('deleted', Options)"
qs = qs.extra(where=[not_detailed_query, not_deleted_query])
@w495
w495 / bashline.sty
Last active October 24, 2016 22:13
bashline.sty — simple implementation of command line calls for [Xe]LaTeX.
% !TeX encoding = UTF-8
\ProvidesPackage{bashline}[2016/10/24 v. 0.1]
\makeatletter
\newcommand{\bashline@file@name}[1]{%
/tmp/${USER}-${HOSTNAME}-\jobname-#1.tex%
}
\newread\bashline@file
\newcommand{\bashline@command@one}[2][tmp]{%
\immediate\write18{#2 > \bashline@file@name{#1}}
@w495
w495 / dict_to_xml.py
Created October 10, 2016 16:42
Dumps python dict to xml string. Current example is suitable for dumping OVS-XML format from python dictionay
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import lxml.etree as et
import six
def dumps(data):
name = data.keys()[0]
value = data[name]
@w495
w495 / process_pool_executor_example.py
Last active April 21, 2017 00:30
Example for concurrent.futures.ProcessPoolExecutor. One of the key ideas to use `as_completed` to get un-ordered result. If a sequence order is import, you should sort result by yourself. This is part of https://github.com/w495/python-video-shot-detector
import itertools
import multiprocessing as mp
from concurrent.futures import ProcessPoolExecutor, as_completed
from .base_extractor import BaseExtractor
class ParallelExtractor(BaseExtractor):
POOL_SIZE = mp.cpu_count()
IMAGE_GROUP_SIZE = 512