Skip to content

Instantly share code, notes, and snippets.

@JulianWgs
JulianWgs / black_for_author.py
Created May 17, 2021 21:20
Run Python black only for specific git author email address
import git
import black
from blib2to3.pgen2.tokenize import TokenError
new_lines = list()
author_email = "example@example.org"
filename = "setup.py"
for commit, lines in repo.blame('HEAD', filename):
if author_email == commit.author.email:
try:
@JulianWgs
JulianWgs / apply_to_key.py
Created April 22, 2020 10:39
Apply function to specific keys in python list (to for example censor text)
import re
import copy
def apply_to_key(iterable, keys, func):
iterable = copy.deepcopy(iterable)
if isinstance(iterable, list):
for item in iterable:
iterable = clean(item, keys, func)
elif isinstance(iterable, dict):
for key, item in iterable.items():
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JulianWgs
JulianWgs / convert_table_to_df.py
Created March 28, 2020 10:23
Converting pip list --outdated to pandas dataframe
# This is also applicable to other table which are styled similar.
import pandas as pd
with open("packagelist.txt") as file:
input_str = file.read()
column_widths = [len(dashes) for dashes in input_str.split("\n")[1].split()]
column_names = input_str.split("\n")[0].split()
data = {key: list() for key in column_names}
for line in input_str.split("\n")[2:]:
@JulianWgs
JulianWgs / hour_index.py
Created March 10, 2020 10:28
Create index on hour range in pandas (date_range)
# index are integers
# when the index is already a time delete unit arg
pd.date_range(
start=pd.to_datetime(df.columns.min(), unit="h"),
end=pd.to_datetime(df.columns.max(), unit="h"),
freq="1H"
).time
@JulianWgs
JulianWgs / logarithmic_rounding.py
Created February 7, 2020 08:32
Logarithmic rounding
a = 20.3
10**np.round(np.log10(a))