Skip to content

Instantly share code, notes, and snippets.

@vitalizzare
vitalizzare / my_app.py
Last active May 28, 2021 07:20
What does it mean in python: if __name__ == "__main__"
# In the programm my_app.py we only import
# the module "test.py" from the folder "modules"
import modules.test
@vitalizzare
vitalizzare / interrupt_processing_data.py
Created June 1, 2021 16:54
Finish processing the data neatly, save the state and exit by pressing Ctrl-C (Python CLI)
from signal import SIGINT, signal
from time import sleep
import pickle
def load(store):
try:
with open(store, 'br') as f:
start = pickle.load(f)
except Exception:
#!/bin/sh
# pydoc_grep <keyword>
# Grep for the keyword through the pydoc's topics
keyword="${1:?Give me a keyword to look for as a first parameter}"
python -m pydoc 'topics' | awk '/^[A-Z ]+$/ {for (i=1; i<=NF; i++) print $i}' | {
unset found
while read topic; do
python -m pydoc "$topic" | grep --ignore-case "$keyword" > /dev/null
#!/bin/sh
# Check whether we can obtain names for the control characters
# with the unicodedata.name method in python
curl --silent 'https://www.unicode.org/Public/13.0.0/ucd/NameAliases.txt' |\
sed '/^\ *$/d; /^#.*/d' |\
python -c '
import unicodedata
@vitalizzare
vitalizzare / sudoku_solver.py
Last active December 12, 2021 17:20
Another sudoku solver
from copy import deepcopy
from pprint import pprint
import numpy as np
# Idea borrowed from the video https://youtu.be/G_UYXzGuqvM
# and implemented in a kind of numpyish way
#
# TODO: 1) how can I use numpy.lib.stride_tricks.sliding_window_view
# or something like that to solve the task?
# 2) get rid of the array SOLUTION
@vitalizzare
vitalizzare / convert_number.py
Last active December 12, 2021 18:00
Number Systems and Radix Conversion (converting numbers to a given number base)
# python >= 3.8
assert (sys.version_info.major == 3 and
sys.version_info.minor >= 8)
from math import log
def convert(num:int, base=3) -> list:
'''Return a list of "digits" representing num in the given base,
ordered from lower to higher positions'''
width, num = int(1 + log(num, base)), (num,)
@vitalizzare
vitalizzare / skyline_max_height.py
Created January 15, 2022 15:19
Return the maximum height of a continuous sequence of true values in a 2D array, starting from the bottom
import numpy
def get_max_height(skyline:numpy.ndarray) -> int:
'''Returns the maximum height of a continuous sequence
of true values in a 2D array, starting from the bottom'''
mask = True
return sum(any(mask := row & mask)
for row in reversed(skyline.astype(bool)))
@vitalizzare
vitalizzare / total_video_duration.sh
Last active July 22, 2023 17:13
Calculating the total duration of all the videos in a directory passed as the first parameter, ffprobe and awk needed
#!/bin/ven bash
# Finding the total duration of all the videos
# in the local Videos directory by default
# or in the directory passed as the first parameter
folder="${1:-$HOME/Videos}"
[ -d "$folder" ] || { echo "Folder not found: $folder" >&2; exit 1; }
echo "Calculating total video duration in $folder" >&2