Skip to content

Instantly share code, notes, and snippets.

@datavudeja
datavudeja / ErrorHandlers.py
Created August 13, 2025 12:40 — forked from rpdelaney/ErrorHandlers.py
A python function decorator to catch exceptions and report their contents
from typing import Callable, Any
import functools
import traceback
def handle_exceptions(func: Callable[..., Any]) -> Callable[..., Any]:
"""
Decorate a function to catch and handle exceptions by returning a detailed error message.
Args:
@datavudeja
datavudeja / beartype.py
Created August 13, 2025 12:38 — forked from leycec/beartype.py
`@beartype` Decorator and Unit Test Suite Thereof
#!/usr/bin/env python3
'''
`@beartype` decorator, implementing a rudimentary subset of PEP 484-style type
checking based on Python 3.x function annotations.
See Also
----------
https://stackoverflow.com/a/37961120/2809027
Stackoverflow answer introducing the `@beartype` decorator.
@datavudeja
datavudeja / debounce.py
Created August 13, 2025 12:38 — forked from drnguyenn/debounce.py
Python Debounce
"""A ``debounce`` decorator implementation in Python similar to debounce_ in ``lodash``.
.. _debounce: https://lodash.com/docs#debounce
"""
from functools import wraps
from threading import Timer
import time
from typing import Any, Callable, Dict, Optional, Tuple
@datavudeja
datavudeja / python_logging.py
Created August 13, 2025 12:37 — forked from giladbarnea/python_logging.py
python_logging.py: python logging snippets
from __future__ import annotations
import builtins
from datetime import datetime
import functools
import inspect
import logging
import os
import re
import sys
@datavudeja
datavudeja / contract.py
Created August 13, 2025 12:36 — forked from dillonhicks/contract.py
Contracts, Interfaces, and Implementations
from __future__ import absolute_import, print_function
from abc import ABCMeta
import functools
import inspect
import logging
from collections import defaultdict, namedtuple
import boltons.typeutils
import six
@datavudeja
datavudeja / README.md
Created August 13, 2025 12:34 — forked from player1537/README.md
This is a library of helper functions that I use when writing Python Jupyter code.

This is a library of helper functions that I use when writing Python Jupyter code.

Usually, I just copy-paste this between notebooks, but I've found a need for simplifying the code-reuse story across different notebooks.

from __future__ import annotations
try:
    from mediocreatbest import auto
except ImportError:
 %pip install --quiet --upgrade pip
@datavudeja
datavudeja / 02 basics.py
Created August 13, 2025 12:19 — forked from mrdhiraj/02 basics.py
basics of python
#!/usr/bin/env python3
# * ABOUT
# ---------------------------------------------------------------------
# This section try to explain about the basic python things which will
# help to make your program simpler. It's time you switch from
# interpreter to some text-editor/IDE.
#
# May be you want to start with turtle before this
import torch
import time
import functools
from typing import Literal, Optional, Callable, Any
class TimerContext:
@staticmethod
def cuda_timer(unit: Literal['ms', 's'] = 's'):
"""返回CUDA计时上下文管理器"""
@datavudeja
datavudeja / stopwatch.py
Created August 12, 2025 14:25 — forked from quinnkj/stopwatch.py
Cookbook Recipes for timing blocks of code at runtime.
"""
webapp/core/utilities/stopwatch.py
Example Usage:
```
def countdown(n):
while n > 0:
n -= 1
# Use 1: Explicit start/stop
@datavudeja
datavudeja / timing_function_decorator.py
Created August 12, 2025 13:55 — forked from ArthurDelannoyazerty/timing_function_decorator.py
EASY TO USE. LONG FILE. EXAMPLE AT THE BOTTOM. A decorator that time functions, and have advanced features : options to print to the console, to a default file, to a custom file. Can dynamically group functions by a tag. Can display a complete timing summary in the console. Can create chart that compare functions or compare whole groups.
"""
```
from . import timefunc, timing_manager
```
This module provides a comprehensive and thread-safe toolkit for performance monitoring
in Python applications. It is designed to be both easy to use for quick debugging
and powerful enough for detailed performance analysis.
The module is built around two core components: