Skip to content

Instantly share code, notes, and snippets.

View switowski's full-sized avatar
🐍
I'm Pythoning

Sebastian Witowski switowski

🐍
I'm Pythoning
View GitHub Profile
$ 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
# permission_vs_forgiveness.py
class BaseClass:
hello = "world"
class Foo(BaseClass):
pass
FOO = Foo()
try:
with open("path/to/file.txt", "r") as input_file:
return input_file.read()
except IOError:
# Handle the error or just ignore it
with open("path/to/file.txt") as input_file:
return input_file.read()
import os
if os.access("path/to/file.txt", os.R_OK):
...
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():
...
@switowski
switowski / gist.py
Created July 16, 2020 17:39
rerunplus.py
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')
@switowski
switowski / gist.py
Created July 16, 2020 17:39
ignore_exceptions8.py
def unload_ipython_extension(ipython):
ipython.input_transformers_post = [
f for f in ipython.input_transformers_post if f.__name__ != 'clean_input'
]
@switowski
switowski / gist.diff
Created July 16, 2020 17:38
changes3.diff
def clean_input(lines):
new_lines = []
+ lines = combine_multiline(lines)
@switowski
switowski / gist.py
Created July 16, 2020 17:38
ignore_exceptions7.py
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