This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Point { | |
var x: Float64 | |
var y: Float64 | |
init(_ x: Float64, _ y: Float64) { | |
self.x = x | |
self.y = y | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import builtins | |
import os | |
import sys | |
from builtins import __import__ | |
def fix_path(p: str): | |
if not p.startswith('/'): | |
return p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import floor | |
from fractions import Fraction as F | |
def pi_approx(bits): | |
def term(k): | |
return F(1, 16 ** k) * (F(4, 8 * k + 1) - F(2, 8 * k + 4) - F(1, 8 * k + 5) - F(1, 8 * k + 6)) | |
return sum(term(k) for k in range(bits // 4 + 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import sys | |
def main(): | |
args = sys.argv | |
for i, x in enumerate(args): | |
print('[{}] = {!r}'.format(i, x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- Create a rule function which calculates the next value of a single cell in | |
-- a cellular automaton | |
-- @param rule | |
-- Rule number. | |
-- @param neighbours | |
-- Number of neighbours in each direction which affect the next state of a | |
-- cell. With 0, a cell only affects itself. With 1, the two immediate | |
-- neighbours also affect the cell. | |
function wolfram_ca_rule_fn(rule, neighbours) | |
return function(get_cell_fn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import lru_cache, wraps | |
from itertools import count | |
_method_lru_cache_sequence = count() | |
def method_lru_cache(*lru_args, **lru_kwargs): | |
""" | |
Like `functools.lru_cache` which can be used on methods of a class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
import fcntl | |
@contextmanager | |
def lock_file(path): | |
with open(path, 'wb') as file: | |
fcntl.flock(file, fcntl.LOCK_EX) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def partition_items(items, num_top_items, key=lambda x: x): | |
if items and 0 < num_top_items < len(items): | |
pivot = items[0] | |
smaller = [] | |
larger = [] | |
for i in items[1:]: | |
if key(i) < key(pivot): | |
smaller.append(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@contextlib.contextmanager | |
def temp_file_path(path): | |
""" | |
Context manager which returns slight variation of the specified path, which can be used to write a file to without clobbering an already existing file until the write is successful. The file at the returned path will either be moved onto the passed path (if the context is left normally) or deleted (if the context is left by throwing an exception). | |
""" | |
temp_path = path + '~' | |
dir_path = os.path.dirname(path) | |
if not os.path.exists(dir_path): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
import sys, os, argparse, subprocess, time | |
class UserError(Exception): | |
def __init__(self, msg, *args): | |
super().__init__(msg.format(*args)) | |
NewerOlder