Skip to content

Instantly share code, notes, and snippets.

@polyvertex
polyvertex / class_vs_namedtuple.py
Last active June 3, 2018 15:54
Small performance test on class objects creation versus named tuples
from collections import namedtuple
from pprint import pprint
import sys
import timeit
class C1:
__slots__ = ('a', )
class C2:
@polyvertex
polyvertex / memstreambuf.h
Created April 12, 2019 20:37
A memory stream buffer class compliant with std::basic_streambuf
#pragma once
namespace xx {
// A memory stream buffer class compliant with `std::basic_streambuf`.
//
// Usage example:
//
// std::vector<char> data;
// // ... fill-in *data* here ...
import os.path
import sublime
import sublime_plugin
# build 3176: this does not work properly. self.view.set_name() works but it
# seems to sort-of "unlinks" the view from its physical file path, so that if
# the "Save" command is invoked upon this view, a "Save As" dialog will pop up
# even though the view was loaded from an existing file.
@polyvertex
polyvertex / datastruct.py
Last active October 2, 2020 12:33
__slots__ based class pretty much like a mutable collections.namedtuple
import itertools
import threading
_datastruct_cache_lock = threading.Lock()
_datastruct_cache = {}
class DataStruct(object):
"""
A ``__slots__`` based class that works pretty much like a mutable
`collections.namedtuple`
@polyvertex
polyvertex / sqlite.py
Last active May 6, 2025 07:54
sqlite.py - sqlite3 with nested transactions for real
# Copyright (c) Jean-Charles Lefebvre
# SPDX-License-Identifier: MIT
import contextlib
import importlib
import importlib.resources
import os
import re
import sqlite3
import sys
@polyvertex
polyvertex / arty.py
Last active July 20, 2025 09:51
arty.py - A collection wrapper to easily get values from nested collections in a pythonic way
# Copyright (c) Jean-Charles Lefebvre
# SPDX-License-Identifier: MIT
import re
from collections.abc import Iterable, Mapping, Sequence, Sized
__all__ = ["Arty"]
_NONE_TYPE = type(None)
@polyvertex
polyvertex / entro.py
Last active July 8, 2025 09:19
entro.py - simple entropy check to evaluate whether compression is worth it
from collections import Counter
from typing import Union
def should_compress(
data: Union[bytes | bytearray | memoryview],
/, *,
threshold: int = 128) -> bool:
# do not compress if buffer size is < threshold
if len(data) < threshold: