Skip to content

Instantly share code, notes, and snippets.

View michaelbukachi's full-sized avatar

Michael Bukachi michaelbukachi

View GitHub Profile
@michaelbukachi
michaelbukachi / script.js
Last active June 4, 2024 20:06
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 / sentence_fixer.py
Last active June 9, 2017 05:27
A script to correct sentences
import re
def replace(match):
if match:
phrase = match.group(0)
symbol = match.group(1)
return symbol + ' ' + phrase[-1].upper() if symbol is '.' else phrase[-1]
def parse(string):
string = re.sub(r'([\.\,\?\:\;])\w', replace, string) # add space between any punctuation symbol and letter
@michaelbukachi
michaelbukachi / ExcelParser.java
Created June 30, 2017 13:03
Exam timetable parser
package dita.dev.myportal.utils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
@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 / clear.sh
Created January 20, 2018 13:45
Git remove ignored files from Git index
#!/bin/bash
git rm -r --cached .
git add .
git commit -am "Remove ignored files"
@michaelbukachi
michaelbukachi / MainActivity.java
Created June 10, 2018 10:14
Spinner Custom Object Example
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
@michaelbukachi
michaelbukachi / Auth.java
Created July 24, 2018 10:08
Mocking of asynchronous call
public static class Auth {
private static Auth auth;
private static String baseUrl;
private String message = null;
public static Auth getInstance(String base) {
baseUrl = base;
if (auth == null) {
auth = new Auth();
@michaelbukachi
michaelbukachi / CoroutinesTest.kt
Created December 6, 2018 18:54
Coroutine Mockk Example
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.junit.Test
class CoroutinesTest {
@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 / file.py
Last active January 11, 2019 21:12
Mongoengine case insensitive query with _in operator
import operator
from functools import reduce
from mongoengine import Document, Q, StringField
class Person(Document):
name = StringField()
names = ['mike', 'John', 'JIMMY']