Skip to content

Instantly share code, notes, and snippets.

@lexxai
lexxai / 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
@lexxai
lexxai / 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():
@lexxai
lexxai / 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
@lexxai
lexxai / 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
@lexxai
lexxai / 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}"
@lexxai
lexxai / 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}!"
@lexxai
lexxai / iscsi_mount
Last active October 13, 2024 18:02
Startup script 'iscsi_mount' rc.d (start / stop) for FreeBSD 9.0
#!/bin/sh
#
# PROVIDE: iscsi_mount
# REQUIRE: NETWORKING
# BEFORE: LOGIN
# KEYWORD: shutdown
. /etc/rc.subr
name="iscsi_mount"
rcvar="iscsi_mount_enable"
@lexxai
lexxai / iscsi-zfs
Last active May 11, 2023 23:47 — forked from 2510/iscsi-zfs
FreeBSD rc.script for activating ZFS pool over iSCSI
#!/bin/sh
# PROVIDE: iscsi-zfs
# REQUIRE: iscsid iscsictl mountcritlocal var
# KEYWORD: nojail shutdown
. /etc/rc.subr
name="iscsi_zfs"
start_cmd="${name}_start"