Skip to content

Instantly share code, notes, and snippets.

View brunomsantiago's full-sized avatar

Bruno Santiago brunomsantiago

  • Brazil
View GitHub Profile
@brunomsantiago
brunomsantiago / on_vpn.py
Created February 16, 2022 16:03
Verify if connected to VPN based on local ip address
import socket
def my_ip():
hostname = socket.gethostname()
return socket.gethostbyname(hostname)
def on_vpn(ip: str, vpn_ip_begin='10.'):
return ip.startswith(vpn_ip_begin)
@brunomsantiago
brunomsantiago / app.py
Created February 18, 2022 18:29
Template for PyQT tray application with system notifications, status on both icon color and tootip
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtWidgets import QMainWindow, QMenu, QSystemTrayIcon
green_icon = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x002\x00\x00\x002\x08\x06\x00\x00\x00\x1e?\x88\xb1\x00\x00\x0c\xf3IDATx\x9c\xedY{pT\xd7y\xff}\xe7\xdc{\xb5+\xad\x00\xa1\x07z\xf0\x10\x18$\x12\x84\x93\x16\xb0\x0b*f\x150\xaf)Nl\x03\xf6\xe0d<\xe9\xb4\x9e\x16\xd3d\x92\x99$\x1d:\x8e\xc1u\x9d\xb1'\xe9\xb4um'\x19\xb7\x8e\xebW\x02\x8e\r\xc4\xd8 ;h1\x96\"\xc0\xb2\xb1\xa16P0\x8b\x91\x84@+\t\xc1J\xbb\xdc{\xcf\xfd\xfa\xc7\xee]\xf6qw\x853\xe9L\xd2\xc9oF\xa3\x9d\xbd\xe7~\xdf\xf7;\xdfs\xcf\xa1\xf6\xb7\xf7\xe0\xff\x03\xb4B\x0f\tD\xd7#\x84\xc1\xfc\xdb\x1a\xf0\xbb\xd2Q\x90HysC\xa0\x1c\x15\x05\x15\x0c \x82H\xfb\x89+\xd7c\xcc\xff\xa5\x8e\x82D68\x1bhT\x0ck\x86,\xca\xbb\x1b\x93\xa4\x8f\x1f\xe46\r\xa1\x90]\xd0\x9a<\xf8;c\x83\xbc\xa0\xe2y\xbd2\x0c\x80\xac\x11\xf5|\xf0\xc3\x82:<\x89\x84\x83a\xdf=7\xdf\xa3\xe3\xc5\xdf|W\xd4`\x99\x13'\x13`\x99\xb5L\x01\xa4\xd1e>\x83\x9a5\x
@brunomsantiago
brunomsantiago / shorten.py
Created May 6, 2022 14:28
Shortening strings in python (inspired by standard library textwrap.shorten)
def shorten_end(string, width=80, placeholder=' ...'):
if len(string) <= width:
return string
n2 = width - len(placeholder)
return string[:n2] + placeholder
def shorten_begin(string, width=80, placeholder='... '):
if len(string) <= width:
return string
n1 = len(string) - width + len(placeholder)
@brunomsantiago
brunomsantiago / are_strings_grouped.py
Last active February 9, 2023 02:13
Check if all items of a list of string are grouped. ['a', 'a', 'b'] are, ['a', 'b, 'a'] are not.
def are_strings_grouped(list_of_strings):
# First find the number of expected changes
n_unique_strings = len(set(list_of_strings))
n_expected_changes = n_unique_strings - 1
# Then find the real number of changes
groups_channges = [1 for former, latter
in zip(list_of_strings[:-1], list_of_strings[1:])
if former != latter]
n_real_changes = sum(groups_channges)
# Finally compare them
@brunomsantiago
brunomsantiago / sensitivity_label.py
Last active February 26, 2023 02:58
Python functions to read and apply sensitivity labels to microsoft documents using powershell
import json
import subprocess
import time
def read_label(
filepath,
full_result=False,
powershell=r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
stdout_encoding='iso8859-15',
@brunomsantiago
brunomsantiago / wait.py
Last active June 7, 2023 16:23
Python function to block execution until desired timestamp, with countdown
from time import sleep
from datetime import datetime, timedelta
def wait(until,
update_time=1,
waiting_message='Waiting...',
done_message='Done'):
print(waiting_message)
while (