A record of patching a python package

I'm a bad boy

** 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.

Own patch? What's wrong with git?

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). ** **

Creation procedure

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.

Source code

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

Impressions

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

A record of patching a python package
A well-prepared record of data analysis in Python
A good description of Python decorators
[Python] A memorandum of beautiful soup4
A brief summary of Python collections
A record of hell lessons imposed on beginner Python students
Introduction of python drawing package pygal
Record of Python introduction for newcomers
A memorandum of understanding for the Python package management tool ez_setup
A story that struggled to handle the Python package of PocketSphinx
Display a list of alphabets in Python 3
Make a relation diagram of Python module
Connect a lot of Python or and and
[python] Get a list of instance variables
[python] [meta] Is the type of python a type?
Create a deb file from a python package
The story of blackjack A processing (python)
[Python] Get a list of folders only
A memorandum of python string deletion process
Introduction of Python
Basics of Python ①
Basics of python ①
Copy of python
Introduction of Python
A memo of a tutorial on running python on heroku
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Draw a graph of a quadratic function in Python
[python] Create a list of various character types
Get the caller of a function in Python
A memorandum of calling Python from Common Lisp
Make a copy of the list in Python
A memorandum of extraction by python bs4 request
Solve A ~ D of yuki coder 247 with python
Rewriting elements in a loop of lists (Python)
A note about the python version of python virtualenv
Make a joyplot-like plot of R in python
[Python] A rough understanding of the logging module
Output in the form of a python array
Get a glimpse of machine learning in Python
Create a package containing global commands in Python
[Python] A rough understanding of iterators, iterators, and generators
A discussion of the strengths and weaknesses of Python
Around the authentication of PyDrive2, a package that operates Google Drive with Python
I made a script to record the active window using win32gui of Python
[Python] Implementation of clustering using a mixed Gaussian model
[Python] Operation of enumerate
List of python modules
A * algorithm (Python edition)
[Python] A program that counts the number of valleys
A memo connected to HiveServer2 of EMR with python
[Python] Take a screenshot
(Python) Treat integer values as a set of flags
Recommendation of building a portable Python environment with conda
Create a Python module
Unification of Python environment
Copy of python preferences
Make a copy of a Google Drive file from Python
Cut a part of the string using a Python slice
A python lambda expression ...
Basics of Python scraping basics
A collection of code often used in personal Python