Skip to content

Instantly share code, notes, and snippets.

@DavidCEllis
DavidCEllis / dataclass_preprocess.py
Last active April 17, 2024 14:17
Example preprocessor for dataclasses to use Annotated[...] values instead of Field.
import inspect
from copy import copy
from dataclasses import dataclass, field, fields, Field
from typing import Annotated, Any, ClassVar, get_origin
# Modifying objects
class FieldModifier:
__slots__ = ("modifiers", )
modifiers: dict[str, Any]

Improving Dataclasses' Import Speed

I've seen the committers thread on trying to improve dataclasses' start time performance but it seemed focused on the execution time, while that is an area to look at I think it may be worth looking at improving the import time of dataclasses itself.

I've been working on my own implementation of the same idea and had found that importing some stdlib modules had a significant impact on the overall time it took to run. On looking at dataclasses I noticed that it also imported some of the same modules.

@DavidCEllis
DavidCEllis / namespace_impact_on_python_launch_performance.md
Last active December 22, 2022 18:16
Import time differences when a namespace package is installed

Import performance tests

When I was working on performance for prefab_classes I noticed that on recreating my venv the output from importtime had changed. It turns out that this was due to the original venv having sphinx installed. In particular the sphinxcontrib namespace packages.

Having these installed results in importlib.util being imported on python launch which pulls in a number of other python modules.

Two commands used to check performance.

@DavidCEllis
DavidCEllis / filedialogs.py
Last active July 18, 2022 13:31
Tkinter file dialog wrappers that return Path objects or None with a contextmanager to handle unwanted main window.
"""
Quick tkinter file dialogs for scripts.
Returns Path objects or None.
Hides the main tkinter window with a context manager.
I kept remaking this or copying it from project to project for short scripts so here it is for easier copy/paste.
See https://docs.python.org/3/library/dialog.html#module-tkinter.filedialog for options.
"""
@DavidCEllis
DavidCEllis / jsimport.py
Last active October 23, 2017 17:07
Import JavaScript directly into python
"""
Python Javascript importer
(Python 3.6+ - may work on earlier if you remove the f-strings)
Requires js2py: https://github.com/PiotrDabkowski/Js2Py
pip install js2py
This is a little import hook that takes advantage of
js2py in order to translate javascript to python during
@DavidCEllis
DavidCEllis / shuffle_builtins.py
Created January 6, 2017 01:02
Bad ideas #1 - Reassigning builtins based on dict ordering - no effect if you're using 3.6!
"""
An alternative approach to making code Python 3.6 only
The new dict implementation means the original and new
builtin lists should be identical in Python 3.6
Inspired by a friend's tweet mentioning that someone had used str
as a variable name in an inherited codebase.
Python 2 Behavior: