Skip to content

Instantly share code, notes, and snippets.

@juanbretti
juanbretti / image_compress.py
Last active July 9, 2023 19:04
Find all .png files, convert them to .jpg, rename to .png
# %%
import os
from PIL import Image
import argparse
def replace_path(file_path_png, file_path_jpg, directory, full_check=True):
"""Replace the file path of the original .png to the new .jpg file inside all the .md files. If no "directory" is defined, it will replace at the current folder level.
Args:
file_path_png (str): Full path to the .png file.
@juanbretti
juanbretti / onenote_to_obsidian.py
Last active September 5, 2023 19:34
Reformat .md to better suit Obsidian formatting
# %%
import os
from datetime import datetime
import csv
import pandas as pd
from pathlib import Path
# Inputs
directory = 'C:\OneNote'
name_length = 15
@juanbretti
juanbretti / convert.py
Last active March 14, 2023 12:00
Convert TimeOtp from KeePass to Bitwarden or Vaultwarden
import json
data = json.load(open("bitwarden_export_20221019000142.json", encoding='utf-8'))
totp_keepass = "TimeOtp-Secret-Base32"
# data['items'][2]['fields'][0]['name'] == totp_keepass
i = 0
for index1, value1 in enumerate(data['items']):
if 'fields' in value1:
# print('value1', value1, "\n"*5)
@juanbretti
juanbretti / clean_col.py
Created June 1, 2022 07:45
`clean_col` in Python after a LinkeIn post
# After https://www.linkedin.com/posts/pablo-rosa-2b1183aa_datascience-dataanalytics-python-activity-6937455012306386944-xdx1?utm_source=linkedin_share&utm_medium=member_desktop_web
import pandas as pd
df = pd.DataFrame(data={'First Row': [1,2,3], 'secnond row':[4,5,6], 'THIRD_ROW':[7,8,9]})
def clean_col(name):
return (
name.strip().lower().replace(" ", "_")
)
@juanbretti
juanbretti / try_until_it_works.py
Last active June 27, 2021 21:42
Try until it works
while True:
try:
# do stuff
except SomeSpecificException:
continue
break
@juanbretti
juanbretti / global_variable.py
Created June 15, 2021 18:51
Global variable in Python example
# %%
x = 0
print(x)
# %%
def no_global():
x = 2
return x
@juanbretti
juanbretti / filter_between_hours.py
Last active May 13, 2021 20:40
Filter time series between hours
import pandas as pd
import numpy as np
df = pd.read_excel(open('Example.xlsx', 'rb'), sheet_name='Data')
start_time = '20:15'
end_time = '9:15'
index = pd.DatetimeIndex(df['Date_Time'])
df = df.iloc[index.indexer_between_time(start_time, end_time)]