Skip to content

Instantly share code, notes, and snippets.

View takwas's full-sized avatar
🏠
Working from home

Olúwátóósìn Anímáṣahun takwas

🏠
Working from home
View GitHub Profile
from typing import Type, Union
class BaseType:
pass
class EvenType:
pass
class OddType:
import requests
class Req1:
def __init__(self):
self._session = requests.Session()
def do_req():
with self._session:
@takwas
takwas / fastapi_teardown.py
Created June 5, 2020 01:47
fastapi_teardown.py
def register_cleanup_hook(app: fastapi.FastAPI):
@app.on_event('shutdown')
async def cleanup_seq_handler():
print('Worker shutting down. Cleaning up ...')
logger.info('Worker shutting down. Cleaning up ...')
# do some cleanup ...
# THE SOLUTION IS THE FUNCTION `generate_first_elements`
# write a function which takes an iterable of iterators as
# an argument and returns a generator of first elements of
# the input iterators
def generate_first_elements(iterable_arg):
# return a "generator object" that produces the first
from functools import wraps
def log(utf8_string, tags):
# do some logging
pass
def log_on_exception(*tags):
"""Decorate a function to log any exception it raises.
@takwas
takwas / notes.md
Created May 1, 2019 19:23 — forked from DavidWittman/notes.md
A Brief Introduction to Fabric

A Brief Introduction to Fabric

Fabric is a deployment management framework written in Python which makes remotely managing multiple servers incredibly easy. If you've ever had to issue a change to a group servers, this should look pretty familiar:

for s in $(cat servers.txt); do ssh $s service httpd graceful; done

Fabric improves on this process by providing a suite of functions to run commands on the servers, as well as a number of other features which just aren't possible in a simple for loop. While a working knowledge of Python is helpful when using Fabric, it certainly isn't necessary. This tutorial will cover the steps necessary to get started with the framework and introduce how it can be used to improve on administering groups of servers.

@takwas
takwas / Python TCP Client Example.py
Created April 18, 2019 15:08 — forked from Integralist/Python TCP Client Example.py
Python TCP Client Server Example
import socket
hostname, sld, tld, port = 'www', 'integralist', 'co.uk', 80
target = '{}.{}.{}'.format(hostname, sld, tld)
# create an ipv4 (AF_INET) socket object using the tcp protocol (SOCK_STREAM)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect the client
# client.connect((target, port))
@takwas
takwas / netstat
Created October 13, 2017 18:51
netstat
tcp 0 0 192.163.X.Y:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
unix 2 [ ACC ] STREAM LISTENING 118650122 /var/run/dovecot/log-errors
unix 2 [ ACC ] STREAM LISTENING 118650220 /var/run/dovecot/anvil-auth-penalty
unix 2 [ ACC ] STREAM LISTENING 118650224 /var/run/dovecot/quota-status
unix 2 [ ACC ] STREAM LISTENING 118650227 /var/run/dovecot/master
@takwas
takwas / demo.py
Created August 14, 2017 18:23
Exception message printing demo
###########
# Python 2:
###########
try:
raise Exception
except Exception as e:
s,r = getattr(e, 'message') or str(e), getattr(e, 'message') or repr(e)
print 's:', s, 'len(s):', len(s)
print 'r:', r, 'len(r):', len(r)
@takwas
takwas / supervisor.conf
Created April 2, 2017 14:16 — forked from tsabat/supervisor.conf
Sample supervisor config file
; Sample supervisor config file.
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
;chmod=0700 ; sockef file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; (default is no username (open server))
;password=123 ; (default is no password (open server))
;[inet_http_server] ; inet (TCP) server disabled by default