Note that there are some explanatory texts on larger screens.

plurals
  1. PObeginners program designed using threads and classes
    primarykey
    data
    text
    <p>It seems that this question is too long for anyone to comment on... I'm trying to print out some text and a progress bar in a module called 'laulau.py'. Here's a test piece of code that shows a simple version. My goal is to have only <em>one</em> thread, and send information to it. My question is <strong><em>what is the best way to do this ?</em></strong> </p> <p>file1 (test.py)</p> <pre><code>#!/usr/bin/env python from laulau import laulau import time print "FIRST WAY" total=107 t=laulau() t.echo('this is text') t.setbartotal(total) for a in range(1,total): t.updatebar(a) time.sleep(0.01) time.sleep(1) print print "\ndone loop\n" t.stop() time.sleep(1) print "SECOND WAY" with laulau().echo("this is text"): time.sleep(1) print "\nyes this is working\n" time.sleep(2) </code></pre> <p>file2: laulau.py</p> <pre><code>#!/usr/bin/env python # vim:fileencoding=utf8 from __future__ import division import time import string import threading from sys import stdout class laulau(threading.Thread): def __init__(self, arg=None): super(laulau,self).__init__() self._stop = False self.block='█' self.empty='□' self.TEMPLATE = ('%(progress)s%(empty)s %(percent)3s%%') self.progress = None self.percent = 0 self.bar_width=30 self.bartotal=None def run (self): # start thread for text while not self._stop: if self.bartotal is None: print self.arg, stdout.flush() time.sleep(0.3) else: self.progress = int((self.bar_width * self.percent) / 100) self.data = self.TEMPLATE % { 'percent': self.percent, 'progress': self.block * self.progress, 'empty': self.empty * (self.bar_width - self.progress), } stdout.write('\033[%dG'%1 + self.data + self.arg) stdout.flush() time.sleep(0.1) def setbartotal(self,total): # set progress bar total if self.bartotal is None: self.bartotal = total self.updatebar(0) def updatebar (self,num): self.num=num self.percent = self.percentage(self.num) def percentage (self,numagain): return int((numagain/self.bartotal)*100+1) def echo (self,arg="Default"): #self.thread_debug() self.arg=arg self._stop = False self.start() return self def thread_debug(self): print "threading enumerate :%s"%threading.enumerate() print "current thread :%s"%threading.currentThread() print "thread count (including main thread):%s"%threading.activeCount() def stop(self): self._stop = True def stopped(self): return self._stop == True def __enter__(self): print "\nwe have come through the enter function\n" return self def __exit__(self, type, value, traceback): self._stop = True print "\nwe have exited through the exit function\n" return isinstance(value, TypeError) </code></pre> <p>In some cases the second way could work. e.g., when I am printing some text, and just need the thread to die at the end of it, but not in the case of a progress bar when it needs updates sending to it. While this all sort of works, and I learned a lot, I still can't figure out how to encapsulate this class in the way I want. As I only want one thread I don't really need to keep instantiating the class, I just need to do this once.</p> <p>so e.g. my ideal way would be having three functions only:</p> <ul> <li>1 to control text, turn on progress bar etc (from within one parsed string)</li> <li>2 to set the progress bar total</li> <li>3 to set the progress bar iteration</li> </ul> <p>I need to change two variables in the class (for the progress bar)</p> <ul> <li>one for the total</li> <li>one for the iteration</li> </ul> <p>...and it works out percentage from that.</p> <p>First I thought I should start the thread by inheriting the class stuff from threading, then after looking at <code>threading.Thread(target=blah,etc)</code> at first I couldn't see how to use more than one function, then I discovered I could just put the class name in there <code>threading.Thread(target=laulau)</code> and that would start a thread with the class in, but then I was stumped on how to send that thread information seeing as I hadn't assigned it to a 'name' as in <code>t=laulau()</code></p> <p>My second thought was to have functions outside of the class in my module, but because I need more than one function I got a bit confused there too by adding this to the beginning of laulau.py:</p> <pre><code>def eko (arg): t=laulau() t.echo(arg) def barupate(iteration): t.updatebar(a) def bartotal(): t.setbartotal(a) </code></pre> <p>the first function made an instance of the class but the preceding functions could not change any variables within that. and <em>then</em> i came across function attributes such as <a href="https://stackoverflow.com/questions/338101/python-function-attributes-uses-and-abuses">this</a>. </p> <pre><code>class Foo: @webmethod def bar(self, arg1, arg2): ... def webmethod(func): func.is_webmethod = True return func </code></pre> <p>I then started thinking maybe I could use this somehow but have never come across it before. </p> <p>Ideally id like something like this:</p> <pre><code>echo.total(107) echo('[progressbar] this is text') # starts progress bar and instance of thread if not already there... for a in range(1,total): echo.updatebar(a) time.sleep(0.01) time.sleep(1) echo.stop() # bar would stop at the end of iterations but text animations (blinking etc) may still be going at this point... print print "\ndone loop\n" </code></pre> <p>if you know about python you are probably looking at me funny now, but bear in mind that I'm a total beginner non-professional and am learning every day, a lot of it thanks to this site. cheers for any help!</p> <p>edit: should add that I'm aware of the progress bar module and various other recipes, but I'm making this for learning and fun purposes :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload