Skip to content

Instantly share code, notes, and snippets.

@antonl
antonl / async_demo.py
Created July 17, 2025 21:12
Nested async concurrency bug
import asyncio
from dataclasses import dataclass, field
from time import perf_counter_ns, sleep
from typing import Literal
@dataclass
class API:
state: list[tuple[str, Literal["start", "stop"], int]] = field(default_factory=list)
sleep: float = 0.01
@antonl
antonl / chatbot.py
Created May 22, 2025 23:23
Socket-based chatbot
import socket
from concurrent.futures import ThreadPoolExecutor
import logging
from datetime import datetime
# Configure logging
logging.basicConfig(
level=logging.DEBUG, # Set the minimum logging level
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', # Set the format of log messages
datefmt='%Y-%m-%d %H:%M:%S', # Set the format of the date and time
@antonl
antonl / webcrawler.py
Created May 16, 2025 20:00
Webcrawler solutions
# ThreadPool version
from concurrent.futures import ThreadPoolExecutor
import time
class Solution:
def crawl(self, startUrl: str, htmlParser: 'HtmlParser') -> List[str]:
domain, *_ = startUrl[7:].split('/')
domain = "http://" + domain
@antonl
antonl / minidask.py
Last active May 5, 2025 19:57
Toy task queue with common subexpression elimination
"""
Implements a simple task computation framework inspired by Dask.
The tasks are encoded as a map where the keys are the names of
values and the values are either computed values, references to
other keys, or tuples representing a function call.
For example, the function call fn(x) may be represented as
```python
import io
import os
from functools import lru_cache
from collections import Counter
from dataclasses import dataclass
import regex as re
import mmap
from concurrent.futures import ProcessPoolExecutor, as_completed
GPT2_PATTERN = r"""'(?:[sdmt]|ll|ve|re)| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""

Keybase proof

I hereby claim:

  • I am antonl on github.
  • I am aloukian (https://keybase.io/aloukian) on keybase.
  • I have a public key ASDfbiDVp6PrF9hVGyK-3DuCF9vHS2KHI408vLzUpiLlAwo

To claim this, I am signing this object:

@antonl
antonl / config.py
Created January 22, 2016 16:46
Glueviz config file for opening transient grating datasets
from glue.config import data_factory
from glue.core import Data, Component
from h5py import File
import numpy as np
def is_nudie_file(filename, **kwargs):
'''checks that the given file is a nudie file'''
if not filename.endswith('.h5'):
return False
@antonl
antonl / pyqt4-example-main.py
Last active August 29, 2015 14:24
PyQt4 Ui Example
from __future__ import print_function, unicode_literals
import sys
from PyQt4 import QtGui, QtCore
from main_ui import Ui_MainWindow
import subprocess
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
@antonl
antonl / make-figure.py
Created May 29, 2015 15:24
Persistent oscillation in PIXIS cameras
from winspec import SpeFile
from pathlib import Path
from matplotlib.pylab import *
from scipy.signal import argrelmax
# Period of sampling in seconds
regen_period = 0.001*2
# Change the path to load the SPE file
file_path = Path('./100BRX-800fs.SPE')
# tags for phase cycles
phase_cycles = [0, 1, 0.6, 1.6, 1.3, 2.3]
# subspaces
sub1, sub2 = np.zeros((3,3), dtype=complex), np.zeros((3,3), dtype=complex)
sub1 = np.array([[np.exp(1j*np.pi*(x)), np.exp(1j*np.pi*(-x)), 1] for x in phase_cycles[0::2]])
sub2 = np.array([[np.exp(1j*np.pi*(x)), np.exp(1j*np.pi*(-x)), 1] for x in phase_cycles[1::2]])
# Full matrix
C = np.kron(np.array([[1, 0], [0, 0]]), sub1) + np.kron(np.array([[0, 0], [0, 1]]), sub2)