Skip to content

Instantly share code, notes, and snippets.

@Ivaylo-Bachvarov
Last active February 28, 2017 12:39
Show Gist options
  • Save Ivaylo-Bachvarov/4f06052c9a142e060ce344388ef7d43a to your computer and use it in GitHub Desktop.
Save Ivaylo-Bachvarov/4f06052c9a142e060ce344388ef7d43a to your computer and use it in GitHub Desktop.
Dimitar's Python Tasks

Decorator Problems

@accepts

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

Examples

@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!

@encrypt(key)

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.

Example

@encrypt(2)
def get_low():
    return "Get get get low"

get_low()

Igv igv igv nqy

@log(file_name)

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.

Example

@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

@performance(file_name)

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.

Example

@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

Generators

chain

Implement a function that takes two iterables and returns another one that concatenate the two iterables.

def chain(iterable_one, iterable_two):
    pass

Example

>>> list(chain(range(0, 4), range(4, 8)))
[0, 1, 2, 3, 4, 5, 6, 7]

compress

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

Example

>>> list(compress(["Ivo", "Rado", "Panda"], [False, False, True]))
["Panda"]

cycle

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)

Book Reader

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.

Link to the book

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!

Book 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.

Mouse Beep

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment