Instantly share code, notes, and snippets.

# Jonas Byströmhighfestiva

• Sort options
Created Dec 15, 2019
Calculate PI with an incredibly low accuracy
View calc_pi.py
 #!/usr/bin/env python3 import sys terms = (int(sys.argv[1]) + 1) // 2 pi = 4 + sum(4/-(v*4+3) + 4/(v*4+5) for v in range(terms)) print(pi)
Created Nov 15, 2019
Optimized and simplified BitMEX's cross-margin calculation
View bitmex-cross-margin-liquidation-calc.py
 def long_liq_price(wallet_balance_btc, qty, price): usd = qty * int(-100000000 / price - 0.5) a = wallet_balance_btc * 99925056.20784412 + 0.011498875843117663 * usd a *= a>0 x = usd*1.004831 - a return -100000000 / int(x / qty) def short_liq_price(wallet_balance_btc, qty, price): usd = qty * int(-100000000 / price - 0.5)
Created Oct 26, 2019
cli bitmex liquidation calculation formula, minimal implementation
View bitmex-liquidation-calculator.py
 #!/usr/bin/env python3 import argparse from math import ceil, floor sign = lambda amt: -1 if amt<0 else (1 if amt>0 else 0) class dobj:
Last active Aug 13, 2019
Minimal general-purpose test web server
View minimal-web.py
 #!/usr/bin/env python3 from flask import Flask, request, jsonify app = Flask(__name__) methods = 'GET POST PUT DELETE'.split() @app.route('/', methods=methods)
Last active Oct 1, 2018
Autotask to Exact Online invoice-to-book-keeping converter
View knasboll.py
 #!/usr/bin/env python3 import argparse import csv from datetime import datetime import dateutil.parser from glob import glob import xml.etree.ElementTree as ET
Created Dec 30, 2017
Minimal 19 LoC memoization with timeout
View memoize.py
 import time class memoize(object): def __init__(self, timeout=60): self.cache = {} self.timeout = timeout def _flush(self): now = time.time()
Created Dec 8, 2017
Plot some candlesticks based on crypto currency data. Prepped for use in real webserver.
View coinplot.py
 #!/usr/bin/env python3 from bokeh.embed import components as plot_html from bokeh.plotting import figure, show, output_file import pandas as pd import requests import webbrowser url = 'https://www.binance.com/api/v1/klines?interval=15m&symbol=ARNBTC&limit=100'
Last active Nov 7, 2017
CLI-based chat client in 14 LoC
View websocket-chat-client.py
 #!/usr/bin/env python3 import asyncio from threading import Thread import websockets loop = asyncio.get_event_loop() def user_input(websocket): while True:
Last active Nov 7, 2017
Chat server in 13 LoC
View websocket-chat-server.py
 #!/usr/bin/env python3 import asyncio import websockets connections = set() async def broadcast(websocket, path): connections.add(websocket) try:
Created Nov 2, 2017