Skip to content

Instantly share code, notes, and snippets.

@bj0
bj0 / DelegateAccess.kt
Created July 6, 2022 20:16 — forked from aartikov/DelegateAccess.kt
Wrap MutableStateFlow to property delegate
internal object DelegateAccess {
internal val delegate = ThreadLocal<Any?>()
internal val delegateRequested = ThreadLocal<Boolean>().apply { set(false) }
}
internal val <T> KProperty0<T>.delegate: Any?
get() {
try {
DelegateAccess.delegateRequested.set(true)
this.get()
@bj0
bj0 / default.nix
Created October 4, 2021 18:02
Launch a coconut jupyter console using nix
# coconut/default.nix
{ stdenv
, python3
, name ? "nixpkgs"
, packages ? p: []
, pkgs
, pkg ? "coconut"
}:
let
@bj0
bj0 / UsbPermissions.kt
Last active August 20, 2024 18:09
Requesting permission to open UsbDevice in Kotlin using coroutines
package com.my.app
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import kotlinx.coroutines.experimental.CompletableDeferred
@bj0
bj0 / Cached.kt
Created January 26, 2017 17:15
A modified version of Kotlin's lazy delegate that can be reset.
/**
* Created by Brian Parma on 2/10/16.
*
* This is basically just the lazy delegate copy and pasted, then modified so that it can be
* reset by setting it equal to null
*/
/**
* Represents a value with lazy initialization.
*
* To create an instance of [Cached] use the [cached] function.
@bj0
bj0 / setup_fake.py
Created July 19, 2016 18:21
A python script that downloads nuget and installs FAKE and Xunit test runner. It also creates a script for running tests.
#!python
import os
import subprocess as sp
__build = \
'''
// include Fake lib
#r @"tools/FAKE/tools/FakeLib.dll"
open Fake
@bj0
bj0 / async_iterable.py
Last active June 6, 2018 15:07
A Python 3.5 decorator for creating asynchronous iterators from coroutines (since we don't have async generators yet).
import inspect
import types
@types.coroutine
def _yield_(value):
return (yield value)
async def yield_(value):
@bj0
bj0 / bottle_sse.py
Last active August 29, 2015 14:23
SeverSentEvent example with bottle + gevent/eventlet
# from gevent import monkey, sleep, getcurrent, spawn
# from gevent.queue import Queue
# monkey.patch_all()
from eventlet import monkey_patch, sleep, spawn
from eventlet.queue import Queue
monkey_patch()
import re
import subprocess as sp
import time
@bj0
bj0 / gen_perlin_texture
Last active August 29, 2015 14:13
generate a texture from Perlin Noise in kivy 1.8
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.graphics.texture import Texture
from kivy.core.window import Window
from noise import pnoise2, snoise2
kv='''
#:import math math
@bj0
bj0 / gist:7497386
Last active December 28, 2015 11:59
Getting tags (including coverart) out of .aa file using pyaudibletags
from pyaudibletags import aafile
import Image, sys
import io
import binascii
def main(file):
f = aafile(file)
print 'Title: {}'.format(f.get_parent_title())
print 'Author: {}'.format(f.get_author())
print 'Date: {}'.format(f.get_pubdate())
@bj0
bj0 / jinja2.ipynb
Created April 9, 2013 05:55
IPython Custom Cell Magic for Rendering Jinja2 Templates
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.