Skip to content

Instantly share code, notes, and snippets.

public static class PromiseLike
{
public static Task Create(Action<TaskCompletionSource> action)
{
ThrowIfNull(action);
var tcs = new TaskCompletionSource();
action.Invoke(tcs);
return tcs.Task;
}
@Cologler
Cologler / SingletonService.cs
Created June 25, 2024 09:47
Singleton helper for DI
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
public interface ISingletonService<out TService>
{
TService Serivce { get; }
}
public static class SingletonServiceHelper
{
# copied from https://stackoverflow.com/questions/17782142/
def patch_requests_timeout(default_timeout: float) -> None:
import requests
old_send = requests.Session.send
def new_send(*args, **kwargs):
if kwargs.get("timeout", None) is None:
kwargs["timeout"] = default_timeout
@Cologler
Cologler / BitArrayExtensions.cs
Created July 19, 2022 13:42
BitArray to array
static class BitArrayExtensions
{
public static byte[] ToByteArray(this BitArray bits)
{
// copied from https://stackoverflow.com/questions/560123/convert-from-bitarray-to-byte
byte[] ret = new byte[(bits.Length + 7) >> 3];
bits.CopyTo(ret, 0);
return ret;
}
@Cologler
Cologler / deta-export-database.ps1
Created July 4, 2022 17:18
export database as json from deta.sh to stdout
param (
[Parameter(Mandatory = $true)]
[string] $ProjectKey,
[Parameter(Mandatory = $true)]
[string] $BaseName
)
$BaseUrl = "https://database.deta.sh/v1"
@Cologler
Cologler / bulk-git-clone.nu
Created July 4, 2022 13:02
bulk clone repos
# bulk clone repos from config list.
#
# to run the script, try:
# ^nu 'bulk-git-clone.nu' .\git_repos.yaml
def get-qualname [repo_url: string] {
let $name_parts = if $repo_url =~ '^https?://' {
let $match = ($repo_url | parse -r '^https?://(?P<host>.+)/(?P<user>.+)/(?P<repo>.+)$')
if ($match | length) > 0 {
@Cologler
Cologler / iwait.py
Last active May 26, 2022 12:33
wait with keyboard Interrupt support
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022~2999 - Cologler <skyoflw@gmail.com>
# ----------
#
# ----------
from time import monotonic as _time
def iwait(wait, timeout=None, *, interval=0.1):
@Cologler
Cologler / Load-PartialScripts.ps1
Created November 13, 2021 06:15
load all partial scripts to caller context.
# load all partial scripts to caller context.
# - for caller, add `. "$(Split-Path $PSCommandPath)\Load-PartialScripts.ps1"`;
# - the partial scripts must named `{caller}.{partial}.ps1`;
foreach (
$item in $(
Get-ChildItem `
-Path $(Split-Path $MyInvocation.PSCommandPath -Parent -Resolve) `
-Filter "$(Split-Path $MyInvocation.PSCommandPath -LeafBase).*$(Split-Path $MyInvocation.PSCommandPath -Extension)" `
| Sort-Object { Split-Path $_ -LeafBase }
@Cologler
Cologler / jsonp.py
Created March 17, 2021 02:01
a json+ decoder which support regex
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021~2999 - Cologler <skyoflw@gmail.com>
# ----------
#
# ----------
import json
import re
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021~2999 - Cologler <skyoflw@gmail.com>
# ----------
# check if there has some invisible chars in your file path.
# ----------
from typing import *
import os
import sys