dwhall / farc

Framework for state machines with run-to-completion concurrency using asyncio. Python 3.4 or later
MIT License
28 stars 6 forks source link

provide a function to convert signal to str #7

Closed maxpeng-apc closed 3 years ago

maxpeng-apc commented 3 years ago

When I debug the state machine written in farc package, I may print the received signal to understand what is going on. After I register a signal using Signal.register function, the signal is represented as an int. If I print the signal as a integer, it is difficult to understand its meaning. I propose to add a function to Signal class for converting the signal to a string.

class Signal(object):
    ...
    def sig_to_str(self, sig):
        return self._lookup[sig]
dwhall commented 3 years ago

This is a good idea. At first, I hoped I could make a __str__() method to be Pythonic. But the Singleton pattern that Signal adopts prevents this. So I went with a straightforward solution:

    @staticmethod
    def to_str(sigid):
        return Signal._lookup[sigid]

Usage:

farc.Signal.to_str(farc.Signal.TIME_TICK))

I will commit this shortly.