Created
May 21, 2017 01:55
-
-
Save hernamesbarbara/4648950b4234f095c7ab08be188cad11 to your computer and use it in GitHub Desktop.
Custom thefuck rule for Jupyter commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""jupyter.py - Custom `nvbn/thefuck` rule for fixing typos in Jupyter commands | |
Rule should go here: ~/.config/thefuck/rules/jupyter.py | |
More on github: https://github.com/nvbn/thefuck | |
Example: | |
$ jupyter notebok | |
jupyter: 'notebok' is not a Jupyter command | |
1 hernamesbarbara at localhost in ~/ | |
$ fuck | |
jupyter notebook [enter/↑/↓/ctrl+c] | |
""" | |
import sys | |
import os | |
import re | |
from thefuck.utils import get_closest, replace_argument, for_app | |
JUPYTER_SUBCOMMANDS = ('console', 'notebook', 'qtconsole', 'nbconvert', | |
'kernelspec', 'migrate', 'nbextension', | |
'serverextension','troubleshoot','trust') | |
def _rg_err_subcmd(err_msg): | |
try: | |
return re.search(r"\'(.*?)\'", err_msg).group(1) | |
except: | |
return None | |
def _get_closest_subcommand(invalid_subcmd): | |
return get_closest(invalid_subcmd, JUPYTER_SUBCOMMANDS, | |
n=3, cutoff=0.6, fallback_to_first=True) | |
def _is_missing_subcmd(command): | |
return command.script == "jupyter" | |
def _is_invalid_subcmd(command): | |
err_msg = command.stderr.strip().lower() | |
return (err_msg.startswith("jupyter:") | |
and err_msg.endswith("is not a jupyter command")) | |
@for_app('jupyter') | |
def match(command): | |
return _is_missing_subcmd(command) or _is_invalid_subcmd(command) | |
def get_new_command(command): | |
if _is_missing_subcmd(command): | |
return "jupyter console" | |
else: | |
invalid_subcmd = _rg_err_subcmd(command.stderr) | |
correct_subcmd = _get_closest_subcommand(invalid_subcmd) | |
return replace_argument(command.script, invalid_subcmd, correct_subcmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment