Skip to content

Instantly share code, notes, and snippets.

View sfaleron's full-sized avatar
:electron:
Figuring it out as I go

Chris Fuller sfaleron

:electron:
Figuring it out as I go
View GitHub Profile
@sfaleron
sfaleron / immutarray.py
Last active December 25, 2023 18:09
Immutable/hashable numpy Array
# ISC License (ISC)
#
# Copyright 2021 Christopher Fuller
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
# provided that the above copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
@sfaleron
sfaleron / mathoscraper.py
Last active November 7, 2018 15:36
Scrapes the commands from a Mathomatic session
# Scrape a Mathomatic session for the commands. Does not catch special
# prompts, such as "Enter <identifier>: " after a calc command.
# Mathomatic: lightweight command-line Computer Algebra System
# http://mathomatic.orgserve.de/math/
from __future__ import print_function
import sys
import re
@sfaleron
sfaleron / newbinds.py
Last active July 30, 2019 13:56
Class factory helper; set the attribute dictionary from locals
class NewBinds(object):
"""Copy a dictionary, while retaining only updates.
The intended purpose of this class is to simplify the code
creating attribute dictionaries for class factories, but it
would also work anywhere else a lot of bindings which include
function definitions are bundled into a dictionary.
Example usage: the locals dictionary is passed at instantiation,
additional bindings are made, and then the instance is called with
@sfaleron
sfaleron / pyinsts.py
Last active July 25, 2018 20:56
Queries Windows registry for Python installations, as specified in PEP514. The bitness is determined by invoking it, since only v3.5 and later include that in the registry.
# PEP514 defines these registry entries
# https://www.python.org/dev/peps/pep-0514/
from __future__ import print_function
# https://gist.github.com/sfaleron/6d31cfe2a7188b6bcea5ca67346254a1
import pybits
import sys
@sfaleron
sfaleron / pybits.py
Last active July 27, 2018 15:45
Return the "bitness" of the Python interpreter. Generally 32 or 64, but this is not presumed.
"""Presumes that the bitness is a power of two. There have been exceptions,
but they are hopefully securely sealed up in the dustbin of history. I
am not aware of any port of Python to such a platform.
Additionally, systems with fewer than eight bits are not handled correctly.
If you find yourself in such a situation, you are presumed to know better
than to rely on this module."""
def pyBits():
@sfaleron
sfaleron / lazyprimes_py3.py
Last active July 30, 2019 13:59
Infinite prime number generator for Python3
# Originally from http://logn.org/2009/07/lazy-primes-sieve-in-python.html
# Updated for Python3 by Chris Fuller.
# The automated 2to3 translation doesn't work. The tricky bit is the heap item.
# This is a (int, object) tuple in the original module, but the second element
# becomes a map iterator in Python3, which does not compare, so an exception
# is raised when the heap is sorted.
# It turns out that the object in the original tuple is irrelevant to the sort
# (it compares by memory location, which isn't well defined!). The int deter-
@sfaleron
sfaleron / queryver.py
Last active November 2, 2017 21:35
Check the current version of maven artifacts at jcenter
# Query the current version of maven artifacts at jcenter
# Current favorites:
#
# org=org.jetbrains.kotlin pkg=kotlin-runtime
# org=io.kotlintest pkg=kotlintest
# org=no.tornado pkg=tornadofx
import requests
@sfaleron
sfaleron / classy.js
Created February 27, 2017 10:46
Making JS classier. Until moving on to something with classiness baked-in that compiles to JS :)
// Requires ES5
// Inheritance pattern inspired by http://stackoverflow.com/a/5546053
// instanceOf has the usual straightforward-inheritance-checker problems with iFrames
if (typeof classyJS === 'undefined') {
var classyJS = (function () {
'use strict';
@sfaleron
sfaleron / ln2.py
Created September 24, 2015 23:44
Quick ln2 function for Python
from math import ceil
# for example, inc=4 gives ln16, or the number of hexadecimal digits
# required to represent n.
def ln2(n, inc=1):
if n<0:
raise ValueError('math domain error')
i = 0
n = int(ceil(n))
@sfaleron
sfaleron / prettybin.py
Created September 24, 2015 23:40
Python function to create a hexdump inspired by old DOS DEBUG command
_fmtinner = ('',' ') * 3 + ('','-') + ('',' ') * 3 + ('','')
_fmtouter = (' ', ' |', '|')
def _mkhex(d, i):
try:
return '%02x' % (ord(d[i]),)
except IndexError:
return ' '
def _mkchar(d, i):