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 / 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 ]
@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 / 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 / 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 / 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 / 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 / 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 / CIImage_CVPixelBuffer.swift
Created August 4, 2020 22:05
BGRA to BGR convertion
import Foundation
import UIKit
extension CGImage {
public var cvPixelBuffer: CVPixelBuffer? {
let attrs = [
kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferMetalCompatibilityKey: kCFBooleanTrue
] as CFDictionary
@emrahgunduz
emrahgunduz / mail.rc
Last active April 5, 2019 10:14
Mailx and configuration for Cron email sending
set ask askcc append dot save crt
ignore Received Message-Id Resent-Message-Id Status Mail-From Return-Path Via D$
account forcron {
set smtp-use-starttls
set ssl-verify=ignore
set smtp-auth=login
set smtp=smtp://example.com:587
set from="Email Address <email@example.com>"
set smtp-auth-user=USERNAME
@emrahgunduz
emrahgunduz / imap-sync.sh
Created February 28, 2017 07:04
A bash script to clone multiple email-password combinations from one imap host to another. Requires imapsync installed/compiled and a csv file for email list. Uses port 993+ssl as default.
#!/bin/sh
# CSV file line example:
# some.name@domain.com,PASSWORD
if [ -z "$1" ]
then
echo "CSV file is not specified!"
exit 1
fi