** Note: This article is a niche in its own right **
I was dying for a feature that wasn't in the python package I was using. Sometimes researchers have to prepare things that others don't use. However, that feature is a niche and cannot be perceived as a [^ douki] pull request. So I made a patch.
As you know, UNIX-like has a command called patch. Use this. It's okay with git, but ** I wanted to write in the paper "I haven't made any strange changes. The difference is only here." (Important). ** **
You can output the changes you made by doing git diff. So use git just to create patches. It is also good to be able to rewind history. So, after all, I use git.
First, I had to jump to the source code of the package I was using to edit it. No matter where you are in the virtual environment, you can go with one liner. I rewrote the following hoge and flew away.
cd $(python -c "import hoge as target; from pathlib import Path; print(Path(target.__file__).parent)")
So, before editing, manage git for the time being.
git init
git add *
git commit
git branch patch
git checkout patch
vim hoge.py
Well, git add * may even include the cache, so that's appropriate. I'm vimmer, so I edit with vim, but of course it doesn't have to be vim. Diff when you're done making changes.
mkdir ~/warui_patch
git diff > ~/warui_patch/complex.patch
This completes patch creation.
Below is a shell script for applying the patch to the package. Below, the complex.patch created earlier is applied to the mne package. I have some options.
--- R: Revert the patch --- p: Display the path of the corresponding package --- h: Help
The path of this package is displayed when I want to edit something
cd $(sh ./patcher -p)
As an option, it allows you to jump under the package. Of course, even in a virtual environment, it will fly with the will of iron.
#!/usr/bin/env sh
OPT=$1
PACKAGE='mne'
PATCH='complex.patch'
make_python_path_code () {
echo "import $PACKAGE; from pathlib import Path; print(Path($1.__file__).parent)"
}
make_python_path () {
echo "$(python -c "$(make_python_path_code $1)")/"
}
patch_python () {
patch $OPT -u -p 1 -d $(make_python_path $1) < $2
}
if [ "$1" = "-h" ]; then
echo Patch script for python.
echo ========================
echo -h: Show this message
echo -R: Reverse patch
echo -p: Print script dir
exit
fi
if [ "$1" = "-p" ]; then
echo $(make_python_path $PACKAGE)
exit
fi
patch_python $PACKAGE $PATCH
I don't really want to rewrite the package, even for research purposes. Spicy ... (´ ・ ω ・ `)
[^ douki]: I wanted to Wavelet transform the brain waves to calculate the average of the absolute values from the complex numbers and estimate the size of the brain waves in the brain, but I wanted the complex numbers instead of the average of the absolute values. It is. The calculation cost becomes dangerous because it is not averaged.
Recommended Posts