Skip to content

Instantly share code, notes, and snippets.

@salt-die
Last active June 1, 2020 02:04
Show Gist options
  • Save salt-die/dd49e4e8dd65bf144d672843bd0b4513 to your computer and use it in GitHub Desktop.
Save salt-die/dd49e4e8dd65bf144d672843bd0b4513 to your computer and use it in GitHub Desktop.
my ipython startup file!
#utf-8
######################
# Line Transformers: #
######################
# ------------------------------------------
# --- `λxy.` shortcut for `lambda x, y:` ---
# ------------------------------------------
import re
__λ__ = re.compile(r'λ[a-z]+[.]')
def __λ_sub__(m):
m = m.group()
return f'lambda {", ".join(m[1:-1])}:'
def _transform_λ(lines):
return [__λ__.sub(__λ_sub__, line) for line in lines]
get_ipython().input_transformers_cleanup.append(_transform_λ)
###########
# Magics: #
###########
from IPython.core.magic import register_line_magic
# --------------------------------------------
# --- %a for fast creation of numpy arrays ---
# --------------------------------------------
import numpy as np
@register_line_magic
def a(line):
def listify(row, type_=int):
return list(map(type_, row.split()))
if ',' in line:
lines = line.split(',')
try:
return np.array([listify(row) for row in lines])
except ValueError:
return np.array([listify(row, float) for row in lines])
try:
return np.array(listify(line))
except ValueError:
return np.array(listify(line, float))
del a
# -----------------------------------------
# ------------- Truth Tables --------------
# -------- %tt expr1, expr2, ... ----------
# -----------------------------------------
from tiny_math.truthtables.truthtable_trees_sly import TruthTable
@register_line_magic
def tt(line):
TruthTable(*line.split(',')).display()
del tt
# -----------------------------------------
# -------- Load Github User and API -------
# --------- %gist file description --------
# -----------------------------------------
from github import Github, InputFileContent
github = Github('github user token')
salt = github.get_user()
@register_line_magic
def gist(line):
file, description = line.split(' ', 1)
with open(file, 'r') as f:
contents = f.read()
salt.create_gist(True, {file: InputFileContent(contents)}, description)
del gist
# -----------------------------------------
# ------------- Quick Pydocs --------------
# ------------- %docs object --------------
# -----------------------------------------
import webbrowser
@register_line_magic
def docs(line):
webbrowser.open(f'https://docs.python.org/3/search.html?q={line}')
del docs
#############
# Preloads: #
#############
# -----------------------------------------
# ---------- overloaded reload ------------
# ----------- reload(object) --------------
# -----------------------------------------
import importlib
def reload(obj):
if hasattr(obj, '__spec__'): return importlib.reload(obj)
try:
module = importlib.reload(globals()[obj.__module__])
except KeyError:
module = importlib.import_module(obj.__module__)
globals()[obj.__name__] = getattr(module, obj.__name__)
# -----------------------------------------
# ---------- Preload tablemaker -----------
# -----------------------------------------
from table_maker.table_maker import table_maker as tm
# -----------------------------------------
# ------ Preload ast pretty printer -------
# -----------------------------------------
from Snippets.prettiest_ast import pp as past
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment