Skip to content

Instantly share code, notes, and snippets.

"""This is a docstring at module level."""
print(__doc__)
"""This docstring is ignored"""
print(__doc__)
def my_func():
"""This is a docstring in my_func."""
print(my_func.__doc__)
my_func()
@yipyip
yipyip / gist:6205271
Last active December 20, 2015 22:29
rerolling...
import random as rand
####
def reroll_rec(minval=1, maxval=6, accu=0, depth=99):
"""It's tailrecursion when a function don't have to go back the whole call stack.
Only for demonstration purpose. Not intended for production use.
"""
assert depth >= 0
@yipyip
yipyip / gist:6163205
Created August 6, 2013 09:46
nicht root menu, sondern nur submenu aendern.
def rename_team(team_number):
"""rename teamnames in the teamnamces dict and in the menu dict"""
# hier kommt nicht der neue name
new_name = input("please enter new name for team number {}: ".format(
team_number))
if new_name == "":
print("nothing renamed")
return
team_names[team_number] = new_name
@yipyip
yipyip / gist:6150627
Last active December 20, 2015 14:59
string concatenation timings (Man beachte auch das letzte Ergebnis, da ist etwas Overhead beim Aufruf drin.)
In [41]: s = [str(i) for i in xrange(100)]
In [42]: timeit reduce(lambda x, y: x + y, s, '')
10000 loops, best of 3: 31.9 us per loop
In [43]: timeit "".join(s)
100000 loops, best of 3: 2.98 us per loop
In [44]: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p = s[:16]
@yipyip
yipyip / gist:6149951
Created August 4, 2013 10:13
no text += ...
comment_buffer = []
...
comment_buffer.append('my_comment')
...
def show_comments(comment_buffer):
'\n'.join(comment_buffer)
@yipyip
yipyip / gist:6121157
Last active December 20, 2015 11:09
Hier noch mehr Goblins. ;-)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Stinky and Grunty reloaded.
(http://pyvideo.org/video/880/stop-writing-classes)
;-)
Works with Python2 + 3.
...
try:
getattr(myobject, 'a_method')
except AttributeError:
return 0
else:
return callable(getattr(myobject, 'a_method'))
...
from __future__ import print_function
@yipyip
yipyip / hangman
Last active December 19, 2015 23:59
wrong naming in make_task: chars->markers
#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-
"""A Simple Hangman Demo for Python3.
"""
####
import random as rand