Skip to content

Instantly share code, notes, and snippets.

View brunomsantiago's full-sized avatar

Bruno Santiago brunomsantiago

  • Brazil
View GitHub Profile
@brunomsantiago
brunomsantiago / search_df.py
Created April 28, 2021 22:49
String search on pandas dataframe
import numpy as np
import pandas as pd
from random import choice, randint
names = ['David', 'James', 'John', 'Michael', 'Richard', 'Robert', 'William']
surnames = ['Davis', 'Jones', 'Lee', 'Miller', 'Moore', 'Smith', 'Taylor']
colors = ['Black', 'Blue', 'Green', 'Purple', 'Red', 'White', 'Yellow']
pets = ['', 'Dog', 'Cat', 'Bird', 'Fish', 'Bunny', 'Hamster', 'Guinea Pig']
def random_person():
@brunomsantiago
brunomsantiago / rename.py
Created April 8, 2021 12:30
Batch rename (serialize) video files
from pathlib import Path
# Input parameters
folders = ['/my/video/folder1', '/my/video/folder2']
video_file_types = 'avi flv m4v mkv mov mp4 mpg wmv'.split(' ')
# Folders as pathlib objects
folders = [Path(folder) for folder in folders if Path(folder).is_dir()]
# List and sort all files from all folders
@brunomsantiago
brunomsantiago / human_readable_size.py
Last active April 6, 2021 19:37
human_readable_size
import math
def human_readable_size(size_bytes, base=1024, decimal_places=2):
suffixes = 'B KB MB GB TB PB EB ZB YB'.split(' ')
if size_bytes == 0:
return '0B'
magnitude_index = int(math.floor(math.log(size_bytes, base)))
magnitude_bytes = math.pow(base, magnitude_index)
size = size_bytes / magnitude_bytes
suffix = suffixes[magnitude_index]
@brunomsantiago
brunomsantiago / toggle_touchpad.py
Created March 25, 2021 23:40
Toogle (enable/disable) Thinkpad 440p touchpad on Linux
#!/home/bms/miniconda3/bin/python3
# This script toggles status (enabled/disabled) of my notebook touchpad. My
# notebook is a Thinkpad 440p and its touchpad is a Synaptics TM2722-001. The
# system is Linux Mint. I map it to a keyboard shortcut (alt+F11 in my case).
#
# I tried just using: xinput disable 14
# ... but the device id keeps changing after boot.
#
# I tried just using: xinput disable "Synaptics TM2722-001"
@brunomsantiago
brunomsantiago / video_to_thumb_mosaic.py
Created March 24, 2021 23:30
Making thumbnail mosaic from videos using Python and OpenCV
from pathlib import Path
import cv2
import numpy as np
def make_mosaic(cap, n_rows=4, n_cols=5):
n_frames = n_rows * n_cols
cap_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
floor = int(cap_frames*0.05)
ceil = int(cap_frames*0.95)
frames_indexes = np.linspace(floor, ceil, n_frames).astype(int)
# Imports to download the wav file. Not necessary for local files.
from requests import get
from io import BytesIO
# Import to read the wav file.
from scipy.io import wavfile
# Import to plot the waveform.
import matplotlib.pyplot as plt