Skip to content

Instantly share code, notes, and snippets.

View michaelbukachi's full-sized avatar

Michael Bukachi michaelbukachi

View GitHub Profile
@michaelbukachi
michaelbukachi / async.kt
Created December 4, 2019 13:37
callback to kotlin coroutine
interface Callback<S> {
fun success(s: S)
fun error(e: Exception)
}
fun<T> doSomethingLong(callback: Callback<T>) {
}
private suspend fun <T> someFuncAwait() :T = suspendCancellableCoroutine {
@michaelbukachi
michaelbukachi / forkdelta.py
Last active January 14, 2020 08:44
Forkdelta example
import websockets
import asyncio
async def hello():
uri = 'wss://api.forkdelta.app/socket.io/'
async with websockets.connect(uri,) as websocket:
# await websocket.send('getMarket')
await websocket.send('getMarket')
res = await websocket.recv()
import threading
import time
def func1():
print('Starting func 1')
end_time = time.time() + 5
i = 0
while time.time() < end_time:
package farmdrive.ripe.utils
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import androidx.core.app.ActivityCompat
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.location.*
@michaelbukachi
michaelbukachi / script.js
Last active October 22, 2021 14:36
Fabric.js Textbox resize according to specified height
// load canvas with id 'c'
const canvas = new fabric.Canvas('c');
// use canvas height as the height limit
var limit = canvas.height;
var text = new fabric.Textbox('Some very long text');
// set initial values
text.set({
top: margin,
width: canvas.width,
@michaelbukachi
michaelbukachi / admin.py
Last active November 25, 2021 10:40
Flask-Admin with `autocommit=True`
'''
This workaround helps avoid:
sqlalchemy.exc.InvalidRequestError: No transaction is begun
whenever you try to create/update a model when `autocommit=True`
'''
class BaseModelView(ModelView):
def create_model(self, form):
self.session.begin()
super(BaseModelView, self).create_model(form)
@michaelbukachi
michaelbukachi / PersistentCookieStore.java
Last active April 20, 2022 07:20
Java persistent cookie storage using the gson library.
/**
* Checkout https://gist.github.com/Triodes/b54aea95aceeb160c23d for an android implementation
*
**/
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import java.io.FileNotFoundException;
import java.io.FileReader;
@michaelbukachi
michaelbukachi / realm.kt
Last active May 24, 2022 04:31
Realm Coroutines
import io.realm.*
import io.realm.kotlin.where
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
private suspend fun <T: RealmObject, S: RealmQuery<T>> findAllAwait(query: S): RealmResults<T> = suspendCancellableCoroutine { continuation ->
val listener = RealmChangeListener<RealmResults<T>> { t -> continuation.resume(t) }
@michaelbukachi
michaelbukachi / pycharm-precommit-fix.md
Last active June 8, 2023 15:37
Pycharm precommits fix