Skip to content

Instantly share code, notes, and snippets.

@Cologler
Cologler / iwait.py
Last active May 26, 2022 12:33
wait with keyboard Interrupt support
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022~2999 - Cologler <[email protected]>
# ----------
#
# ----------
from time import monotonic as _time
def iwait(wait, timeout=None, *, interval=0.1):
@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 / 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 / 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;
}
# 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 / 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
{
public static class PromiseLike
{
public static Task Create(Action<TaskCompletionSource> action)
{
ThrowIfNull(action);
var tcs = new TaskCompletionSource();
action.Invoke(tcs);
return tcs.Task;
}