Skip to content

Instantly share code, notes, and snippets.

@gboeer
gboeer / create_swapfile.sh
Created May 8, 2024 09:27
This script is used to manage swap space on a Linux system. It first disables all swapping, deletes any existing swap file, creates a new swap file with a size of 12000 MB, sets appropriate permissions for security, formats the file as swap space, and finally activates the swap space.
#!/bin/bash
# Disable swapping for all swap devices and files.
sudo swapoff -a
# Remove the swap file.
sudo rm /swapfile
# Create a new swap file with size 12000 MB.
sudo dd if=/dev/zero of=/swapfile bs=1M count=12000
def array_chunks(arr, chunk_size):
"""
Splits an array into chunks of up to `chunk_size` elements.
Args:
arr (list): The input array to be split into chunks.
chunk_size (int): The size of each chunk. The last chunk may be smaller if there are not enough elements left.
Returns:
list of lists: A list where each element is a chunk of the original array, up to `chunk_size` in length.
@gboeer
gboeer / flatten_list.py
Last active February 21, 2024 09:26
Flattens a nested list of arbitrary depth into a flat list.
def flatten(lst):
"""
Flattens a nested list of arbitrary depth into a flat list.
This function recursively traverses each element in a list. If an element is a list,
it is further flattened. This continues until no nested lists remain, producing a
flat list containing all non-list elements from the original nested structure.
Parameters:
- lst (list): The list to be flattened. Can contain nested lists of arbitrary depth.
@gboeer
gboeer / pydantic_model_field.py
Created February 15, 2024 10:49
Retrieve a field from a pydantic BaseModel which just has the given name or has the name set as an alias
from typing import Optional
from pydantic import BaseModel
def get_model_field(model: BaseModel, field_name: str) -> Optional[str]:
"""
Retrieves the field name from a Pydantic model based on a given field name or alias.
This function searches the model's fields to find a match for the given field name or alias.
If a match is found, it returns the actual field name. Since pydantic allows using
@gboeer
gboeer / substring_idx.py
Created December 1, 2023 10:51
Python: Using regular expressions, return a list of all starting indices for a given substring in a string
import re
def substring_idx(text, substring):
"""Return the starting indices of all occurrences of substring in text."""
pattern = re.escape(substring)
return [match.start() for match in re.finditer(pattern, text)]
@gboeer
gboeer / bits_to_dec.py
Created December 3, 2021 14:54
Convert an array or string of bits to decimal value
def bits_to_dec(arr):
"""
Works for all of those cases:
[False, True, True] --> 3
[1, 0, 1, 0] --> 6
"0111" --> 7
[1, '0', 0, False, '1', True] --> 35
"""
return int("".join([str(int(x)) for x in arr]), 2)
@gboeer
gboeer / CustomQActionGroup.cpp
Created March 27, 2018 15:39
For QT5 this is an easy way to use a QActionGroup or a QButtonGroup to allow for an exclusive selection/checking of Actions/Buttons or no selection at all.
/*
Keywords: C++, QT, QT5, GUI, QActionGroup, QButtonGroup
By default a QActionGroup allows for an exclusive selection of actions (only one action can be selected at a time).
However there is no way to configure it out of the box to allow for exclusive selection or no selection at all.
Here is an easy way how to use the QActionGroup to enable also no selection, without the need to subclass it.
*/
#include <QActionGroup>
#include <QAction>
@gboeer
gboeer / MyGraphicsView.cpp
Created February 9, 2018 14:40
Enable dragging of a QT5 QGraphicsView with middle mouse button (or any other button than the default)
/**
So you want to use the QGraphicsView Drag Mode but with another mouse button than the default left click?
You can of course reimplement the mouse_move_event in a subclass but did you notice how ugly it is to implement
all the dragging functionality by yourself?
All you want to do is use another mouse_button with the existing dragging functionality
but there is no such thing as a setDraggingButton function.
Here we use a little hack which enables the dragging mode only temporarly when clicking a button of your choice and then
sends a left-click event which will trigger the standard dragging functionality of the QGraphicsView.
@gboeer
gboeer / gist:1231475
Created September 21, 2011 07:33
C++ String Tokenizer
#include <string>
#include <vector>
//! Tokenize the given string str with given delimiter. If no delimiter is given whitespace is used.
void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " ")
{
tokens.clear();
// Skip delimiters at beginning.
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".