Last active
September 15, 2020 20:26
-
-
Save alchzh/85ab9e3d340f44718b35946b7632dfe6 to your computer and use it in GitHub Desktop.
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
import ipywidgets as widgets | |
from IPython.display import display | |
from IPython.core import magic_arguments | |
from IPython.core.magic import cell_magic, magics_class, Magics | |
from inspect import signature | |
def test_golf(f, default_input="", arg_count=1, separator=None, by_line=False, converter=str): | |
stylesheet = widgets.HTML(value="<style>.golf-input { font-family: monospace; height: 200px; }</style>") | |
input_area = widgets.Textarea(value=default_input) | |
input_area.add_class("golf-input") | |
button = widgets.Button(description='Test') | |
output = widgets.Output() | |
def run_line(line): | |
args = line.strip().split(separator, arg_count-1) | |
print(f(*(converter(arg) for arg in args))) | |
def run(b): | |
output.clear_output() | |
with output: | |
input = input_area.value | |
if by_line: | |
for line in input.split("\n"): | |
run_line(line) | |
else: | |
run_line(input) | |
button.on_click(run) | |
box = widgets.HBox([widgets.VBox([stylesheet, input_area]), widgets.VBox([button, output])]) | |
display(box) | |
run(button) | |
@magics_class | |
class GolfMagics(Magics): | |
@magic_arguments.magic_arguments() | |
@magic_arguments.argument( | |
'function', metavar='function', type=str, | |
help='the name of the golf function' | |
) | |
@magic_arguments.argument( | |
'-s', '--separator', default="None", | |
help="""Separator for function parameters.""" | |
) | |
@magic_arguments.argument( | |
'-w', '--whole', action="store_false", | |
help="""Whether to process whole input at once.""" | |
) | |
@magic_arguments.argument( | |
'-t', '--type', default="str", | |
help="""What type to convert parameters to.""" | |
) | |
@cell_magic | |
def golf(self, line="", cell=None): | |
idx = cell.find("# --INPUT--\n") | |
if idx == -1: | |
code = cell | |
default_input = "" | |
else: | |
code = cell[:idx] | |
default_input = "\n".join(line.lstrip("# ") for line in cell[idx+12:].strip().split("\n")) | |
self.shell.ex(code) | |
args = magic_arguments.parse_argstring(self.golf, line) | |
f = self.shell.user_ns[args.function] | |
display(widgets.HTML(value=f"<h2><code>{args.function}</code>: <b>{len(code.strip())}</b> characters</h2>")) | |
test_golf( | |
f, | |
default_input=default_input, | |
separator=self.shell.ev(args.separator), | |
by_line=args.whole, | |
converter=self.shell.ev(args.type), | |
arg_count=len(signature(f).parameters) | |
) | |
ip = get_ipython() | |
ip.register_magics(GolfMagics) |
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
python-3.8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment