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 -m timeit -s "from permission_vs_forgiveness import test_lbyl" "test_lbyl()" | |
2000000 loops, best of 5: 155 nsec per loop | |
$ python -m timeit -s "from permission_vs_forgiveness import test_aff" "test_aff()" | |
2000000 loops, best of 5: 118 nsec per loop |
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
# permission_vs_forgiveness.py | |
class BaseClass: | |
hello = "world" | |
class Foo(BaseClass): | |
pass | |
FOO = Foo() |
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
try: | |
with open("path/to/file.txt", "r") as input_file: | |
return input_file.read() | |
except IOError: | |
# Handle the error or just ignore it |
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
with open("path/to/file.txt") as input_file: | |
return input_file.read() |
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 os | |
if os.access("path/to/file.txt", os.R_OK): | |
... |
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 os | |
if os.path.exists("path/to/file.txt"): | |
... | |
# Or from Python 3.4 | |
from pathlib import Path | |
if Path("/path/to/file").exists(): | |
... |
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
from IPython.core.magic import register_line_magic | |
@register_line_magic | |
def rerunplus(line): | |
get_ipython().run_line_magic('load_ext', 'ignore_exceptions') | |
get_ipython().run_line_magic('rerun', line) | |
get_ipython().run_line_magic('unload_ext', 'ignore_exceptions') | |
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
def unload_ipython_extension(ipython): | |
ipython.input_transformers_post = [ | |
f for f in ipython.input_transformers_post if f.__name__ != 'clean_input' | |
] |
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
def clean_input(lines): | |
new_lines = [] | |
+ lines = combine_multiline(lines) |
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
def combine_multiline(lines): | |
new_lines = [] | |
for line in lines: | |
if line.startswith(' ') and new_lines: | |
new_lines[-1] += line | |
else: | |
new_lines.append(line) | |
return new_lines |