Skip to content

Instantly share code, notes, and snippets.

@polyvertex
polyvertex / sqlite.py
Last active January 19, 2021 19:59
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 / 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`
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 / 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 ...
@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 / garmin_encode_file_name.py
Last active May 25, 2018 07:58
Imitate the file naming of Garmin devices
import datetime
import string
def name_file(dt):
"""
Return a name according to the given `datetime.datetime` object.
This is how Garmin devices seem to name their activity files, as explained
`here <https://forums.garmin.com/forum/into-sports/running/forerunner-220-aa/53766->`_.
@polyvertex
polyvertex / kimsufi.py
Created October 4, 2017 14:09
A script to check Kimsufi server availability using OVH's API
#!/usr/bin/env python
#
# A script to check Kimsufi server availability using OVH's API.
# Typical usage:
# python kimsufi.py --dump-txt --dump-json --loop --verbose 160sk1
#
import sys
import os.path
import argparse
@polyvertex
polyvertex / char_type_of.cpp
Last active December 31, 2016 09:59
Deduce the char type of virtually any literal or container (C++11)
// char_type_of: deduce the char type of virtually any literal or
// container (C++11 SFINAE at its finest)
// Original author: Jean-Charles Lefebvre <polyvertex@gmail.com>
// util: an almighty version of std::decay<>, with super powers
template <typename T>
struct super_decay
{
typedef
typename std::remove_cv<
@polyvertex
polyvertex / super_decay.cpp
Created December 30, 2016 15:56
An almighty std::decay<>
template <typename T>
struct super_decay
{
typedef
typename std::remove_cv<
typename std::remove_pointer<
typename std::remove_reference<
typename std::remove_extent<
typename std::decay<T>::type>::type>::type>::type>::type type;
};
@polyvertex
polyvertex / wtypes.py
Last active August 27, 2020 04:34
A wrapper over ctypes for the Windows API
import ctypes as ct
from ctypes.wintypes import *
import uuid
def has_func(ctypes_dll, func_name):
if hasattr(ctypes_dll, func_name):
return func_name
if not func_name.endswith("W") and hasattr(ctypes_dll, func_name + "W"):
return func_name + "W"
return None