Skip to content

Instantly share code, notes, and snippets.

View CodeByAidan's full-sized avatar
💻
i love HPC/DL

aidan CodeByAidan

💻
i love HPC/DL
  • some college located in northeast usa
  • 20:27 (UTC -04:00)
View GitHub Profile
@CodeByAidan
CodeByAidan / better_typing.py
Last active July 26, 2024 15:38
will be updating this a lot whenever I want to use typing but with typing that's not present in typing library. :trollface: mypy + pylance ready!
from __future__ import annotations
from typing import Any, TypeVar, overload
T = TypeVar("T")
@overload
def cast(typ: type[T], val: Any, /) -> T: ...
@overload
@CodeByAidan
CodeByAidan / gradient_text_rich.py
Created July 24, 2024 19:23
create a colored gradient text using Rich in Python
def gradient_text(text: str, start_color: str, end_color: str) -> Text:
gradient = Text()
start: ColorTriplet | None = Color.parse(start_color).triplet
end: ColorTriplet | None = Color.parse(end_color).triplet
for i, char in enumerate(text):
ratio: float = i / (len(text) - 1)
blended_color = tuple(
int(start[j] + (end[j] - start[j]) * ratio) for j in range(3)
)
color_code: str = f"#{''.join(f'{value:02x}' for value in blended_color)}"
@CodeByAidan
CodeByAidan / get-path-clipboard-quoted.ps1
Created July 17, 2024 12:36
Copy the current path into your clipboard in a one-liner with PowerShell.
Set-Clipboard -Value ("`"" + (Get-Location).Path + "`"")
# "C:\Windows"
@CodeByAidan
CodeByAidan / Microsoft.PowerShell_profile.ps1
Created July 15, 2024 20:58
little sexy script I made to stop having fnm (Fast Node Manager - https://github.com/Schniz/fnm) from not being detected by the Path on PowerShell - just reinstalls it! You can do this same kinda thing with anything! This script is part of my $PROFILE
function Ensure-Fnm {
if (-not (Get-Command fnm -ErrorAction SilentlyContinue)) {
$flagFile = "$env:TEMP\fnm_installing.flag"
if (-not (Test-Path $flagFile)) {
New-Item -Path $flagFile -ItemType File | Out-Null
try {
Start-Process powershell -ArgumentList "-NoProfile -Command `"winget install Schniz.fnm; Remove-Item -Path $flagFile`"" -WindowStyle Hidden
$maxRetries = 12 # wait up to 60 seconds (12 * 5)
$retry = 0
while (-not (Get-Command fnm -ErrorAction SilentlyContinue) -and $retry -lt $maxRetries) {
@CodeByAidan
CodeByAidan / log_variables_in_function.py
Created July 15, 2024 19:13
Decorator that prints the variables in a function (being wrapped) values as they change. Pretty nifty.
import sys
from functools import wraps
from typing import Any, Callable, Optional, TypeVar
F = TypeVar("F", bound=Callable[..., Any])
def log_variables(func: F) -> F:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
def tracer(frame, event, arg) -> Optional[Callable]:
@CodeByAidan
CodeByAidan / log_variables_in_recursion.py
Last active July 15, 2024 18:02
One of the most painful code I've wrote, a decorator to log variables in a function every time they set/get. Notice the recursion part 😔
from functools import wraps
from typing import Any, Callable, TypeVar
F = TypeVar("F", bound=Callable[..., Any])
def log_variables(func: F) -> F:
@wraps(func)
def wrapper(*args, **kwargs) -> Any:
if not hasattr(wrapper, "initialized"):
@CodeByAidan
CodeByAidan / colors.py
Created July 8, 2024 19:21
who needs colorama when you have static typing and factories?
from __future__ import annotations
from typing import ClassVar, Type, TypedDict, Unpack, cast
from pydantic import BaseModel, Field, create_model, model_validator
class ColorData(TypedDict):
black: str
red: str
@CodeByAidan
CodeByAidan / wahgwandelilah.vb
Created July 8, 2024 19:10
i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love vba i love…
Public Function Similarity(ByVal String1 As String, _
ByVal String2 As String, _
Optional ByRef RetMatch As String, _
Optional min_match = 1) As Single
Dim b1() As Byte, b2() As Byte
Dim lngLen1 As Long, lngLen2 As Long
Dim lngResult As Long
If UCase(String1) = UCase(String2) Then
@CodeByAidan
CodeByAidan / DataFrameStore.py
Created July 2, 2024 13:16
Preserve any size DataFrame, load/save - FAST!
from typing import Optional, TypeGuard
import pandas as pd
from pandas import DataFrame
class DataFrameStore:
"""
A class to store and manage a DataFrame, with the ability to save and load it to
and from a file in Feather format.
@CodeByAidan
CodeByAidan / quiz.py
Created July 1, 2024 18:23
just some example quiz using model inheritance with @OverRide from typing in Python 3.12
import random
from dataclasses import dataclass
from string import ascii_lowercase
from typing import override
@dataclass
class Question:
prompt: str
answer: str