Skip to content

Instantly share code, notes, and snippets.

View emrahgunduz's full-sized avatar

Emrah Gündüz emrahgunduz

  • @Impressions-app
  • California
View GitHub Profile
@emrahgunduz
emrahgunduz / health_check.py
Last active March 4, 2021 18:25
Python - Create a simple health check server (async)
import asyncio
import json
import threading
from aiohttp import web
def healthcheck_response ( data, *, text=None, body=None, status=200, reason=None, headers=None, content_type="application/json", dumps=json.dumps ):
return web.json_response(
data,
text=text,
@emrahgunduz
emrahgunduz / counter.py
Last active April 14, 2021 19:31
Python thread safe counter
from threading import Lock
class Counter( object ):
def __init__ ( self ):
self.__value = 0
self.__lock = Lock()
def increment ( self, by_value: int = 1 ):
with self.__lock:
@emrahgunduz
emrahgunduz / log.py
Created April 13, 2021 09:44
Simple auto flushing, thread safe logger for Elastic consumer (Singleton)
from __future__ import annotations
from typing import Union
import os
import uuid
import json
from datetime import datetime
from threading import Lock
class Log( object ):
@emrahgunduz
emrahgunduz / took.py
Created April 13, 2021 09:47
Simple thread safe process timer
from threading import Lock
import time
class Took:
def __init__ ( self ):
self.__lock = Lock()
with self.__lock:
self.__start = time.time()
self.__total = time.time()
@emrahgunduz
emrahgunduz / plot-multi.py
Created April 15, 2021 11:25
Plot multiple images (numpy array) with mathplotlib, defining only column size
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams["figure.figsize"] = 12,12
def plot_images(images, columns=4):
total = len(images)
nrow = ceil(total / columns)
ncol = columns
fig = plt.figure(figsize=(ncol+1, nrow+1))
@emrahgunduz
emrahgunduz / timed_class.py
Created July 8, 2021 19:51
A timed, self running, stoppable class for Python
from threading import Timer
from typing import ClassVar
from typing import Optional
class SelfRunner( object ):
_timeout: ClassVar[ float ] = 10.0
_timer: Optional[ Timer ] = None
_end_called: bool = False
@emrahgunduz
emrahgunduz / async_timer.py
Created August 18, 2021 09:21
Async timer with callback
import asyncio
from asyncio import Future
from collections import Callable
from typing import ClassVar
from typing import Optional
class Timer:
_task: Optional[ Future ] = None
_timeout: ClassVar[ float ]