Beispiel #1
0
def outer():
    myassert(True, "---- in outer ----")
    myassert("outer" in globals(), "outer is in global scope")
    myassert("outer" in locals(), "outer is in local scope")
    myassert("inner" in globals(), "inner is in global scope")
    myassert("inner" in locals(), "inner is in local scope")
    myassert("a" in locals(), "a is in scope")
    myassert("b" in locals(), "b is in scope")
    myassert("c" in locals(), "c is in scope")

    a = 1

    myassert(True, "---- in outer after a ----")
    myassert("outer" in globals(), "outer is in global scope")
    myassert("outer" in locals(), "outer is in local scope")
    myassert("inner" in globals(), "inner is in global scope")
    myassert("inner" in locals(), "inner is in local scope")
    myassert("a" in locals(), "a is in scope")
    myassert("b" in locals(), "b is in scope")
    myassert("c" in locals(), "c is in scope")

    def inner():
        pass

    b = 2

    myassert(True, "---- in outer after b ----")
    myassert("outer" in globals(), "outer is in global scope")
    myassert("outer" in locals(), "outer is in local scope")
    myassert("inner" in globals(), "inner is in global scope")
    myassert("inner" in locals(), "inner is in local scope")
    myassert("a" in locals(), "a is in scope")
    myassert("b" in locals(), "b is in scope")
    myassert("c" in locals(), "c is in scope")

    if a == 1:
        c = 3
        myassert(True, "---- in outer in if ----")
        myassert("outer" in globals(), "outer is in global scope")
        myassert("outer" in locals(), "outer is in local scope")
        myassert("inner" in globals(), "inner is in global scope")
        myassert("inner" in locals(), "inner is in local scope")
        myassert("a" in locals(), "a is in scope")
        myassert("b" in locals(), "b is in scope")
        myassert("c" in locals(), "c is in scope")

    myassert(True, "---- in outer after c ----")
    myassert("outer" in globals(), "outer is in global scope")
    myassert("outer" in locals(), "outer is in local scope")
    myassert("inner" in globals(), "inner is in global scope")
    myassert("inner" in locals(), "inner is in local scope")
    myassert("a" in locals(), "a is in scope")
    myassert("b" in locals(), "b is in scope")
    myassert("c" in locals(), "c is in scope")
Beispiel #2
0
 def inner_function():
     myassert(outer_value == 'ninja', 'can see ninja')
     myassert(inner_value == 'samurai', 'can see samurai')
Beispiel #3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

myassert(True, "---- before outer ----")
myassert("outer" in globals(), "outer is in global scope")
myassert("outer" in locals(), "outer is in local scope")
myassert("inner" in globals(), "inner is in global scope")
myassert("inner" in locals(), "inner is in local scope")
myassert("a" in locals(), "a is in scope")
myassert("b" in locals(), "b is in scope")
myassert("c" in locals(), "c is in scope")


def outer():
    myassert(True, "---- in outer ----")
    myassert("outer" in globals(), "outer is in global scope")
    myassert("outer" in locals(), "outer is in local scope")
    myassert("inner" in globals(), "inner is in global scope")
    myassert("inner" in locals(), "inner is in local scope")
    myassert("a" in locals(), "a is in scope")
    myassert("b" in locals(), "b is in scope")
    myassert("c" in locals(), "c is in scope")

    a = 1

    myassert(True, "---- in outer after a ----")
    myassert("outer" in globals(), "outer is in global scope")
    myassert("outer" in locals(), "outer is in local scope")
    myassert("inner" in globals(), "inner is in global scope")
    myassert("inner" in locals(), "inner is in local scope")
Beispiel #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

weapons = ['shuriken', 'katana', 'nunchucks']
map((lambda (i, x): myassert(x == weapons[i], weapons[i] + ' is expected')), enumerate(weapons))
Beispiel #5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

def is_prime(num):
    is_prime.answers = {}

    if num in is_prime.answers:
        return is_prime.answers[num]
    prime = num != 1
    for v in range(2, num):
        if num % v == 0:
            prime = False
            break
    is_prime.answers[num] = prime
    return prime

myassert(is_prime(5), '5 is prime')
myassert(is_prime.answers[5], 'answer is cached')
Beispiel #6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

class Store:
    def __init__(self):
        self.next_id = 1
        self.cache = {}

    def add(self, fn):
        try:
            fn.id
            return False
        except:
            self.next_id = self.next_id + 1
            fn.id = self.next_id
            self.cache[fn.id] = fn
            return True

def ninja():
    pass

store = Store()

myassert(store.add(ninja), 'function is added')
myassert(store.add(ninja), 'function is added')

Beispiel #7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

ninja = lambda: myassert(True, 'Ninja')
ninja()
Beispiel #8
0
 def inner_function(param_value):
     myassert(outer_value, 'can see ninja from inside')
     myassert(inner_value, 'can see samurai from inside')
     myassert(param_value, 'can see wakizashi from inside')
     myassert(too_late,    'can see ronin from inside')
Beispiel #9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

def chirp(n):
    return chirp(n - 1) + '-chirp' if  n > 1 else 'chirp'

myassert(chirp(3) == 'chirp-chirp-chirp', 'result is expected')
Beispiel #10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

outer_value = 'ninja'
later = None

def outer_funtion():
    inner_value = 'samurai'
    def inner_function(param_value):
        myassert(outer_value, 'can see ninja from inside')
        myassert(inner_value, 'can see samurai from inside')
        myassert(param_value, 'can see wakizashi from inside')
        myassert(too_late,    'can see ronin from inside')
    global later
    later = inner_function

myassert('too_late' in locals(), 'can see ronin from outside')
too_late = 'ronin'
outer_funtion()
later('wakizashi')
Beispiel #11
0
def outer():
    myassert('inner' in locals(), 'inner is defined before innner')
    def inner():
        pass
    myassert('inner' in locals(), 'inner is defined after innner')
Beispiel #12
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

def is_nimble():
    return True

myassert('is_nimble' in locals(), 'is_nimble is defined')
myassert(is_nimble.__name__ == 'is_nimble', 'is_nimble has name')

can_fly = lambda s: True

myassert('can_fly' in locals(), 'can_fly is defined')
myassert(can_fly.__name__ == 'canfly', 'canfly has name')

def outer():
    myassert('inner' in locals(), 'inner is defined before innner')
    def inner():
        pass
    myassert('inner' in locals(), 'inner is defined after innner')

outer()
myassert('inner' in globals(), 'inner is in global scope')
Beispiel #13
0
def outer_function():
    myassert(outer_values == 'ninja', 'can see ninja')
Beispiel #14
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from lib.utils import myassert

class Ninja:
    def chirp(self, n):
        return self.chirp(n - 1) + '-chirp' if  n > 1 else 'chirp'

class Samurai:
    pass

ninja = Ninja()
myassert(ninja.chirp(3) == 'chirp-chirp-chirp', 'ninja result is expected')

samurai = Samurai()
samurai.chirp = ninja.chirp

ninja = None
myassert(samurai.chirp(3) == 'chirp-chirp-chirp', 'samurai result is expected')