Skip to content

Instantly share code, notes, and snippets.

@Susensio
Susensio / pandas_groupby_mostfrequent.py
Created December 19, 2022 18:13
Pandas groupby and get most_frequent
# Default way of handling groupby and mostfrequent is slow
df.groupby(groupby_column)[null_column].agg(lambda x: x.iat[0])
# if there are nan's in df:
df.groupby(groupby_column)[null_column].agg(lambda x: x.iat[0] if not x.isnull().all() else np.nan)
# faster way: use value_counts and keep first value
(df
.groupby(groupby_column)[null_column]
.value_counts(sort=True, dropna=False)
@Susensio
Susensio / bash_xdg_compliant.sh
Last active February 22, 2023 08:17
Make bash follow XDG Base Directory specs. Must be run as root, it edits /etc files
#!/usr/bin/env bash
# Move ~/.bash* files to $XDG_CONFIG_HOME/bash/bash*
set -o nounset
set -o errexit
set -o pipefail
### ROOT SPACE
etc_bashrc_file="/etc/bash.bashrc"
etc_bashrcd_folder="/etc/bash/bashrc.d"
@Susensio
Susensio / numpy_lru_cache.md
Last active October 26, 2023 16:10
Make function of numpy array cacheable

How to cache slow functions with numpy.array as function parameter on Python

TL;DR

from numpy_lru_cache_decorator import np_cache

@np_cache()
def function(array):
 ...
@Susensio
Susensio / sd.fish
Created October 17, 2023 07:22 — forked from arcuru/sd.fish
Fish shell completions for ianthehenry/sd
# Completions for the custom Script Directory (sd) script
# MODIFIED from https://gist.github.com/patricksjackson/5065e4a9d8e825dafc7824112f17a5e6
# Improvements:
# - compliant with SD_ROOT
# - better handling of empty folders
# These are based on the contents of the Script Directory, so we're reading info from the files.
# The description is taken either from the first line of the file $cmd.help,
# or the first non-shebang comment in the $cmd file.
@Susensio
Susensio / profiling_qcachegrind.sh
Last active February 10, 2024 07:53
Python profiling with QCacheGrind
# Profile with cProfile python module and save to output
python -m cProfile -o output.cprof script.py
# Transform into tree format for QCacheGrind
pyprof2calltree -i output.cprof -o callgrind.cprof
# Open QCacheGrind and load callgrind.cprof file
@Susensio
Susensio / cached_method.md
Last active April 11, 2024 08:59
Instance cached_method for unhashable self, type hinted, with optional parameters

Cached Method Decorator in Python

This repository provides a Python decorator @cached_method that caches method results similar to functools.lru_cache, but with each instance having its own cache.

Overview

The cached_method decorator allows you to cache the results of an instance method call so that subsequent calls with the same arguments will return the cached result instead of performing the computation again.

This decorator provides two key benefits:

  • The cache is cleared when the instance is garbage collected (by using weak references).
@Susensio
Susensio / property_inheritance.md
Last active April 19, 2024 00:42
Inherit property setter in python 3.7

Python @property inheritance the right way

Given a Parent class with value property, Child can inherit and overload the property while accessing Parent property getter and setter.

Although we could just reimplement the Child.value property logic completely without using Parent.value whatsover, this would violate the DRY principle and, more important, it wouldn't allow for proper multiple inheritance (as show in the example property_inheritance.py bellow).

Two options:

  • Child redefines value property completely, both getter and setter.