6
\$\begingroup\$

I call these text updates "Feeds". A Feed object has some basic attributes like its content string, how many lives it has and its priority in the queue. I haven't made a unittest for this one, but below I have added a small test function for the purpose of posting this question. I use this to update statusbar in GUI applications.

import threading
import queue
import time

LOW_PRIORITY = 0
MEDIUM_PRIORITY = 1
HIGH_PRIORITY = 2

class Feed(object):
    def __init__(self, msg, life=1, priority=LOW_PRIORITY):
        self.msg = msg
        self.life = life
        self.priority = LOW_PRIORITY if life == -1 else priority

    def hit(self):
        if self.life > 0:
            self.life -= 1
        elif not self.life:
            raise ValueError("Feed {} is already dead.".format(self))

    def __bool__(self):
        return True if self.life else False

    def __lt__(self, obj):
        if self.priority > obj.priority:
            return True
        return False

class FeedThread(threading.Thread):
    def __init__(self, globevent, handler, updateinterval=3):
        super(FeedThread, self).__init__()

        self.globevent = globevent
        self.handler = handler
        self.updateinterval = updateinterval
        self.feedqueue = queue.PriorityQueue()

    def PushFeed(self, feed):
        self.feedqueue.put(feed)

    def run(self):
        while not self.globevent.is_set():
            try:
                feed = self.feedqueue.get(block=True, timeout=1)
                if feed:
                    self.handler(feed.msg)
                    feed.hit()
                    self.PushFeed(feed)
                    time.sleep(self.updateinterval)
            except queue.Empty:
                pass

def test_feed():
    newstime = threading.Event()
    def bullettin(news):
        print("\r{:^79}".format(news), end="")
    scroll = FeedThread(newstime, bullettin)
    scroll.start()
    sports_feed = Feed("Barclays Premier League - Man Utd V Tottenham - 12:45",
                        life=2, priority=MEDIUM_PRIORITY)
    weather_feed = Feed("London - Cloudy, High 26C. Winds EDE at 15 to 30 km/h",
                        life=-1)
    entertainment_feed = Feed("Upcoming movie - Pixels (2015)",
                            life=2)
    for feed in (sports_feed, weather_feed, entertainment_feed):
        scroll.PushFeed(feed)
    time.sleep(15)
    breaking_feed = Feed("BREAKING NEWS - Cats love lasers - pew pew pew!", 
                        priority=HIGH_PRIORITY)
    scroll.PushFeed(breaking_feed)
    time.sleep(15)
    newstime.set()

if __name__ == "__main__":
    test_feed()
\$\endgroup\$
2
  • \$\begingroup\$ "didn't stick to the guidelines (PEP or Sphinx)" - why not? Are you using some other style guide? Which? \$\endgroup\$
    – jonrsharpe
    Jul 16 '15 at 6:08
  • \$\begingroup\$ Generally only for consistency with projects whose style was established prior to PEP-8. Making up your own makes it less readable for those used to following established guides (whether PEP-8 or another), and your code crucially isn't internally consistent. \$\endgroup\$
    – jonrsharpe
    Jul 16 '15 at 11:18
1
\$\begingroup\$

A few quick comments:

  • In __bool__(), rather than using an if statement, just do return bool(self.life). It has the same effect, but it's much cleaner.

  • Likewise, in __lt__(), you can just do return (self.priority > obj.priority).

    Note that the convention in those comparison scenarios is to call the other object other, not obj, so I would recommend renaming the variable.

  • There are no docstrings or comments, which is going to make life very difficult for anybody reading the code later. You should explain what the code is supposed to be doing, and why – right now it’s just guesswork.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.