Skip to content

Instantly share code, notes, and snippets.

@orez-
Created May 20, 2014 17:05
Show Gist options
  • Save orez-/0874d6cf3af715d0e36b to your computer and use it in GitHub Desktop.
Save orez-/0874d6cf3af715d0e36b to your computer and use it in GitHub Desktop.
Neat Scripts - A collection of neat scripts to show off features of the language used.
# Credit to daviddonna for most of this.
import inspect
def continuation(f):
def helper(f, args):
count = len(inspect.getargspec(f).args)
if len(args) == count:
return f(*args)
elif len(args) < count:
return lambda x, *eargs: helper(f, args + (x,) + tuple(eargs))
else:
return f(*args[:count])(*args[count:])
return lambda x, *eargs: helper(f, (x,) + tuple(eargs))
if __name__ == "__main__":
@continuation
def f(a, b, c):
print a, b, c
f(1)(2)(3)
f(1, 2)(3)
f(1)(2, 3)
f(1, 2, 3)
@continuation
def g(a, b, c):
return lambda d, e: (a, b, c, "!", d, e)
print g(1, 2, 3, 4, 5)
print g(1)(2)(3)(4, 5)
print g(1, 2)(3, 4, 5)
class Stupidest(object):
def __init__(self, dumbest=[]):
self.dumbest = dumbest
self.dumbest.append("lol")
Stupidest()
Stupidest()
Stupidest()
Stupidest()
Stupidest()
x = Stupidest()
print x.dumbest
# >> ['lol', 'lol', 'lol', 'lol', 'lol', 'lol', 'lol']
def falsify(f):
return FalseFun(f)
class FalseFun:
def __init__(self, f):
self.f = f
def __call__(self, *args, **kwargs):
self.f(*args, **kwargs)
def __nonzero__(self):
return False
if __name__ == "__main__":
class Superclass(object):
@falsify
def foo(self, *args):
print ' '.join(map(str, args))
class Subclass(Superclass):
def foo(self, *args):
print "Well, " + ' '.join(map(str, args))
sup = Superclass()
sub = Subclass()
if sup.foo:
print "Darn."
else:
print "Woohoo!"
if sub.foo:
print "Good!"
else:
print "No :("
sup.foo("And", "this", "works", "too")
sub.foo("who", "thought", "it", "wouldn't?")
# DO NOT EVER USE THIS CODE FOR ANY PURPOSES
__author__ = "Brian Shaginaw"
class Evil(object):
"""int-wrapper to allow ++num and --num (but not num++ or num--) in python.
Python cannot normally do this. If you wanted to be thorough you'd make all
the other int operations set _inc and _dec to None, but again, you shouldn't
ever be using this code anyway.
"""
def __init__(self, num=0):
self._inc = None
self._dec = None
self.num = num
def __int__(self):
return self.num
def __str__(self):
return str(self.num)
def __pos__(self):
"""+self. Two of these is an increment."""
if self._inc: # already one +
toR = self._inc # get the original guy and add one to him
toR.num += 1
self._inc = None # don't allow another + to trigger the increment
return toR
toR = Evil(self.num) # +self returns a new Evil that knows about the old one
toR._inc = self # this way you can (+x) twice without incrementing (more important for -)
return toR
def __neg__(self):
"""-self. Two of these is an increment."""
if self._dec:
toR = self._dec
toR.num -= 1
self._dec = None
return toR
toR = Evil(-self.num) # one "-" negates the number, of course.
toR._dec = self
return toR
if __name__ == "__main__":
x = Evil(5)
# These all work as expected
print x # 5
print ++x # 6
print x # 6
print --x # 5
print x # 5
print -x # -5
print -x # -5
# This is part of why you should never ever use this code.
y = -x
print y # -5
print -y # 4 (?????)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment