Skip to content

Instantly share code, notes, and snippets.

@tubaman
tubaman / walkdata.py
Created October 11, 2023 00:29
walk_data - os.walk but for json-like data
from unittest import TestCase
def walk_data(root):
"""Walk a nested data structure like os.walk"""
data = root[-1]
root = root[:-1]
if hasattr(data, 'items'):
for key, value in data.items():
yield root + (key,), value
@tubaman
tubaman / collect.py
Created June 1, 2024 16:07
A generic version of listify that can take any type like list, set, dict
import functools
import types
def collect(collection_type):
"""A decorator that will collect stuff from a generator. For example,
>>> def tuple_generator():
... yield ('a', 'b')
... yield ('c', 'd')
import unittest
from datetime import timedelta, date
from collections.abc import Sequence, Iterator
from itertools import islice
class date_range(Sequence):
def __init__(self, start, stop, step=timedelta(days=1)):
self.start = start
@tubaman
tubaman / anywidget-play.ipynb
Created September 28, 2024 13:16
AnyWidget prototype of pyodide-worker to main thread RPC
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.