Skip to content

Instantly share code, notes, and snippets.

View Greyvend's full-sized avatar

Serge Mosin Greyvend

View GitHub Profile
@Greyvend
Greyvend / repository_pattern.py
Created February 7, 2021 15:59
Repository pattern implementation in Python
"""
This is Python implementation of Repository pattern for accessing Data model
in an Object Oriented manner, simulating collection interface and abstracting
persistence operations.
The Repository also has Factory method for dealing with different Databases. Another
approach is to add direct engine string ingestion to the Repository __init__ method.
"""
from abc import ABC
@Greyvend
Greyvend / heap.py
Created June 16, 2020 08:56
Addressable Binary Heap
import math
from operator import lt
class Heap:
"""Binary heap, with pluggable comparison function."""
def __init__(self, cmp=lt):
self.len = 0
self.arr = []
@Greyvend
Greyvend / analysis.py
Last active September 14, 2017 10:42
Blog scraping and analysis code snippets. Refer to the corresponding repo for the full working example: https://github.com/Databrawl/blog_analysis.
from itertools import groupby
from operator import itemgetter
from utils import outlier_threshold
def filter_view_deviations(data):
query_sorted_data = sorted(data, key=itemgetter('query'))
result = []
for k, group in groupby(query_sorted_data, key=itemgetter('query')):
@Greyvend
Greyvend / server.py
Created May 30, 2017 06:47
Tornado TCP Server & Client with Redis connection and simple subscription protocol. Refer to the corresponding repo for the full working example: https://github.com/Databrawl/real_time_tcp
import signal
import asyncio
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.iostream import StreamClosedError
from tornado.tcpserver import TCPServer
from tornado.platform.asyncio import AsyncIOMainLoop, to_asyncio_future
import aioredis
@Greyvend
Greyvend / client.py
Created May 14, 2017 13:51
Tornado TCP Server & Client with Redis connection. Refer to the corresponding repo for the full working example: https://github.com/Databrawl/real_time_tcp/tree/3e01d85e719bf793a4811b2d701609a9a4d36597
from concurrent.futures import ThreadPoolExecutor
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.iostream import StreamClosedError
from tornado.tcpclient import TCPClient
class Client(TCPClient):
msg_separator = b'\r\n'
@Greyvend
Greyvend / client.py
Last active July 26, 2017 16:26
Tornado echo TCP Server & Client. Refer to the corresponding repo for the full working example: https://github.com/Databrawl/real_time_tcp/tree/3147a3f4bddf70616f19a7e4c41df6118d5f348a.
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.tcpclient import TCPClient
class Client(TCPClient):
msg_separator = b'\r\n'
@gen.coroutine
def run(self, host, port):
@Greyvend
Greyvend / gist:b912619625798edcad2a04ccfced75b5
Created February 20, 2017 15:34 — forked from nhoad/gist:8966377
Async stdio with asyncio
import os
import asyncio
import sys
from asyncio.streams import StreamWriter, FlowControlMixin
reader, writer = None, None
@asyncio.coroutine
def stdio(loop=None):
@Greyvend
Greyvend / Flask-Restful_S3_File_Upload.py
Created September 12, 2016 13:26 — forked from JMSwag/Flask-Restful_S3_File_Upload.py
Uploading a file to S3 while using Flask with Flask-Restful to create a REST API.
# -*- coding: utf-8 -*-
"""
An example flask application showing how to upload a file to S3
while creating a REST API using Flask-Restful.
Note: This method of uploading files is fine for smaller file sizes,
but uploads should be queued using something like celery for
larger ones.
"""
from cStringIO import StringIO
@Greyvend
Greyvend / apiary.apib
Created March 6, 2016 13:02
API blueprint for Mongo Polygon field.
# Data structures
## Polygon
- name : Big polygon (string, required) - name of the polygon
- points : [[[0, 0], [0, 5], [5, 5], [0, 5], [0, 0]], [[1, 1], [4, 1], [4, 4], [1, 4], [1, 1]]] (required) - list of lists of points (x, y) coordinate pairs.
First list is an enclosing polygon. Next ones are cut out areas.