Created
May 16, 2020 16:21
-
-
Save leontrolski/3c966bce4f3cdb42aeb16f9e0d97fd17 to your computer and use it in GitHub Desktop.
Finn decorators
This file contains hidden or 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
| # let's imagine you didn't care about | |
| # the `output_filename` or the `extensions` | |
| def decorator(f): | |
| def inner(*args, **kwargs): | |
| f(*args, **kwargs) | |
| save(plt) | |
| return inner | |
| def plot_something(foo: int, bar: int) -> None: | |
| ... | |
| plot_something = decorator(plot_something) | |
| # the sugar-y equivalent to this bit: | |
| def plot_something(foo: int, bar: int) -> None: | |
| ... | |
| plot_something = decorator(plot_something) | |
| # is this: | |
| @decorator | |
| def plot_something(foo: int, bar: int) -> None: | |
| ... | |
| # now let's add in the `output_filename` | |
| # and the `extensions` | |
| def plot_saver(filename, extensions): | |
| def decorator(f): | |
| def inner(*args, **kwargs): | |
| f(*args, **kwargs) | |
| for extension in extensions: | |
| save(plt, filename, extension) | |
| return inner | |
| @plot_saver("my-special-filename", [".png", ".jpeg"]) | |
| def plot_something(foo: int, bar: int) -> None: | |
| ... | |
| # As an aside, I'd be a bit cautious of this global | |
| # `plt` object (although I can't remember exactly how | |
| # matplotlib or whatever work) | |
| # It would probs be be better if you can do similar to: | |
| def plot_saver(filename, extensions): | |
| def decorator(f): | |
| def inner(*args, **kwargs): | |
| plt = f(*args, **kwargs) | |
| for extension in extensions: | |
| save(plt, filename, extension) | |
| return inner | |
| @plot_saver("my-special-filename", [".png", ".jpeg"]) | |
| def plot_something(foo: int, bar: int) -> matplotlib.Plot: | |
| ... | |
| plt = blahblahblah(...) | |
| return plt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment