Python Forum

Full Version: How can I print from within actor(func) ??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following example from here.

To help me understand what is going on, I like to put print() statements in various places, at least until everything works.

func.__name__ gets the name of the func parameter passed to actor(func) as a string.

I tried putting print(func.__name__) in various places in the function actor(), but it does not print. Generators are tricky!

How can I print(f'key in _registry is {func.__name__}') from within actor(func) ??

This Python routine works fine and does what it should.

#! /usr/bin/python3
# actor1.py
#
# Simple attempt at actors

_registry = { }

# _registry[name] is a generator
# this function was called send() which was confusing, given .send
def data(name, msg):
    # send msg to the generator, which will yield it! What is the point?
    # maybe with some other function this will be useful
    _registry[name].send(msg)

# can't get actor to print func.__name__
# tried putting this in various places: print(f'key in _registry is {func.__name__}')
def actor(func):
    def wrapper(*args, **kwargs):
        gen = func(*args, **kwargs)
        next(gen)
        _registry[func.__name__] = gen
    return wrapper

if __name__ == '__main__':
    @actor
    def printer():
        while True:
            msg = yield
            print('printer:', msg)

    # initiate the genrator
    printer() # initiate the _registry
    printer() # produces {'printerQ': <generator object printerQ at 0x7b11d97329d0>}
    n = 10
    while n > 0:
        data('printer', n)
        n -= 1
def actor(func):
    def wrapper(*args, **kwargs):
        gen = func(*args, **kwargs)
        next(gen)
        _registry[func.__name__] = gen
        print(f'key in _registry is {func.__name__}') 
    return wrapper
or
def actor(func):
    def wrapper(*args, **kwargs):
        gen = func(*args, **kwargs)
        next(gen)
        _registry[func.__name__] = gen
    print(f'key in _registry is {func.__name__}') 
    return wrapper
Either spot I get this for output.
Output:
key in _registry is printer printer: 10 printer: 9 printer: 8 printer: 7 printer: 6 printer: 5 printer: 4 printer: 3 printer: 2 printer: 1
What is the problem that I am not seeing?
Thanks for the reply.

The problem is probably Idle. When I do this in Idle,

def actor(func):
    # this will not print
    #print(f'key in _registry is {func.__name__}')
    def wrapper(*args, **kwargs):
        gen = func(*args, **kwargs)        
        next(gen)      
        _registry[func.__name__] = gen
        print(f'key in _registry is {func.__name__}')
    return wrapper

printerQ()
None
n = 10
while n > 0:
    send('printerQ', n)
    n -= 1 
I get:

Output:
printer: 10 printer: 9 printer: 8 printer: 7 printer: 6 printer: 5 printer: 4 printer: 3 printer: 2 printer: 1
But in bash it prints:

Output:
pedro@pedro-HP:~/myPython/yield/tutorial2014$ ./actor1.py key in _registry is printer printer: 10 printer: 9 printer: 8 printer: 7 printer: 6 printer: 5 printer: 4 printer: 3 printer: 2 printer: 1
Should have tried it in bash as well!