<commit_before># -*- coding: utf-8 -*-

import sys

# Write a function which returns greeting message depending on usernames


def greeting(*names):
    msg = ""
    for name in names:
        msg += "Welcome {}\n".format(name)
    return msg


def test_greeting():
    assert "python" in greeting("python")
    res = greeting("python", "emacs")
    assert "python" in res
    assert "emacs" in res


# Write a function which returns greeting message depending on usernames
# and number of times greetings needs to be printed

def greeting_with_times(**kwargs):
    msg = ""
    for name, times in kwargs.iteritems():
        for time in range(times):
            msg += "Welcome {}\n".format(name)
    return msg


def test_greeting_with_times():
    res = greeting_with_times(python=2, emacs=3)
    assert res.count("python") == 2
    assert res.count("emacs") == 3
    res = greeting_with_times(krace=0)
    assert res.count("krace") == 0


# Python scopes locals(), globals(), nested functions.

# Write a function called cache which will cache factorial result.
# When factorial is called with same number argument as previous call
# fetch result from cache.

cached_results = {}

def cache(f):

    def inner(n):
        if n in cached_results:
            print "cached_results"
            return cached_results[n]
        cached_results[n] = f(n)
        return cached_results[n]
    return inner


@cache
def factorial(n):
    if n <= 1:
        return 1
    else:
        return n * factorial(n-1)

# test


def test_factorial():
    assert factorial(1) == 1
    assert 1 in cached_results

# Write a function called login which will take a parameter username.
# Write a decorator called login_required which is on top of login.
# login_decorator will look into listed of predefined username and
# return True or False

def login_required(f):
    def wrapped(*args, **kwargs):
        username = kwargs['username']
        if username in ['kracekumar', 'admin']:
            return f(*args, **kwargs)
        else:
            return False
    return wrapped

@login_required
def login(username):
    print "login succeeded"
    return True


def test_login():
    assert login({}, username="kracekumar") is True
    assert login({}, username="demo") is False


def test():
    print("test_greeting")
    test_greeting()
    print("test_greeting_with_times")
    test_greeting_with_times()
    print("test_factorial")
    test_factorial()
    print("test_login")
    test_login()

def main():
    pass


if __name__ == "__main__":
    if 'test' in sys.argv:
        test()

    main()
<commit_msg>Update 02-decorator-sol.py<commit_after># -*- coding: utf-8 -*-

import sys

# Write a function which returns a greeting message depending on usernames


def greeting(*names):
    msg = ""
    for name in names:
        msg += "Welcome {}\n".format(name)
    return msg


def test_greeting():
    assert "python" in greeting("python")
    res = greeting("python", "emacs")
    assert "python" in res
    assert "emacs" in res


# Write a function which returns a greeting message depending on usernames
# and number of times greetings needs to be printed

def greeting_with_times(**kwargs):
    msg = ""
    for name, times in kwargs.iteritems():
        for time in range(times):
            msg += "Welcome {}\n".format(name)
    return msg


def test_greeting_with_times():
    res = greeting_with_times(python=2, emacs=3)
    assert res.count("python") == 2
    assert res.count("emacs") == 3
    res = greeting_with_times(krace=0)
    assert res.count("krace") == 0


# Python scopes locals(), globals(), nested functions.

# Write a function called cache which will cache  factorial result.
# When  factorial is called with same number argument as previous call
# fetch result from cache.

cached_results = {}

def cache(f):

    def inner(n):
        if n in cached_results:
            print "cached_results"
            return cached_results[n]
        cached_results[n] = f(n)
        return cached_results[n]
    return inner


@cache
def factorial(n):
    if n <= 1:
        return 1
    else:
        return n * factorial(n-1)

# test


def test_factorial():
    assert factorial(1) == 1
    assert 1 in cached_results

# Write a function called login which will take a parameter username.
# Write a decorator called login_required which is on top of login.
# login_decorator will look into listed of predefined username and
# return True or False

def login_required(f):
    def wrapped(*args, **kwargs):
        username = kwargs['username']
        if username in ['kracekumar', 'admin']:
            return f(*args, **kwargs)
        else:
            return False
    return wrapped

@login_required
def login(username):
    print "login succeeded"
    return True


def test_login():
    assert login({}, username="kracekumar") is True
    assert login({}, username="demo") is False


def test():
    print("test_greeting")
    test_greeting()
    print("test_greeting_with_times")
    test_greeting_with_times()
    print("test_factorial")
    test_factorial()
    print("test_login")
    test_login()

def main():
    pass


if __name__ == "__main__":
    if 'test' in sys.argv:
        test()

    main()

{'fraction_non_alphanumeric': 0.10448901623686724, 'fraction_numerical': 0.005348615090735435, 'mean_word_length': 2.987814166031988, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4100890', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1552, 'n_words': 533}