Skip to content

Instantly share code, notes, and snippets.

@fwiffo
fwiffo / unpacking_gotcha.py
Created March 26, 2013 21:51
Using tuples to swap values around is a convenient, idiomatic and efficient way to avoid temporary variables. However, you have to be careful about the order in which you unpack a tuple! Don't think of them as "a bunch of assignments that happen at once" but rather a sequence of assignments that happen in order. I got bit by this today and it wa…
# Let's rearrange the hierarchy of these objects
foo = SomeClass()
foo.bar = SomeClass()
foo.bar.baz = None
assert foo.bar is not None # Fine so far...
new_foo, foo.bar, foo.bar.baz = foo.bar, foo.bar.baz, foo
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# AttributeError: 'NoneType' object has no attribute 'baz'
# Wait a sec, didn't we just assert that foo.bar is not None?
@fwiffo
fwiffo / finditer.py
Last active January 10, 2022 01:09
Simple implementation of an iterable version of str.find()
def finditer(S, sub, start=0, end=None, overlap=False):
"""Iterate over the indices of substring sub in S.
Returns an iterable, in ascending order, of the indices in S where
substring sub is found such that sub is contained within S[start:end].
Optional arguments start and end are interpreted as in slice notation. A
True value for the optional argument overlap will allow overlapping
matches.
"""
@fwiffo
fwiffo / hanoi.b
Created March 24, 2013 15:05
The Towers of Hanoi in Brain****.
>++++[<++++++++>-]<.......>++++[<++++++++>-]<+.>++++[<-------->-]<-..........
...>+++++[<+++++++>-]<-.>+++++[<------->-]<+.............>+++++[<+++++++>-]<.
[-]++++++++++.>+++++[<++++>-]<++.......>++++++++++[<+++++++++>-]<++.[-]>++++[
<++++++++>-]<.............>++++++++++[<+++++++++>-]<++.[-]>++++[<++++++++>-]<
.............>++++++++++[<+++++++++>-]<++.[-]++++++++++.>+++++[<++++>-]<++...
....>++++++++++[<+++++++++>-]<++.[-]>++++[<++++++++>-]<.............>++++++++
++[<+++++++++>-]<++.[-]>++++[<++++++++>-]<.............>++++++++++[<+++++++++
>-]<++.[-]++++++++++.>+++++[<++++>-]<++....>++++++++[<++++++++++>-]<-.>++++++
+[<------->-]<-.....>+++++++[<+++++++>-]<+.[-]>++++[<++++++++>-]<....>+++++[<
++++>-]<.>+++++[ A B C <---->-]<.....>+
@fwiffo
fwiffo / pologlotquine.c
Created March 24, 2013 14:51
A polyglot quine. Will compile, run and print its source code as C, C++, Python (2.x or 3.x) or Perl.
#include <stdio.h>
#define q(a,...) a
#define substr q
#define eval(a) main(){char c[]=a,n=10;c[419]=0;printf(c+4,n,n,n,n,34,34,n,34,39,c+4,39,34,n);}/* Copyright (C) Thomas Schumm <[email protected]>
exec("from sys import*;substr=q=lambda y:exit(stdout.write(y[4:-46]%((10,)*4+(34,34,10,34,39,y[4:-46],39,34,10))))",None);#*/
eval(substr(q("$p='#include <stdio.h>%c#define q(a,...) a%c#define substr q%c#define eval(a) main(){char c[]=a,n=10;c[419]=0;printf(c+4,n,n,n,n,34,34,n,34,39,c+4,39,34,n);}/* Copyright (C) Thomas Schumm <[email protected]>%cexec(%cfrom sys import*;substr=q=lambda y:exit(stdout.write(y[4:-46]%%((10,)*4+(34,34,10,34,39,y[4:-46],39,34,10))))%c,None);#*/%ceval(substr(q(%c$p=%c%s%c;printf($p,(10)x4,34,34,10,34,39,$p,39,34,10)%c),1,-1))%c';printf($p,(10)x4,34,34,10,34,39,$p,39,34,10)"),1,-1))