from numpy_lru_cache_decorator import np_cache
@np_cache()
def function(array):
...
# 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) |
#!/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" |
# 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. |
# 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 |
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.
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).
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
redefinesvalue
property completely, both getter and setter.