Skip to content

Instantly share code, notes, and snippets.

@rwarren
rwarren / yaml_pathlib_fun.py
Last active September 13, 2021 15:14
Getting PyYaml's `yaml.safe_load` to support `pathlib.Path`
import pathlib
import yaml
def _path_representer(dumper, data):
return dumper.represent_scalar("!Path", str(data))
def _path_constructor(loader, node):
value = loader.construct_scalar(node)
return pathlib.Path(value)
@rwarren
rwarren / chia_stats.py
Created May 27, 2021 14:26
Chia stats estimator functions
def daysToWin(netspaceFraction, confidence = 0.95, challengesPerDay = 4608):
losingProbability = 1 - netspaceFraction
challengesToWin = math.log(1 - confidence, losingProbability)
return challengesToWin / challengesPerDay
def earningsPerYear(xchValue_USD, netspaceFraction, confidence = 0.95, challengesPerDay = 4608, xchPerWin = 2):
xchPerDay = xchPerWin / daysToWin(netspaceFraction, confidence, challengesPerDay)
dailyUSD = xchValue_USD * xchPerDay
return dailyUSD * 365
@rwarren
rwarren / xfer_plots.py
Last active September 12, 2021 15:07
Chia plot copying script -- To copy from the "final" plot location to farm disks, safely filling farm disks
#!/usr/bin/env python3
import os
from pprint import pprint as pp
import shutil
import socket
import sys
import typing as th
# Largest plot size seen is 108,935,751,977, and smallest is 108,232,347,648. For
@rwarren
rwarren / telegraf.conf
Created May 22, 2021 15:47
Stripped version of my telegraf.conf for chia
[global_tags]
srv_loc="home"
srv_vtype="real"
srv_purpose="chia-plotter"
srv_owner="internal"
[agent]
interval = "60s"
round_interval = true
metric_batch_size = 1000
@rwarren
rwarren / pgcon2020_irc_announce,py
Last active May 28, 2020 18:41
Massive hack to automate talk announcements for pgcon 2020. There is DEFINITELY a better way to do this.
#!/usr/bin/env python
# THIS FILE IS A MAAAAAAAAAAAASSIVE HACK QUICKLY MADE FOR THE ONLINE PGCON_2020
# There is DEFINITELY a better way to do this.
import argparse
import datetime
import sched
import socket
import sys
import time
@rwarren
rwarren / get_buffer_bench.py
Created March 23, 2020 13:56 — forked from 1st1/get_buffer_bench.py
get_buffer_bench.py
import struct
try:
from time import perf_counter as clock
except ImportError:
from time import time as clock
import asyncio
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
@rwarren
rwarren / get_ssh_client_ip.sh
Created May 4, 2019 05:48
shell function to robustly get the IP address of the currently connected ssh client (through subshells, user changes, and more)
get_ssh_client_ip() {
# Returns (via stdout) the IP address for the currently connected SSH session
# - this relies on the ssh server setting the SSH_CLIENT env var (done by openssh, dropbear, etc)
pid=$$ # current pid will be checked
while [ "$pid" -ne 1 ]; do
cip=$(cat /proc/$pid/environ | tr '\0' '\n' | grep 'SSH_CLIENT=' | awk -F'[= ]' '{ print $2 }')
[ -n "$cip" ] && break # got it!
pid=$(awk '{ print $4 }' /proc/$pid/stat) # the parent pid for the next try
done
@rwarren
rwarren / dumper.py
Created October 10, 2018 21:25
fiddling with subprocess piping
import time
import sys
DELAY = 0.2
for i in range(10):
sys.stdout.write("1")
time.sleep(DELAY)
sys.stdout.write("\r\n")
@rwarren
rwarren / qml_objectName_test.py
Last active November 9, 2017 02:52
demo of objectName() not working with QML (for me?)
import sys
from PyQt5 import QtCore, QtQml, QtWidgets
QML = b'''
import QtQuick 2.7
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import ObjectNameTester 1.0
Window {
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.