Skip to content

Instantly share code, notes, and snippets.

View VietThan's full-sized avatar

Viet Than VietThan

View GitHub Profile
@VietThan
VietThan / litestar_get_favicon.py
Created March 15, 2024 14:04
Litestar get favicon.ico
import pathlib
from litestar import Litestar, get
from litestar.response import File
@get(path='/favicon.ico')
async def handle_favicon_get() -> File:
icon_path = pathlib.Path(__file__).parent / 'crylaugh2.ico'
return File(
path=icon_path,
@VietThan
VietThan / rage_not_found.py
Created November 15, 2023 15:58
rage face for notfound page in pyramid
from pyramid.view import notfound_view_config
@notfound_view_config(renderer='string')
def notfound_view(request):
request.response.status = 404
return f"""Page not found: path is {request.path}
``..:/oydhhMMhsyhdy+/::/.
:sddhyo+/:-. -NN :mMMMMMy`
`.-:/oyNMmhyyso+/-.` `NMN`-yMMMm+-+yms.
@VietThan
VietThan / docker-compose.py
Created August 2, 2023 16:49
Invoke docker compose with python and subprocess
# inspiration from https://github.com/themotte/rDrama/commit/9da3451ed97ec62cf4dc403e2863ae7d2fdfba98
# import pprint
import logging
import subprocess
from collections.abc import Callable
LOGGER = logging.getLogger(__name__)
@VietThan
VietThan / dict_merge.py
Last active July 19, 2023 13:32
merging dicts yaml for python
from collections import abc
BASIC_TYPES = (int, float, complex, bool, str, bytes)
# modified off https://stackoverflow.com/a/15836901
def dict_merge(a, b):
"""
merges b into a and return merged result
NOTE: tuples and arbitrary objects are not handled as it is totally ambiguous what should happen
"""
@VietThan
VietThan / configs.py
Last active June 23, 2023 15:43
Thread-safe object to store your configs
from threading import Lock, Thread
from pathlib import Path
from typing import Optional, Union
import os
import logging
class SingletonMeta(type):
"""
This is a thread-safe implementation of Singleton.
@VietThan
VietThan / truncate_float_f_up_to_n_decimals.py
Created May 22, 2023 18:11
Set float up to a certain decimal point without rounding in Python
def truncate_float(f: float, n: int):
"""
Truncate float `f` to at most `n` decimal points.
- truncate_float(1.9333, 2) -> 1.93
- truncate_float(1.9373, 2) -> 1.93
- truncate_float(1.9, 2) -> 1.9
https://stackoverflow.com/questions/29246455/python-setting-decimal-place-range-without-rounding
Parameters
@VietThan
VietThan / ZabbixSender.py
Created February 14, 2022 16:59
AWS Lambda python3 script for sending metric value to zabbix
import json
import socket
import struct
import time
import json
class ZabbixSender:
'''
Sender to Zabbix