def inspect(func, name):
"""Show information for the function"""
inspectable = ["__doc__", "__module__", "__defaults__", "__code__", "__closure__"]
print "{0:-^80}".format(name)
print "{0:<20} | {1!r}".format("type", type(func))
print "{0:<20} | {1}".format("callable", callable(func))
for attr in inspectable:
print "{0:<20} | ".format(attr),
value = getattr(func, attr, "- not present -")
if attr == "__doc__":
print value.replace("\n", "\n" + (" " * 21) + "| ")
else:
print "{0!r}".format(value)
print "-" * 80
print
import plain
inspect(plain.fibonacci, "plain.fibonacci")
inspect(plain.center, "plain.center")
inspect(plain.incapsulate_arguments, "plain.incapsulate_arguments")
wrapped_1 = plain.incapsulate_arguments(1, 2, ready=True, reason="freak")
inspect(wrapped_1, "plain.incapsulate_arguments.wrapped")
inspect(plain.incapsulate_scope, "plain.incapsulate_scope")
wrapped_2 = plain.incapsulate_scope("./methods.py")
inspect(wrapped_2, "plain.incapsulate_scope.wrapped")
import methods
inspect(methods.Song.play, "methods.Song.play")
inspect(methods.Song.possible_modes, "methods.Song.possible_modes")
inspect(methods.Song.lyrics, "methods.Song.lyrics")
import callable_class
instance = callable_class.CallMe("Queen of Swans")
inspect(instance, "callable_class.CallMe")
def fibonacci():
"""Infinit generator for elements of Fibonacci sequence"""
a, b = 0, 1
while True:
yield b
a, b = b, a + b
def center(subject, length=100, character='='):
"""Center the `subject` enclosing it with rows of specified symbols
`subject` - string to be centered and enclosed with `character`s
`length` - length of final string. Default: 100
`character` - the character used to enclose `subject`. Default: '='"""
return "{{subject}:{character}^{length}".format(
subject=" " + subject + " ",
length=length,
character=character
)
def incapsulate_arguments(*args, **kwargs):
"""Create a function to depict self arguments"""
def wrapped(owner_name=""):
"""Will create string with owner arguments on demand
`owner_name` - name of owner function"""
return "{owner} has:\n{lst!r}\n{dct!r}".format(
owner=owner_name,
lst=args,
dct=kwargs
)
return wrapped
def incapsulate_scope(filename):
"""Open file for reading and return configurable generator"""
file = open(filename, 'r')
def wrapped(lines):
"""Yields not more then `lines` from given file"""
for line in range(0, lines):
data = file.readline()
if len(data) is 0:
break
yield data
return wrapped
class Song(object):
"""Simple song class"""
playing_mode = "text"
def __init__(self, lyrics):
"""We initialize song with a `lyrics` and think that it's ready!"""
self.___lyrics = lyrics
def play(self):
"""Perform song"""
return self.__lyrics
@staticmethod
def possible_modes():
"""Tell us possible playing modes"""
return ["text", "audio", "telepathy"]
@property
def lyrics(self):
"""Give only reading access to the lyrics"""
return self.__lyrics
class CallMe(object):
"""Callable class that is too lazy to do anything"""
def __init__(self, name):
self.__name = name
def __call__(self, *args, **kwargs):
print "I'm {0} and I don't want do a thing!".format(self.__name)
print "Take back your args: {0!r}".format(args)
print "And never come back with you kwargs: {0!r}".format(kwargs)