Skip to content

Instantly share code, notes, and snippets.

Avatar

Ilya Konstantinov ikonst

View GitHub Profile
@ikonst
ikonst / mocker.py
Created Nov 11, 2021
pytest_mock with patch.method
View mocker.py
import gc
import sys
import types
import unittest.mock
from typing import Any
from typing import Callable
from typing import Generator
from typing import Optional
from typing import TYPE_CHECKING
View mock_instance_method.py
class Foo():
message = 'foo'
def bar(self):
print('bar')
from unittest.mock import patch
def baz(self):
print(self.message)
View pynamodb_test_date.py
from datetime import datetime
import pytz
import dateutil.tz
from pynamodb.models import Model
from pynamodb.attributes import NumberAttribute, UTCDateTimeAttribute
class MyModel(Model):
class Meta:
table_name = 'my_model'
View PhpArrayModernCodec.php
<?php
use DateTime;
use DrSlump\Protobuf;
use google\protobuf\Timestamp;
class PhpArrayModernCodec extends \DrSlump\Protobuf\Codec\PhpArray {
protected function encodeMessage(Protobuf\Message $message)
{
if ($message instanceof Timestamp) {
View abort_on_eof.py
from contextlib import contextmanager
from functools import wraps
from socket import socket as socket_type, MSG_PEEK
from typing import Set
import flask
import gevent
def _abort_on_eof_watchdog(socket: socket_type, greenlets: Set[gevent.Greenlet]) -> bool:
@ikonst
ikonst / abort_on_dc.py
Last active May 14, 2019
Example of a gevent-based Flask server that aborts when downstream disconnects
View abort_on_dc.py
"""
Implements a behavior similar to:
- Go srv's cancelation of context on downstream disconnect
- PHP (unless `ignore_user_abort` is true)
"""
import gevent.monkey
gevent.monkey.patch_all()
from contextlib import contextmanager
from flask import Flask
@ikonst
ikonst / mongoengine_allow_inheritance_be_careful.py
Created Dec 19, 2017
One doesn't just add "allow_inheritance=True"
View mongoengine_allow_inheritance_be_careful.py
from mongoengine import Document, StringField, ListField, EmbeddedDocument, EmbeddedDocumentField
# ******** before remodel: **********
class FailedChargeAttempt_Old(EmbeddedDocument):
id = StringField()
failed_reason = StringField()
View get_org_contribs.py
from github import Github
from csv import DictWriter
# Generate on https://github.com/settings/tokens
TOKEN = '<REDACTED>'
ORG = 'lyft'
AUTHOR = '<github username here>'
g = Github(login_or_token=TOKEN)
org = g.get_organization(ORG)
View mongoengine_referencing_documents.py
from mongoengine import Document, EmbeddedDocument, Q, ReferenceField
from pymongo.read_preferences import ReadPreference
all_models = Document.__subclasses__()
def referencing_models(document):
for model in all_models:
if issubclass(model, EmbeddedDocument):
continue
fields = [field for field in get_reference_fields_of_model(model)
View mov2gif.sh
#!/bin/sh
set -e
# Converts a Quicktime movie to a GIF aniamtion.
# Useful for screen recordings.
# Preliminary step with palette required to make it look good
# without dithering artifacts.
FPS=10
PALETTE=$(mktemp).png