Skip to content

Instantly share code, notes, and snippets.

@timhughes
timhughes / conftest.py
Last active April 13, 2020 02:00
Failed example at test isolation
import secrets
from datetime import timedelta
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from fastapp import settings, crud
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
@timhughes
timhughes / sub01.py
Last active March 29, 2020 16:37
Different methods of creating subcommands with argparse
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 Tim Hughes <[email protected]>
#
# Distributed under terms of the MIT license.
"""
@timhughes
timhughes / conftest.py
Created March 27, 2020 11:48
pytest-mysql with sqlalchemy client
"""Tests main conftest file."""
import sys
import warnings
import pytest
sys.dont_write_bytecode = True
if not sys.version_info >= (3, 6):
warnings.simplefilter("error", category=DeprecationWarning)
@timhughes
timhughes / fastapi_websocket_redis_pubsub.py
Last active February 21, 2025 08:41
FastAPI Websocket Bidirectional Redis PubSub
"""
Usage:
Make sure that redis is running on localhost (or adjust the url)
Install uvicorn or some other asgi server https://asgi.readthedocs.io/en/latest/implementations.html
pip install -u uvicorn
Install dependencies
@timhughes
timhughes / abcmetaexample.py
Created March 14, 2020 13:06
python ABCMeta example
from abc import ABCMeta, abstractmethod
class SomeClass:
def __init__(self):
pass
def another_request(self):
print('test')
@timhughes
timhughes / awesome_python.md
Last active November 15, 2021 23:29
My Awesome Python

My Awesome Python

Packages

  • Arrow: Better dates & times for Python
  • PyPattyrn: A simple library for implementing common design patterns.
  • zerorpc: Like grpc but using ZeroMQ and MessagePack.
  • boltons: Boltons is a set of pure-Python utilities in the same spirit as — and yet conspicuously missing from — the standard library.
  • schedule: Python job scheduling for humans.
@timhughes
timhughes / rate_limiting_bucket.py
Created February 4, 2020 01:24
rate_limiting_bucket.py
"""
TokenBucket implementation from http://code.activestate.com/recipes/511490-implementation-of-the-token-bucket-algorithm/
"""
import asyncio
from time import time
from math import floor
import uvicorn
from fastapi import BackgroundTasks
from fastapi import FastAPI, BackgroundTasks
sudo dnf -y install dnf-automatic
sudo systemctl enable --now dnf-automatic-install.timer
sudo dnf -y upgrade --refresh
sudo dnf -y install dnf-plugin-system-upgrade
sudo dnf -y system-upgrade --allowerasing download --releasever=32
sudo dnf -y system-upgrade reboot
@timhughes
timhughes / calamari.py
Created December 14, 2019 01:41
Yum Squid
#!/usr/bin/env python
from __future__ import print_function
import sys
import logging
import traceback
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(module)-12s %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename='/var/log/squid/calamari.log',
filemode='w')
@timhughes
timhughes / ipv6server.py
Created October 11, 2019 14:30
ipv6server.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 Tim Hughes <[email protected]>
#
# Distributed under terms of the MIT license.
"""