3

私は現在、個人的なプロジェクトのためにプログラムの一部を作成していますが、その 1 つの側面について助けが必要です。

プログラムの仕組みは次のとおりです。

  1. ユーザーが実行する時間を入力します
  2. ユーザーがテキストを入力 - ファイルが変更されます
  3. タイマーが開始されました
  4. オプションユーザーは「パスワード」を入力してタイマーを中断できます
  5. アクションが反転する

これを行う最善の方法を見つけようとしているので、タイマーを除くすべての手順をコーディングしました。理想的には、タイマーにカウントダウンを表示させたいのですが、ユーザーが特定の「パスワード」を入力すると、タイマーが中断され、ステップ番号 5 にスキップします。

これを行う最善の方法は、スレッドを使用することでしょうか? 私は過去にスレッドをあまり扱っていませんでした。ユーザーがそのパスワードを入力したい場合に備えて、ユーザーに制御を戻しながら、タイマーを表示するために何らかの方法が必要です。

ご協力いただきありがとうございます。

コードは次のとおりです。

import time
import urllib
import sys

def restore():
     backup = open(r'...backupfile.txt','r')
     text = open(r'...file.txt', 'w+')
     text.seek(0)

     for line in backup:
         text.write(line)

     backup.close()
     text.close()

text = open(r'...file.txt', 'a+')
backup = open(r'...backupfile.txt','w+')
text.seek(0)

for line in text:
    backup.write(line)

backup.close()

while True:
    url = raw_input('Please enter a URL: ')
    try:
        if url[:7] != 'http://':
            urllib.urlopen('http://' + url) 
        else:
            urllib.urlopen(url)
    except IOError:
        print "Not a real URL"
        continue 

    text.write(url)

    while True:
        choice = raw_input('Would you like to enter another url? (y/n): ')
        try:
            if choice == 'y' or choice == 'n':
                break
        except:
            continue

        if choice == 'y':
            text.seek(2)
            continue

        elif choice == 'n':
            while True:
                choice = raw_input('Would you to restore your file to the original backup (y/n): ')
                try:
                    if choice == 'y' or choice == 'n':
                        break
                except:
                    continue

            if choice == 'y':
                text.close()
                restore()
                sys.exit('Your file has been restored')
            else:
                text.close()
                sys.exit('Your file has been modified')

ご覧のとおり、タイミング部分はまだ追加していません。URL をテキスト ファイルに追加して閉じるだけです。ユーザーが元のファイルを必要とする場合は、reverse() が呼び出されます。

4

1 に答える 1

1

Windows では、msvcrt を使用してキーを要求できます。いくつかのキーを追跡する必要があるため、パスワードを要求するのは実際にはもっと複雑です。このプログラムは F1 で停止します。

import time
import msvcrt

from threading import Thread
import threading

class worker(Thread):

    def __init__(self,maxsec):
        self._maxsec = maxsec
        Thread.__init__(self)
        self._stop = threading.Event()

    def run(self):
        i = 1
        start = time.time()
        while not self.stopped():
            t = time.time()
            dif = t-start
            time.sleep(1) # you want to take this out later (implement progressbar)

            # print something once in a while
            if i%2==0: print '.',

            #check key pressed
            if msvcrt.kbhit():
                if ord(msvcrt.getch()) == 59:
                    self.stop()


            #do stuff

            # timeout
            if dif > self._maxsec:
                break

            i+=1

    def stop(self):
        print 'thread stopped'
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

print 'number of seconds to run '
timeToRun = raw_input()

#input files
#not implemented

#run
w = worker(timeToRun)
w.run()

#reverse actions
于 2012-10-25T16:25:44.793 に答える