Skip to content

Instantly share code, notes, and snippets.

@yangmillstheory
yangmillstheory / compact.cpp
Last active January 22, 2019 02:52
Compact string
#include <iostream>
#include <cstring>
// Compactify a string in linear time.
//
// Examples:
//
// $ compact /foo/bar/baz # /f/b/baz
// $ compact foo/bar/baz # f/b/baz
// $ compact baz # baz
~/c/s/cpp
(i) ../../cquery/build/cquery --check test.cpp
( 0.000s) [main thread ] command_line.cc:438 | Running --check
( 0.000s) [main thread ] utils.cc:298 | Reading /Users/yangmillstheory/code/scrap/cpp/test.cpp
( 0.001s) [main thread ] command_line.cc:451 | Using path /Users/yangmillstheory/code/scrap/cpp/test.cpp
( 0.001s) [main thread ] project.cc:69 | Failed to normalize /Users/yangmillstheory/code/scrap/cpp/build
( 0.001s) [main thread ] project.cc:630 | Trying to load /Users/yangmillstheory/code/scrap/cpp/build/compile_commands.json
( 0.001s) [main thread ] project.cc:637 | Trying to load /Users/yangmillstheory/code/scrap/cpp/compile_commands.json
( 0.001s) [main thread ] project.cc:650 | Unable to load compile_commands.json located at "/Users/yangmillstheory/code/scrap/cpp/build/"; using directory listing instead.
( 0.001s) [main thread ] project.cc:530 | cquery has no clang arguments. Considering adding a .cque
@yangmillstheory
yangmillstheory / walk.py
Created August 25, 2018 01:13
Concurrent directory traversal
import os
import argparse
import hashlib
import threading
from collections import defaultdict
from queue import Queue
rem = Queue(maxsize=0)
sem = threading.Semaphore(value=100)
@yangmillstheory
yangmillstheory / scheduler.py
Last active May 22, 2018 15:53
Task scheduler
import heapq
import logging
import functools
import time
import threading
from datetime import datetime, timedelta
from collections import namedtuple
logger = logging.getLogger(__name__)
@yangmillstheory
yangmillstheory / timer.py
Last active May 20, 2018 17:48
Python timer
import time
class Timer(object):
def __init__(self, func=None):
if func is None:
func = time.perf_counter
self._func = func
self.reset()
@yangmillstheory
yangmillstheory / retry.py
Last active May 18, 2018 00:55
Exponential backoff decorator
import functools
import time
import logging
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
logger = logging.getLogger(__name__)
def retry(predicate=None, max_tries=3, max_delay=60,
@yangmillstheory
yangmillstheory / deadlock.py
Last active May 17, 2018 05:28
Dining philosopher's problem.
import threading
import time
import logging
logging.basicConfig(
format='%(threadName)s: %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)

Keybase proof

I hereby claim:

  • I am yangmillstheory on github.
  • I am valvarez (https://keybase.io/valvarez) on keybase.
  • I have a public key ASC8P7VXvDiMPIRsTwegtLEbAdeJFdDVaxSNvCRk_XspKgo

To claim this, I am signing this object: