Skip to content

Instantly share code, notes, and snippets.

View dabeaz's full-sized avatar

David Beazley dabeaz

View GitHub Profile
@dabeaz
dabeaz / lala.py
Last active January 2, 2023 04:20
Some lambdas
# Author: David Beazley (https://www.dabeaz.com)
# Twitter: @dabeaz
from functools import reduce
run = lambda s: reduce(lambda *_:..., iter(lambda s=[s]:
(_:=s.pop()(),s.append(_))[0], None))
const = lambda v,c : lambda: c(v)
add = lambda x,y,c : lambda: c(x+y)
mul = lambda x,y,c : lambda: c(x*y)
@dabeaz
dabeaz / fibonacci.py
Created January 31, 2022 00:07
Fibonacci, with flair.
# fibonacci.py
def 𝙛𝒊𝗯𝗼𝐧ₐ𝗰𝖈ⅈ(n: 𝑖𝑛𝘵) -> ⁱ𝗇𝔱:
if 𝕟 <= 2:
return 1
else:
return 𝖋ⁱ𝓫𝘰𝖓ᵃ𝒄c𝖎(-1) + f𝚒𝔟𝒐𝓷𝑎𝔠𝐜𝘪(𝓃-2)
for n in 𝒓𝘢n𝐠ℯ(1, 10):
𝑝𝖗i𝖓𝘁(𝐟𝔦b𝓸nₐc𝒄ᵢ(n))
@dabeaz
dabeaz / aproducer.py
Created October 17, 2019 17:46
"Build Your Own Async" Workshop - PyCon India - October 14, 2019 - https://www.youtube.com/watch?v=Y4Gt3Xjd7G8
# aproducer.py
#
# Async Producer-consumer problem.
# Challenge: How to implement the same functionality, but no threads.
import time
from collections import deque
import heapq
class Scheduler:
@dabeaz
dabeaz / README.txt
Created October 15, 2019 20:10
PyCon India 2019, Code from Keynote Presentation by @dabeaz
Code from PyCon India 2019 Keynote Talk
David Beazley (https://www.dabeaz.com)
======================================
This code is presented "as is" and represents what was live-coded
during my closing keynote presentation at PyCon India, Chennai,
October 13, 2009. I have made no changes to the files.
Requires: Python 3.6+, numpy, pygame
@dabeaz
dabeaz / curbench.py
Created October 20, 2017 19:25
Curio Benchmark of Large Data Transfer
# A Curio adaption of benchmark code at:
#
# https://gist.github.com/pitrou/202221ca9c9c74c0b48373ac89e15fd7
import struct
try:
from time import perf_counter as clock
except ImportError:
from time import time as clock
@dabeaz
dabeaz / calc.py
Created September 14, 2016 20:35
SLY (Sly Lex-Yacc) Example
# See: https://github.com/dabeaz/sly
from sly import Lexer, Parser
class CalcLexer(Lexer):
tokens = {
'NAME', 'NUMBER',
}
ignore = ' \t'
literals = { '=', '+', '-', '*', '/', '(', ')' }
@dabeaz
dabeaz / bug.py
Created January 7, 2016 10:11
Diabolical bug involving code that behaves differently in a class decorator vs. a metaclass
# bug.py
def decorate(func):
print('Decorating', func.__name__)
return func
def wrap_methods(cls):
for name in vars(cls):
if name.startswith('f_'):
setattr(cls, name, decorate(getattr(cls, name)))
@dabeaz
dabeaz / aecho.py
Last active October 17, 2023 03:26
Live-coded examples from my PyCon Brasil 2015 Keynote
# aecho.py
from socket import *
import asyncio
loop = asyncio.get_event_loop()
async def echo_server(address):
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
@dabeaz
dabeaz / FileUsage.ipynb
Created March 26, 2013 10:39
IPython Notebook Example for my Usenix ;Login: Article
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dabeaz
dabeaz / pystate.c
Created February 3, 2013 17:56
From Python-3.3 source code, Python/pystate.c. Note the comment: I have no recollection of requesting this feature, but it must have been important at the time ;-).
/* Routines for advanced debuggers, requested by David Beazley.
Don't use unless you know what you are doing! */
PyInterpreterState *
PyInterpreterState_Head(void)
{
return interp_head;
}
PyInterpreterState *