Skip to content

Instantly share code, notes, and snippets.

@serg06
serg06 / any.hpp
Last active August 14, 2024 15:39
Simple header-only C++11 implementation of std::any (tested with GCC)
#include <functional>
#include <typeinfo>
#include <type_traits>
namespace std17
{
class bad_any_cast : public std::bad_cast
{
public:
bad_any_cast() noexcept = default;
@serg06
serg06 / all.json
Created October 5, 2020 02:26
Jerma Youtube captions archive Oct 04 2020
This file has been truncated, but you can view the full file.
{
"id": "UUK3kaNXbB57CLcyhtccV_yw",
"videos": {
"t9OC2ToZIQ0": {
"id": "t9OC2ToZIQ0",
"title": "Yearly Channel Doctor's Appointment",
"description": "You thought I ran out of Spider-Man games? \n\n2nd Channel\nhttps://www.youtube.com/channel/UCL7DDQWP6x7wy0O6L5ZIgxg\n\nTwitch\nhttp://www.twitch.tv/jerma985\n\nTwitter\nhttp://www.twitter.com/jerma985",
"thumbnail": "https://i.ytimg.com/vi/t9OC2ToZIQ0/hqdefault.jpg?sqp=-oaymwEZCNACELwBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLBqzhvyJZO0NP_N8-7VJ-UNBWyjFQ",
"date": "20170829",
"duration": 554
@serg06
serg06 / qthread2.py
Last active February 28, 2021 02:07
Fix for PySIde2 QThread started signal executing/blocking on main thread
# Use this class instead of QThread
class QThread2(QThread):
# Use this signal instead of "started"
started2 = Signal()
def __init__(self):
QThread.__init__(self)
self.started.connect(self.onStarted)
def onStarted(self):
@serg06
serg06 / main.py
Last active March 11, 2021 21:26
PyQt5 / PySide2 QT StyleSheets (qss) dynamic "class" property example
from PySide2.QtCore import QObject, QTimer
from PySide2.QtWidgets import QApplication, QDialog
import sys
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.setWindowTitle("My Form")
self.setStyleSheet("""
@serg06
serg06 / load-script.js
Last active October 22, 2024 13:37
[JS] Load scripts dynamically
// Function to load a JS file
async function loadScript(url) {
return await new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = url;
script.addEventListener('load', () => resolve());
script.addEventListener('error', () => reject());
document.body.appendChild(script);
});
}
@serg06
serg06 / enums.ts
Last active April 9, 2022 05:39
[TypeScript] Iterating over enums
function isStringEnum<T extends Record<string, string | number>>(t: T): boolean {
return typeof Object.values(t).pop() === 'string';
}
export function enumKeys<T extends Record<string, string | number>>(t: T): (keyof T)[] {
return isStringEnum(t) ? Object.keys(t) : Object.keys(t).slice(0, Object.keys(t).length / 2);
}
export function enumValues<T extends Record<string, string>>(t: T): string[];
export function enumValues<T extends Record<string, string | number>>(t: T): number[];
@serg06
serg06 / regex.ts
Last active April 9, 2022 05:39
[TypeScript] Types for JavaScript's replacerFunction in String.prototype.replaceAll
export interface ReplacerArgs {
match: string; // the matched substring
groups: string[]; // captured groups
offset: number; // offset of match
string: string; // entire string
named_groups?: Record<string, string>; // named capturing groups
}
function parseReplacerArgsMutating(args: [string, ...any[]]): ReplacerArgs {
const named_groups = typeof args[args.length - 1] === 'object' ? (args.pop() as Record<string, string>) : undefined;
@serg06
serg06 / powerMod.ts
Last active April 12, 2022 02:01
[TypeScript] powerMod implementation
// x^p mod m
function powerMod(x: number, p: number, m: number): number {
if (m === 1) {
return 0;
}
x = x % m;
let result = 1;
while (p > 0) {
if (p % 2 === 1) {
@serg06
serg06 / iprange.py
Last active April 16, 2022 19:57
[Python] Get all IPs in a range
import itertools as it
"""
Given a start/end IP, return all IPs in that range (inclusive)
"""
def ipRange(start_ip: str, end_ip: str):
start = [int(x) for x in start_ip.split('.')]
end = [int(x) for x in end_ip.split('.')]
yield start_ip
from scipy import ndimage
import numpy as np
from PIL import Image
from random import randint
from copy import deepcopy
# set the file path to wherever your provinces.png is located
im = Image.open(r"colors.png")
print('-------------------------------------------')
# DEBUGGING: simply prints the format, size, and mode of your file