Skip to content

Instantly share code, notes, and snippets.

View vu3jej's full-sized avatar
🌴
On vacation

vu3jej

🌴
On vacation
View GitHub Profile
@nymous
nymous / README.md
Last active May 14, 2024 20:34
Logging setup for FastAPI, Uvicorn and Structlog (with Datadog integration)

Logging setup for FastAPI

This logging setup configures Structlog to output pretty logs in development, and JSON log lines in production.

Then, you can use Structlog loggers or standard logging loggers, and they both will be processed by the Structlog pipeline (see the hello() endpoint for reference). That way any log generated by your dependencies will also be processed and enriched, even if they know nothing about Structlog!

Requests are assigned a correlation ID with the asgi-correlation-id middleware (either captured from incoming request or generated on the fly). All logs are linked to the correlation ID, and to the Datadog trace/span if instrumented. This data "global to the request" is stored in context vars, and automatically added to all logs produced during the request thanks to Structlog. You can add to these "global local variables" at any point in an endpoint with `structlog.contextvars.bind_contextvars(custom

@marcus-at-localhost
marcus-at-localhost / bootstrap-validation.js
Last active October 30, 2023 14:53
[Bootstrap Form Validation HTMX Extension] See example: https://jsfiddle.net/hxjvw62L/ #htmx #bootstrap
htmx.defineExtension('bs-validation', {
onEvent: function (name, evt, data) {
let form = evt.detail.elt;
// check if trigger attribute and submit event exists
// for the form
if(!form.hasAttribute('hx-trigger')){
// set trigger for custom event bs-send
form.setAttribute('hx-trigger','bs-send');
import asyncio
class AsyncConstructorMeta(type):
"""Metaclass to support asynchronous constructors in Python.
Basically we're exploiting the fact that __new__ can return anything in Python.
So we're taking the old __init__ code, removing it from the class, and instead,
we create a custom __new__ method that returns a coroutine wrapping the original
constructor.
@mithicher
mithicher / tom-select.blade.php
Last active April 25, 2024 16:10
Tom Select Livewire Blade Component
/* Component Usage
// Data for options
$users = \App\User::limit(6)->get()->transform(fn($user) => [
'id' => $user->id,
'title' => $user->name,
'subtitle' => $user->email
]);
// Usage in view
@nathandaly
nathandaly / modal.blade.php
Last active January 31, 2024 11:30
Alpine Bootstrap Modal
<div name="{{ $name }}"
x-cloak
x-data="{ show: false, name: '{{ $name }}', details: null }"
@modal.window="
show = ($event.detail.name === name);
details = $event.detail;
"
@modal:close-all.window="show = false"
class="modal d-block"
tabindex="-1"
@muellerzr
muellerzr / make_pr
Created January 24, 2021 05:44
Make a new pr from an existing cloned repository by simply doing `make_pr`
#!/bin/bash
echo "Setting up a new PR"
# Ask for username
echo "Please enter the original GitHub organization or username of the repository (the part of the URL after github.com/): "
read org
# Ask for repo
echo "Please enter the repository name: "
read name
#Ask for branch
read -e -p "Please enter the branch of the repository you wish to use: " -i "main" branch
@giner
giner / fix_ibus-mozc.sh
Last active April 8, 2023 08:04
JAPANESE: Make ibus-mozc remember last used mode
# Make ibus-mozc remember the last used mode (e.g. hiragana) and not switch back to Alphabet on restart
# See https://github.com/google/mozc/issues/381
cd $(mktemp -d)
apt source ibus-mozc
cd mozc-*/
patch src/unix/ibus/property_handler.cc << 'EOF'
--- src/unix/ibus/property_handler.cc.orig 2020-10-28 17:21:18.000849932 +0900
+++ src/unix/ibus/property_handler.cc 2020-10-28 17:21:34.172696046 +0900
@@ -80,7 +80,7 @@
@herotux
herotux / admin.py
Created May 11, 2018 11:32 — forked from padurets/admin.py
django rest api framework session auth example
from django.contrib import admin
from .models import User
admin.site.register([User])
@speedplane
speedplane / celery_app.py
Created October 25, 2017 02:02
Celery Autoscaler Based on Memory and System Load
import multiprocessing
import re
from celery import Celery
from celery.worker.autoscale import Autoscaler as CeleryAutoscaler
class DAAutoscaler(CeleryAutoscaler):
# Try to keep the load above this point.
LOAD_MIN = .8
# Try to keep the load below this.
LOAD_MAX = 1.1