Skip to content

Instantly share code, notes, and snippets.

View almarklein's full-sized avatar

Almar Klein almarklein

View GitHub Profile
"""
In https://github.com/pygfx/wgpu-py/issues/114 we tried to find a way
to invalidate numpy/memoryview views when the base memory was released.
This file lists some attempts (and related cases) that demonstrates how
thet do not work.
In the end, we've not found a way to do it.
"""
import ctypes
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx
import pylinalg as la
import numpy as np
def set_position(ob, pos):
# compat between old and new api
if hasattr(ob, "local"):
ob.local.position = pos
@almarklein
almarklein / timestamp_speed.py
Last active March 14, 2023 22:15
timestamp speed and precision
from datetime import datetime
import numpy as np
import time
n = 100000
mark1 = datetime.now()
@almarklein
almarklein / three_cube.html
Created October 12, 2022 13:05
ThreeJS sandbox
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js sandbox</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<body>
<div id='infodiv' style=""></div>
<script src='https://cdn.jsdelivr.net/gh/mrdoob/three.js@dev/build/three.js'></script>
@almarklein
almarklein / plotly_fast_image_test.html
Created November 30, 2020 12:48
A small html doc that can be used to test the use of reversed axii with fast images for plotly.
<html>
<script src='dist/plotly.js'></script>
<body>
<div id='myDiv'></div>
<script>
var source = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAlCAYAAAAA7LqSAAADmklEQVR4nO2XUWhTVxjHfyfdmKMN1oelK9QSLUVY2RWHrEKtLw66KQX30Ifpg0qxz31wb8O0UNjL1I3h09iDAy000D0Z0DJhEdslBXN7SwZBJmmbErKwNuU2UXDes4d4r700bib33KflD+Ge8+We78sv3/fdcy401VRTTTXVVFMIvxwnEhG5c97fP+lbLPARZHz8qBwY+AAA0zTp69MA/4B8cTo+flRq2n4MY42dMMFgEICRkZ+Uxw2odgigafudq2majn3nWLV8ATGMNdf44cPfd9lVyxeQwcF3MIw1gsEgY2OLzM9fYmDgPebnLzlQquVLj1y//pFcWan+R1eujLm+KxYTHDr0o/K4b6l2GI2efPnY3eTUqSMUiwmgBQDD+ANNC6sOCSgurUxmVAKUSpfp6tpHLJYiFksBL4AXaFr45Vy9lGUkkxmVhpEll9ukra1q6+raB+D68d3d76oK6ZKSjGQyozIWS5HLbVIujwCQyz0jHL4LVIHsT3f3r0xNfS3/zV8jUlpaNsT6eoS2tq/Q9TnSaZOZmRAzMyFKpcvo+hz5fEE5jGcQOxt2GeXzBQC2t6fY3p5y3bu+HnFsqmE8gdh9AThlZGtr67kzDoU6CIU6nPnGxioXLnzD0NCXLC7+rQTGE4jd3MePJ9H1OQBu3PjW2SO2tp6TTvfQ2dnhWjc9PSvs++2rVyl7aun6XTY2Vl22dLqHw4erp97Ozg6WlnqIRqfFqzVqIMADiL3x7cyGrb1733bGds8ArvK6f/+HRkPXVMNHhWj0pMzlNkkmw46tvT1NofAUTXtfPnnyrKbvgwf3yImJpPIznufSOns2C8Dt22EKhafMzq6Kf
"""
Implementation of an iterator that yields the lines in a file in reverse order.
Copyright (C) 2018, Almar Klein
BSD licensed.
"""
## ===== The code =====
def readlines_reversed(f):
@almarklein
almarklein / trim_py_files.py
Last active May 24, 2021 15:58
Trim trailing whitespace in Python files
import os
def trim_py_files(*directories):
"""Remove trailing whitespace on all .py files in the given directories.
"""
nchanged = 0
for directory in directories:
for root, dirs, files in os.walk(directory):
for fname in files:
@almarklein
almarklein / bb64.js
Created January 12, 2018 22:38
Efficient JavaScript implementation of Base64 encoding/decoding of bytes (Uint8Array)
"use strict";
function base64encode(b, last_two) {
"use strict";
console.assert(b.BYTES_PER_ELEMENT == 1);
// Most Base64 encoders use +/ for the last two characters, but not all
if (last_two === undefined) last_two = '+/';
// Init charcodes array
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + last_two;
var charcodes = new Uint8Array(64);
## Option 1 - strings for verbatim html, dicts for elements with attributes
class MyReactlikeWidget(ReactLike):
name = event.StringProp('john', settable=True)
def render(self):
return ['greet: ',
dict(type='b',
onclick=lambda:print('clicked!'),
innerHTML='hi ' + self.name),
@almarklein
almarklein / messagepool.py
Created January 25, 2016 21:53
Multi-process communication via a message pool implemented using UDP multicast
# Copyright 2016 (C) Almar Klein. Consider this 2-clause BSD licensed.
"""
I initially wrote this to be included in flexx for multiple processes
on the same machine to communicate, more specifically, to allow a Flexx
CLI to get info on and terminate running server processes. I tested it
to work on Windows and Linux. I eventuially discarded this approach
because I don't fully get how UDP multicast works. It's important for
this to only work for localhost, and I am not sure if it does, or how
hard it is to compromise this. I went with a simpler approach based on
http requests using Tornado.