Skip to content

Instantly share code, notes, and snippets.

View anselmobd's full-sized avatar

Anselmo Blanco Dominguez anselmobd

View GitHub Profile
@anselmobd
anselmobd / clearscr.py
Last active April 20, 2023 14:23
Console clear screen
#!/usr/bin/env python3
import shutil
def clearscr():
terminal_size=shutil.get_terminal_size()
print("\n"*(terminal_size.lines-2))
@anselmobd
anselmobd / dict_fstring_or_format.py
Created November 8, 2022 00:50
Performance evaluation of three ways to format a string with values stored in a dictionary
import timeit
def v_fstring(row):
return f"{row['group']}.{row['sub']}.{row['num']:05}"
def v_format(row):
return "{group}.{sub}.{num:05}".format(**row)
@anselmobd
anselmobd / uptime.sh
Last active August 11, 2022 23:39
Better uptime to log (compact and standardized)
#!/bin/bash
if [ ! -f $0.pkgchkok ] ; then
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' bc 2> /dev/null |grep "install ok installed")
if [ "" = "$PKG_OK" ] ; then
echo "Required 'bc' package not installed"
exit 1
else
touch $0.pkgchkok
fi
@anselmobd
anselmobd / cmd_out_log.sh
Last active August 11, 2022 22:55
Executes a command, stores its output and prints the differences compared to the last execution (for log)
#!/bin/bash
last_out="$1"
shift
exec_cmd="$@"
script_date=$(date)
date_to_file=$(date '+%Y-%m-%d_%H.%M.%S')
base_dir=$(dirname ${last_out})
name_out=$(basename ${last_out})
@anselmobd
anselmobd / decorator_method_idle_on_none.py
Last active May 22, 2020 17:00
Decorator - Only execute the method if any argument is not None
def method_idle_on_none(old_method):
'''
Decorator: Only execute the method if any argument is not None
'''
def new_method(self, *args):
for arg in args:
if arg is not None:
old_method(self, *args)
break
@anselmobd
anselmobd / teste
Created May 13, 2020 19:48
A test notebook
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Data Science: Introdução a Ciência de Dados.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true
},
@anselmobd
anselmobd / update_dict.py
Last active January 28, 2020 18:52
Like dict.update, but takes care of going through several levels of dicts.
def update_dict(original, adding):
result = original.copy()
for key in adding.keys():
if adding[key] is None:
continue
if isinstance(adding[key], dict):
if key not in result or not isinstance(result[key], dict):
result[key] = adding[key].copy()
else:
result[key] = update_dict(result[key], adding[key])
@anselmobd
anselmobd / send_files_smb.py
Created December 4, 2018 16:05
Copy files to remote SMB share folder
import urllib.request
from smb.SMBHandler import SMBHandler
def send_file_smb(
filename_orig, full_path_dest, filename_dest='', full_path_orig=''):
'''
Copy a local file to an remote SMB share folder and file.
'''
if filename_dest == '':
@anselmobd
anselmobd / incdecmonth.py
Last active August 12, 2022 00:07
Increment and decrement month of date in python
import datetime
def inc_month(dt, months):
'''
"months" can be positive or negative
If day in new month can't be the same, it will be the the closest possible.
'''
newmonth = (((dt.month - 1) + months) % 12) + 1
newyear = dt.year + (((dt.month - 1) + months) // 12)
@anselmobd
anselmobd / locksleep.sh
Last active August 12, 2022 00:10
Avoid multiple instances of the script - Sample script [Linux] [Bash]
#!/usr/bin/env bash
# Avoid multiple instances of the script
# Sample script
# Based on description of
# man 1 flock
# Your code go inside of "main" function and other fuctions
function main {