Make a decorator accepts
that takes as many arguments as the function takes. That decorator specify the types of the arguments that your function takes. If any of the arguments does not match the type in the decorator raise a TypeError
@accepts(str)
def say_hello(name):
return "Hello, I am {}".format(name)
say_hello(4)
TypeError: Argument 1 of say_hello is not str!
@accepts(str)
def say_hello(name):
return "Hello, I am {}".format(name)
say_hello("Hacker")
@accepts(str, int)
def deposit(name, money):
print("{} sends {} $!".format(name, money))
return True
deposit("RadoRado", 10)
Note that this is just a nice example. In real life you don't want use this!
Make a decorator encrypt
that takes an integer. The decorator should encrypts the returned string of a function using the Caesar Cipher. That integer is the encryptions key.
@encrypt(2)
def get_low():
return "Get get get low"
get_low()
Igv igv igv nqy
Make a decorator log
that takes an file_name
and writes in to this file a log. New line for every call of the decorated function.
@log('log.txt')
@encrypt(2)
def get_low():
return "Get get get low"
get_low()
Igv igv igv nqy
And the log file should look like this:
get_low was called at 2015-08-04 02:51:41.929980
get_low was called at 2015-08-04 02:51:45.992980
get_low was called at 2015-08-04 02:51:42.999923
Make a decorator performance
that takes an file_name
and writes in to this file a log. New line for every call of the decorated function. This decorator should log the time needed for the decorated function to execute.
@performance('log.txt')
def something_heavy():
sleep(2)
return "I am done!"
something_heavy()
I am done!
And the log file should look like this:
something_heavy was called and took 2.00 seconds to complete
something_heavy was called and took 2.10 seconds to complete
Implement a function that takes two iterables and returns another one that concatenate the two iterables.
def chain(iterable_one, iterable_two):
pass
>>> list(chain(range(0, 4), range(4, 8)))
[0, 1, 2, 3, 4, 5, 6, 7]
Implement a function that takes one iterables and one iterable mask. The mask is an collection that contains only True
or False
This function returns only this objects from the first collection that have True
on their position in the mask.
def compress(iterable, mask):
pass
>>> list(compress(["Ivo", "Rado", "Panda"], [False, False, True]))
["Panda"]
Implement a function that takes an iterable and returns endless concatenation of it.
def cycle(iterable):
pass
>>> endless = cycle(range(0,10))
for item in endless:
print(item)
You have some text files. They represent a book. Our book contains chapters. Each chapter starts with #
at the beginning of the line. (Markdown book)
Our book is made of many files. Each file has its number 001.txt, 002.txt, 003.txt
Each file may contain one or more chapters.
Write a program that displays on the console each chapter. You can only move forwards using the space button
.
Try not to load the whole book in the memory. Use generator!
Make a python program that generates books.
Your program should take the following parameters.
- Chapters count
- Chapter length range (in words)
The words should be with random length and random char. The format of the book should be the same as previous task. Try to place some new lines in the chapters at random positions. The whole book must be in one file.
Try to generate bigger book. Like 1-2G, and try to pass it to the previous program.
Make a generator that returns the current position of your mouse pointer.
Then make a function that checks if your mouse is at the upper left corner of your screen. If it is your computer should make a beep sound.