Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Countdown of letters with reactor

I am learning twisted and have developed a simple python script that should count down each letter of the alphabet and stop the reactor when it gets to z. However, I am getting the following error:

assert builtins.callable(callable), f"{callable} is not callable"
builtins.AssertionError: a is not callable

Here’s my script:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Countletters(object):
    letters = list( map(chr, range(97, 123)) )

    def countletters(self):
        for l in self.letters:
            if l  == 'z':
                rector.stop()
            else:
                print(l)
                reactor.callLater(1, l)
from twisted.internet import reactor
reactor.callWhenRunning(Countletters().countletters)
print('Start countdown')
reactor.run()
print('Stop countdown')

>Solution :

You are almost there – the problem is the callLater part.

callLater expects a function, something "Callable", but you give it l; a string (the current letter).

Try this for an example:
I pass functools.partial(print, l) to callLater which basically creates a callable object, which will simply do print(l) when called.

import functools

from twisted.internet import reactor

class Countletters(object):
    letters = list(map(chr, range(97, 123)))

    def countletters(self):
        for l in self.letters:
            if l == 'z':
                reactor.stop()
            else:
                print(l)
                reactor.callLater(1, functools.partial(print, l))


reactor.callWhenRunning(Countletters().countletters)
print('Start countdown')
reactor.run()
print('Stop countdown')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading