Skip to content

Instantly share code, notes, and snippets.

Avatar

Mohammad Omidvar momvart

View GitHub Profile
@momvart
momvart / ISO8601Parser.cs
Last active Apr 10, 2022
A set of functions to parse some data and time data expressed in ISO 8601 format for C#.
View ISO8601Parser.cs
using System;
using System.Text.RegularExpressions;
namespace RBC.WorkflowEngine.Core.Utilities;
/// <summary>
/// Provides a set of functions for parsing some of date- and time-related
/// data represented in ISO 8601 formats.
/// </summary>
/// <see href="https://en.wikipedia.org/wiki/ISO_8601">
@momvart
momvart / KotlinObjectExtensions.cs
Created Nov 24, 2021
A set of extensions functions written on object, by default available in Kotlin, ported for c#.
View KotlinObjectExtensions.cs
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
namespace Utilities
{
public static class KotlinObjectExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Also<T>(this T callee, Action<T> action)
@momvart
momvart / ThousandSeparatorVisualTransformation.kt
Last active Apr 9, 2022
A VisualTransformation for inserting thousand-separator commas in TextFields inputting numbers. It also has support for fraction parts and can put limit on the number of fraction digits. #android #compose
View ThousandSeparatorVisualTransformation.kt
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.input.VisualTransformation
import java.text.DecimalFormat
import kotlin.math.min
class ThousandSeparatorVisualTransformation(
var maxFractionDigits: Int = Int.MAX_VALUE,
var minFractionDigits: Int = 0
@momvart
momvart / ObjectToInferredTypesConverter.cs
Last active Feb 27, 2021
A converter for `System.Text.Json` that recursively parse converts json to the inferred types. Uses `ExpandoObject` for objects.
View ObjectToInferredTypesConverter.cs
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Dynamic;
public class ObjectToInferredTypesConverter : JsonConverter<object>
{
public override object Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) =>
@momvart
momvart / GMM.py
Created Jan 20, 2021
Implementation of Gaussian Mixture Models clustering using numpy and scipy.
View GMM.py
import numpy as np
from numpy.random import choice
from numpy.linalg import norm
from scipy.stats import multivariate_normal
def train_gmm(data, k, convergence_threshold):
raw_data = data
data = pd.DataFrame(data) # Should be removed
feature_columns = data.columns
pis = np.ones((k, 1)) * 1 / k
@momvart
momvart / GoogleAuthenticatorUtils.kt
Created Aug 19, 2020
A helper class which can generate a URI for google authenticator.
View GoogleAuthenticatorUtils.kt
import android.net.Uri
object GoogleAuthenticatorUtils {
enum class KeyType(val uriKey: String) {
TIME_BASED("totp"),
COUNTER_BASED("hotp")
}
//Based on https://github.com/google/google-authenticator/wiki/Key-Uri-Format
@momvart
momvart / MarginItemDecoration.kt
Last active Aug 19, 2020
An item decoration for recyclerview which adds margin between items.
View MarginItemDecoration.kt
import android.content.Context
import android.graphics.Rect
import android.util.LayoutDirection
import android.view.View
import androidx.annotation.DimenRes
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MarginItemDecoration(
private val marginLeft: Int,
@momvart
momvart / PlaceholderWithBaseline.kt
Created Jul 23, 2020
Add support of baseline to constraintlayout Placeholder. When you want to set baseline constraint on a Placeholder doesn't work because it doesn't support it. With this simple extension you can solve the issue.
View PlaceholderWithBaseline.kt
import android.content.Context
import android.util.AttributeSet
import androidx.constraintlayout.widget.Placeholder
// You can replace it with Placeholder when you want to set layout_constraintBaseline_toBaselineOf
class PlaceholderWithBaseline @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : Placeholder(context, attrs, defStyleAttr) {
override fun getBaseline(): Int =
content.baseline
@momvart
momvart / BWT_First_To_Last.py
Last active May 13, 2020
A generator for mapping of last to first column in BWT matrix. Input is a BW transform of DNA sequence.
View BWT_First_To_Last.py
#change this if you want to support other types of sequences
def get_char_index(c):
if c == 'A':
return 1
elif c == 'C':
return 2
elif c == 'G':
return 3
elif c == 'T':
return 4
@momvart
momvart / BLOSUM62.py
Created Apr 8, 2020
Simple BLOSUM62 implementation in python. Used for sequence alignment of proteins. Based on https://en.wikipedia.org/wiki/BLOSUM
View BLOSUM62.py
BLOSUM62_MATRIX = [[4, 0, -2, -1, -2, 0, -2, -1, -1, -1, -1, -2, -1, -1, -1, 1, 0, 0, -3, -2],
[0, 9, -3, -4, -2, -3, -3, -1, -3, -1, -1, -3, -3, -3, -3, -1, -1, -1, -2, -2],
[-2, -3, 6, 2, -3, -1, -1, -3, -1, -4, -3, 1, -1, 0, -2, 0, -1, -3, -4, -3],
[-1, -4, 2, 5, -3, -2, 0, -3, 1, -3, -2, 0, -1, 2, 0, 0, -1, -2, -3, -2],
[-2, -2, -3, -3, 6, -3, -1, 0, -3, 0, 0, -3, -4, -3, -3, -2, -2, -1, 1, 3],
[0, -3, -1, -2, -3, 6, -2, -4, -2, -4, -3, 0, -2, -2, -2, 0, -2, -3, -2, -3],
[-2, -3, -1, 0, -1, -2, 8, -3, -1, -3, -2, 1, -2, 0, 0, -1, -2, -3, -2, 2],
[-1, -1, -3, -3, 0, -4, -3, 4, -3, 2, 1, -3, -3, -3, -3, -2, -1, 3, -3, -1],
[-1, -3, -1, 1, -3, -2, -1, -3, 5, -2, -1, 0, -1, 1, 2, 0, -1, -2, -3, -2],
[-1, -1, -4, -3, 0, -4, -3, 2, -2, 4, 2, -3, -3, -2, -2, -2, -1, 1, -2, -1],