Skip to content

Instantly share code, notes, and snippets.

@Cologler
Cologler / example.py
Created August 12, 2020 09:55
sqlitedict with jsonref
with SqliteDict('./c.db', encode=json.dumps, decode=json.loads, autocommit=True) as db:
with contextlib.closing(SqliteDictLoader(db)) as sdloader:
db['a'] = {'b': 2, 'c': {
'$ref': 'sqlitedict://./k'
}}
db['k'] = {'f': 'feji'}
loader = Loader()
loader.loaders.append(sdloader)
c = jsonref.loads(json.dumps(db['a']), loader=loader)
@Cologler
Cologler / get_drive_labels.py
Created August 21, 2020 03:21
get windows drive label
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020~2999 - Cologler <[email protected]>
# ----------
#
# ----------
from ctypes import windll, create_unicode_buffer, c_wchar_p, sizeof
from string import ascii_uppercase
@Cologler
Cologler / AtomicWriteFileStream.cs
Last active August 25, 2020 13:40
atomic write file for C#
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace Utils
{
@Cologler
Cologler / FastDirectoryEnumerator.cs
Created August 27, 2020 16:14
fast directory enumerator
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.Win32.SafeHandles;
// backup for https://www.codeproject.com/Articles/38959/A-Faster-Directory-Enumerator
@Cologler
Cologler / once.js
Last active September 25, 2020 10:06
Replacement for npm package onetime and once.
/* Copyright (c) 2020~2999 - Cologler <[email protected]> */
/**
* Run a function exactly one time.
*
* @param fn {Function}
*/
function once(fn) {
let called;
let value;
@Cologler
Cologler / setup_proc_timeout.py
Created October 11, 2020 19:58
setup process timeout
def setup_proc_timeout(timeout: float, basedir: str, prefix: str=''):
'''
setup process timeout;
dump traceback from all threads to `basedir` if timeout.
'''
import os
import datetime
import atexit
import faulthandler
language: python
dist: xenial
python:
- '3.8'
- '3.9'
before_install:
- pip install poetry poetry-dynamic-versioning
install:
- poetry install
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021~2999 - Cologler <[email protected]>
# ----------
# check if there has some invisible chars in your file path.
# ----------
from typing import *
import os
import sys
@Cologler
Cologler / jsonp.py
Created March 17, 2021 02:01
a json+ decoder which support regex
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021~2999 - Cologler <[email protected]>
# ----------
#
# ----------
import json
import re
@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 }