Python Forum
How can I print from within actor(func) ??
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I print from within actor(func) ??
#1
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
Reply
#2
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?
Reply
#3
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to output one value per request of the CSV and print it in another func? Student44 3 1,399 Nov-11-2022, 10:45 PM
Last Post: snippsat
  Func Animation not displaying my live graph jotalo 0 1,604 Nov-13-2020, 10:56 PM
Last Post: jotalo
  Trying to write func("abcd") -> "abbcccdddd" omm 8 4,274 Oct-24-2020, 03:41 AM
Last Post: bowlofred
  How do I create a actor if I subscribe to a specific topic? sdf1444 0 1,738 Aug-01-2019, 09:29 PM
Last Post: sdf1444
  call func from dict mcmxl22 3 2,937 Jun-21-2019, 05:20 AM
Last Post: snippsat
  About [from FILE import FUNC] Nwb 7 3,694 Apr-21-2019, 02:46 PM
Last Post: snippsat
  Executing func() from a different folder ebolisa 2 2,420 Jan-14-2019, 10:18 AM
Last Post: ebolisa
  Correct number wrong position func. albry 5 6,118 Jan-11-2019, 04:01 PM
Last Post: Larz60+
  How can I return my list from a func? Mike Ru 3 3,154 Oct-22-2018, 01:15 PM
Last Post: buran
  list func in lambda mepyyeti 1 2,959 Mar-10-2018, 09:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020