Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Bukachi michaelbukachi

View GitHub Profile
@michaelbukachi
michaelbukachi / script.js
Last active Jan 24, 2021
Fabric.js Textbox resize according to specified height
View script.js
// 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,
View LocationUtils.kt
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.*
View pycharm-precommit-fix.md
@michaelbukachi
michaelbukachi / realm.kt
Last active Jul 24, 2020
Realm Coroutines
View realm.kt
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) }
View multithread.py
import threading
import time
def func1():
print('Starting func 1')
end_time = time.time() + 5
i = 0
while time.time() < end_time:
@michaelbukachi
michaelbukachi / admin.py
Last active Mar 15, 2020
Flask-Admin with `autocommit=True`
View admin.py
'''
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 / forkdelta.py
Last active Jan 14, 2020
Forkdelta example
View forkdelta.py
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()
@michaelbukachi
michaelbukachi / async.kt
Created Dec 4, 2019
callback to kotlin coroutine
View async.kt
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 / dialog.kt
Created Sep 19, 2019
Alert Dialog overlay color
View dialog.kt
val dialog = Dialog(context!!)
// val dialog = AlertDialog(context!!)
dialog.window?.setBackgroundDrawableResource(android.R.color.black)
// dialog.window?.setBackgroundDrawable(ColorDrawable(Color.BLACK))
dialog.show()
@michaelbukachi
michaelbukachi / credentialshelper.sh
Last active Aug 26, 2019
Docker credentials helper linux setup
View credentialshelper.sh
#!/usr/bin/env bash
sudo apt install libsecret-1-0
wget https://github.com/docker/docker-credential-helpers/releases/download/v0.6.3/docker-credential-secretservice-v0.6.3-amd64.tar.gz && tar -xf docker-credential-secretservice-v0.6.3-amd64.tar.gz && chmod +x docker-credential-secretservice && mv docker-credential-secretservice /usr/local/bin/