Skip to content

Instantly share code, notes, and snippets.

@polynomialherder
polynomialherder / integral_twonorm_comparisons.py
Last active March 21, 2024 16:56
This Python script compares Chebyshev polynomials against polynomials pp = p / ||p||_2
import hashlib
import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate
from chebyshev import ChebyshevPolynomial
def pid(p):
@polynomialherder
polynomialherder / Dockerfile
Last active January 24, 2024 19:21
Dockerfile for running SQL Server with SQL Agent enabled
# Usage:
# Ensure Docker daemon is running, then run from the same directory as this Dockerfile:
#
# docker build -t my-mssql-server .
# docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Yourstr0ngp@ssword' -p 1433:1433 --name sql1 -d my-mssql-server
#
# This builds the image. Then execute the following SQL on the server in SSMS to enable SQL Agent:
#
# EXEC sp_configure 'show advanced options', 1;
# EXEC sp_configure 'Agent XPs', 1;
from scipy.linalg import norm
from chebyshev import ChebyshevPolynomial
import numpy as np
from itertools import combinations, combinations_with_replacement
import matplotlib.pyplot as plt
@polynomialherder
polynomialherder / sql_to_databricks.py
Last active January 24, 2024 19:14
Python script that extracts inlined SQL from a C# script and converts it to the Databricks notebook format
"""
Zero dependency script that extracts inlined SQL from a C# module and translates it to a Databricks formatted notebook
Suppose that you have a 2500 line C# module containing lots of methods of the form
public DataTable GetSomething(...) {
...
var sql = @"<sql query>"
...
}
@polynomialherder
polynomialherder / lexer.py
Last active January 24, 2024 19:15
A toy lexer for an arithmetic language
import pprint
from enum import Enum, auto
class TokenType(Enum):
COMMENT = auto()
SEMICOLON = auto()
DATATYPE = auto()
STRING = auto()
@polynomialherder
polynomialherder / stack_machine.py
Last active June 16, 2023 01:58
A toy stack machine implementation in Python to illustrate match statements
from enum import Enum, auto
from collections import namedtuple
class Operation(Enum):
PUSH = auto()
STORE = auto()
LOAD = auto()
@polynomialherder
polynomialherder / alternate_with.py
Created May 10, 2023 00:42
A function that, given a fixed (constant) alternating value and an iterable, constructs a generator that alternately yields the next generator value, or the constant alternating value
import itertools
def exhausted(generator):
try:
next(generator)
return False
except StopIteration:
return True
@polynomialherder
polynomialherder / remez.py
Created April 25, 2023 21:27
An implementation of the Remez exchange algorithm
import numpy as np
from scipy.linalg import norm, solve
from itertools import pairwise
golden_ratio = (np.sqrt(5) + 1) / 2
def gss(f, a, b, tol=1e-10):

Computer programs consist of instructions that either

  1. Compute a value
  2. Perform some action

Instructions that compute values are called expressions. Expressions always return a value. For example, 2 + 3, add(50, 100), "Nestor " + "is a cat", 1, "hello" and 3.141592653589793 are all expressions (why?).

Instructions that perform actions are called statements. The actions they perform are called "side effects". This nearly always implies some kind interaction with the world outside the program: printing stuff to a terminal screen, fetching data over the internet, or creating and deleting files. Often, but not always, statements return None (different from the string "None"). Paradoxically, None is a value that signifies the quality of not having a value!

@polynomialherder
polynomialherder / load_csv.py
Last active October 19, 2020 01:40
A short demonstration of how to performantly load a delimited datafile into a SQLite database
# Using SQLite's CSV mode from Python for extremely fast bulk data loads
#
# Note that if the target table does not exist, SQLite will create it automatically,
# creating column names from the first row in the CSV assuming TEXT datatypes.
#
# If the target table does exist, SQLite will treat the first row in the CSV file
# as *data*. So if the CSV file contains a header, you should either delete it from the
# source file, or, if using SQLite > 3.32.0, pass the `--skip 1` flag to the `.import`
# command (`import data.csv Data --skip 1`)
#