Skip to content

Instantly share code, notes, and snippets.

@ynkdir
ynkdir / memoryview_pointer.py
Created September 20, 2025 14:10
How to get pointer of memoryview (and how to break python object)
# How to get pointer of memoryview (and how to break python object)
from ctypes import *
PyBUF_FULL_RO = 0x0100 | 0x0010 | 0x0008 | 0x0004
class Py_buffer(Structure):
_fields_ = [
("buf", c_void_p),
("obj", c_void_p),
@ynkdir
ynkdir / winui3_webview2_environment_example.py
Last active September 20, 2025 05:11
Example to use WebView2 with CoreWebView2Environment.CreateWithOptionsAsync()
# Example to use WebView2 with CoreWebView2Environment.CreateWithOptionsAsync().
import asyncio
from win32more.Microsoft.UI.Xaml import Window
from win32more.Microsoft.UI.Xaml.Controls import WebView2
from win32more.Microsoft.Web.WebView2.Core import CoreWebView2Environment, CoreWebView2EnvironmentOptions
from win32more.Windows.Foundation import Uri
from win32more.appsdk.xaml import XamlApplication
@ynkdir
ynkdir / winui3_software_bitmap_example.py
Last active September 7, 2025 06:49
WinUI3 example for SoftwareBitmap
# WinUI3 example for SoftwareBitmap.
from win32more import FAILED, POINTER, Byte, UInt32, WinError
from win32more.appsdk.xaml import XamlApplication, XamlLoader
from win32more.Microsoft.UI.Xaml.Media.Imaging import SoftwareBitmapSource
from win32more.Windows.Graphics.Imaging import (
BitmapAlphaMode,
BitmapBufferAccessMode,
BitmapPixelFormat,
SoftwareBitmap,
@ynkdir
ynkdir / winui3_thread_example.py
Created August 20, 2025 17:03
WinUI3 example for accessing UI component in worker thread.
# WinUI3 example for accessing UI component in subthread.
# UI component cannot be accessed in subthread.
# Use asyncio or DispatcherQueue to execute command in mainthread.
import asyncio
import threading
import time
from win32more.Microsoft.UI.Dispatching import DispatcherQueue
from win32more.Microsoft.UI.Xaml.Media import SolidColorBrush
@ynkdir
ynkdir / main.rs
Last active August 2, 2025 15:45
rust logging allocator
// https://doc.rust-lang.org/std/alloc/struct.System.html
use std::alloc::{GlobalAlloc, Layout, System};
use std::io::Write;
static HEX: [[u8; 1]; 16] = [
[0x30],
[0x31],
[0x32],
[0x33],
@ynkdir
ynkdir / shadowing.py
Created May 11, 2025 14:47
shadowing definition by decorator
# shadowing definition by decorator
import sys
class shadowing:
def __new__(cls, klass):
self = super().__new__(cls)
self._klass = klass
setattr(sys.modules[klass.__module__], klass.__name__, self)
return klass
import importlib
import inspect
class LazyImport:
def __init__(self, module):
self._module = module
def __getattr__(self, name):
try:
value = getattr(self._module, name)
@ynkdir
ynkdir / asyncio-fire-and-forget-task-memo.md
Created December 29, 2024 09:23
Result of asyncio.create_task() may get garbage collected

asyncio.create_task() documentation says

asyncio.create_task(coro, *, name=None, context=None)
...
Important: Save a reference to the result of this function, to avoid a task
disappearing mid-execution. The event loop only keeps weak references to
tasks. A task that isn’t referenced elsewhere may get garbage collected at
any time, even before it’s done. For reliable “fire-and-forget” background
tasks, gather them in a collection:
@ynkdir
ynkdir / asyncio_win32_loop.py
Last active December 14, 2024 02:20
Connecting Asyncio and Win32 event loops
# /// script
# dependencies = ["win32more"]
# ///
import asyncio
import threading
from concurrent.futures import Future
from queue import Queue
from win32more import WinError
@ynkdir
ynkdir / asyncio_tkinter_guest.py
Last active December 20, 2024 15:16
Connecting Asyncio and Tkinter event loops
# Asyncio guest mode
#
# [Using “guest mode” to run Trio on top of other event loops](https://trio.readthedocs.io/en/stable/reference-lowlevel.html#using-guest-mode-to-run-trio-on-top-of-other-event-loops)
#
#
# Trio's guest mode model
#
# UI thread Asyncio thread
#
# +------------+