使用线程和类设计的初学者程序

2024-09-28 01:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题似乎太长了,任何人都不能评论。。。我想在一个叫做'劳劳.py'. 下面是一个测试代码,它显示了一个简单的版本。我的目标是只有一个线程,并向其发送信息。我的问题是什么是最好的方法?

文件1(测试.py)在

#!/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)

文件2:劳劳.py在

^{pr2}$

在某些情况下,第二种方法是可行的。e、 g.当我打印一些文本时,只需要线程在它的末尾死掉,但不在进度条的情况下,当它需要更新发送给它。虽然这一切都是可行的,而且我学到了很多东西,但我仍然不知道如何以我想要的方式封装这个类。因为我只需要一个线程,所以我不需要一直实例化类,我只需要这样做一次。在

例如,我的理想方法是只有三个功能:

  • 1要控制文本,请打开进度条等(从一个已分析的字符串内)
  • 2设置进度条总计
  • 3设置进度条迭代

我需要更改类中的两个变量(对于进度条)

  • 总共一个
  • 一个用于迭代

…然后计算出百分比。在

首先我想我应该从线程继承类的东西来启动线程,然后在看了threading.Thread(target=blah,etc)之后,我开始看不到如何使用多个函数,然后我发现我可以把类名放在那里threading.Thread(target=laulau),这样就可以启动一个包含类的线程,但之后我就不知道如何发送该线程了因为我没有像t=laulau()中那样将其分配给“名称”

我的第二个想法是在我的模块中包含类之外的函数,但是因为我需要不止一个函数,所以我也有点困惑,因为我把它添加到劳劳.py公司名称:

def eko (arg):
    t=laulau()  
    t.echo(arg)

def barupate(iteration):
    t.updatebar(a)

def bartotal():
    t.setbartotal(a)

第一个函数生成类的实例,但前面的函数不能更改其中的任何变量。然后我遇到了函数属性,比如this。在

class Foo:
    @webmethod
    def bar(self, arg1, arg2):
         ...

def webmethod(func):
    func.is_webmethod = True
    return func

然后我开始想也许我可以用这个,但从来没有遇到过。在

理想情况下我喜欢这样的东西:

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"

如果你知道python,你可能会觉得我很有趣,但请记住,我是一个完全初学者,非专业人士,每天都在学习,这在很大程度上要感谢这个网站。为任何帮助干杯!在

编辑:应该补充一下,我知道进度条模块和其他各种方法,但我做这个是为了学习和有趣的目的:)


Tags: 方法函数进度条textpyechotimeis
1条回答
网友
1楼 · 发布于 2024-09-28 01:24:56

如果只需要打印进度条,请使用sys模块,如下所示:

import sys
import time

progress = "0"                                 #this is an int meant to represent 0-100 percent as 0-100
old = "0"                                      #this represents the last updates progress, so that this update only adds the difference and not the full progress

def updatebar(progress,old):
    for item in range((progress-old)/2)        #this takes the range of progress but divides it by 2, making the progress bar 50 characters long
    sys.stdout.write("-")                      #to change the character used to fill the progress bar change the "-" to something else
    sys.stdout.flush()

#you may not want to use a while loop here, this just has an example of how to use the update function as it adds one to the progress bar every second
while True:
    progress += 1
    updatebar(progress,old)
    old = progress                             #sets the old progress as the current one, because next iteration of the while loop the previous progress will be this one's current progress.
    time.sleep(1)

相关问题 更多 >

    热门问题