Skip to content

Instantly share code, notes, and snippets.

#!/bin/sh
SWAP_FILE="/root/swap1"
SWAP_SIZE=10240
FSTAB="/etc/fstab"
SWAP_ENTRY="md99\tnone\tswap\tsw,file=${SWAP_FILE},late\t0\t0"
# Check if swapfile line is already in /etc/fstab
if ! grep -qF "${SWAP_FILE}" "${FSTAB}"; then
@lexsysko
lexsysko / cache_headers.py
Last active March 13, 2025 21:27
MixinCacheHeaders
import hashlib
from datetime import datetime, UTC
from django.conf import settings
from django.core.exceptions import FieldError
from django.http import HttpResponseNotModified, HttpResponse
from django.utils import timezone
from django.utils.http import http_date, urlencode, parse_http_date
from rest_framework.response import Response
@lexsysko
lexsysko / async_to_thread_cancelling.py
Last active December 25, 2024 02:15
Asyncio to_thread Cancelling task
import asyncio
import threading
import time
# Stop event to signal the thread to terminate
stop_event = threading.Event()
async def proc1():
"""
Infinite loop async
@lexsysko
lexsysko / test_speed_isdigit.py
Last active November 24, 2024 15:20
test_speed_isdigit.py
import timeit
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
import re
def version_1(string: str) -> str:
digits = []
for char in string:
if char.isdigit():
@lexsysko
lexsysko / dns_acme.sh
Created November 19, 2024 23:45
dns_acme shell script for TrueNAS Scale
#!/bin/bash
### VARIABLES
# Logfile
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
LOGFILE="${SCRIPT_DIR}/dns_acme.log"
# Source acmesh scripts
export ACME_FOLDER="/mnt/mdata/system_tools/acme/acme.sh" # Change this path to reflect yourf environment
@lexsysko
lexsysko / test_speed_optimization.py
Last active November 14, 2024 14:17
test_speed_optimization.py
import timeit
import matplotlib.pyplot as plt
def version_1(category: str) -> tuple[str, str | None, str | None]:
category_list = category.split("-", 4)
category_list_length = len(category_list)
folder_type = category_list[0]
version_tag = category_list[1] if category_list_length > 1 else None
version = category_list[2] if category_list_length > 2 else None
@lexsysko
lexsysko / cached_enum_many_ways.py
Created November 10, 2024 17:36
cached_enum_many_ways.py
import sys
from enum import Enum
from functools import lru_cache
if sys.version_info >= (3, 11):
from enum import StrEnum
else:
class StrEnum(str, Enum): ...
import sys
from enum import Enum
from functools import lru_cache
if sys.version_info >= (3, 11):
from enum import StrEnum
else:
class StrEnum(str, Enum): ...
from types import MethodType
class Person:
def __init__(self, name):
self.name = name
# A function to add dynamically as a method
def say_hello(self):
return f"Hello, my name is {self.name}"
@lexsysko
lexsysko / adv_types_MethodType.py
Last active October 30, 2024 14:36
Python. types.MethodType.py
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hi there!"
def new_greet(self):
return f"Hello, {self.name}!"