Skip to content

Instantly share code, notes, and snippets.

View toby-p's full-sized avatar

toby petty toby-p

  • New York
View GitHub Profile
@toby-p
toby-p / pandas_sklearn_polynomials.py
Created December 2, 2020 18:49
Apply scikit-learn's PolynomialFeatures class to a Pandas.DataFrame, keeping original index/column labels.
import pandas as pd
from sklearn.preprocessing import PolynomialFeatures
def apply_polynomials(df: pd.DataFrame, degree: int = 2,
interaction_only: bool = False,
include_bias: bool = False):
"""Apply scikit-learn's PolynomialFeatures class to a pandas DataFrame,
keeping the original column labels and index, and extending the columns to
include all new polynomial features. Generally speaking creates a lot of new
@toby-p
toby-p / input_paginated_iterable.py
Created October 27, 2020 20:16
Get user choice from an enumerated iterable, with pagination of options.
def input_iterable_paginated(msg: str, iterable: object, page_length: int = 5,
more: str = "m"):
"""List numbered items from any iterable to be chosen by user input."""
def get_input(valid: list):
value = input().strip().lower()
try:
value = int(value)
if value - 1 in range(len(valid)):
return valid[value - 1]
else:
@toby-p
toby-p / print_check.py
Last active October 29, 2020 19:01
Decorator to print custom messages when executing functions, followed by a check mark when it completes.
# Class:
class PrintCheck:
"""Decorator to print a custom message when calling a function, followed by
a check/tick mark on the same line when the computation finishes. If the
function returns an integer it will be printed in parentheses with the check
mark along with the `item_name` argument, pluralized if greater than 1."""
def __init__(self, msg: str = None, print_items: bool = True,
item_name: str = "item"):
"""
@toby-p
toby-p / runtime_performance.py
Created August 17, 2020 19:14
decorator to store runtime performance of a function in a CSV file.
import datetime
import pandas as pd
import pytz
import time
# To switch logging on/off add config logic here to determine this variable:
LOG_PERFORMANCE = True
TIMEZONE = "US/Eastern"
import traceback
import warnings
import sys
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
log = file if hasattr(file,'write') else sys.stderr
traceback.print_stack(file=log)
log.write(warnings.formatwarning(message, category, filename, lineno, line))
@toby-p
toby-p / search_strings.py
Created September 6, 2019 18:19
Search for strings in iterables and Pandas DataFrames
def str_search(*substrings: str, iterable, exact_match: bool = False) -> list:
"""Case-insensitive search of an iterable for substrings.
Args:
substrings (str): strings to search for in the iterable.
iterable (list, tuple, etc): iterable containing string objects to be
searched.
exact_match (bool): if True only return a single value that exactly
matches the substring supplied (therefore only works if 1 substring
arg is supplied). Otherwise returns list of all partial matches.
@toby-p
toby-p / memory-usage.py
Created June 6, 2019 16:27
Function to track current memory usage.
import os, psutil
# Tool for tracking memory usage:
def usage():
process = psutil.Process(os.getpid())
print(process.memory_info()[0] / float(2 ** 20))
import math
def magnitude(x):
"""Get the order 10 magnitude of a float."""
return int(math.log10(x))
def mpl_y_scale(datamin, datamax, desired_n_steps=5, minmax_buffer=True):
"""Determine nicely spaced, nearest-decimal-rounded y-ticks, and y-axis
limits (with optional buffer) which centrally locate a line plot in a
Matplotlib figure axis.
@toby-p
toby-p / suppress_errors.py
Created December 18, 2018 19:44
Python decorator to suppress errors.
# Global variable to determine whether or not to raise errors.
SUPPRESS_ERRORS = True
def suppress_errors(func):
"""Decorator function to suppress errors."""
if not SUPPRESS_ERRORS:
return func
def suppressed(*args, **kwargs):
try:
return func(*args, **kwargs)
@toby-p
toby-p / debug_print.py
Created October 29, 2018 15:30
Simple debug print statements
from datetime import datetime
import pytz
def debug_print(msg, timezone="US/Eastern"):
"""Print a time-stamped debug message."""
UTC_now = pytz.utc.localize(datetime.utcnow())
now = UTC_now.astimezone(pytz.timezone(timezone))
now_date = now.strftime("%Y_%m_%d")
now_time = now.strftime("%H:%M:%S")
print("[{} @ {}] : {}".format(now_date, now_time, msg))