This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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". |