5

OS をシミュレートする Python スクリプトをセットアップしました。コマンドプロンプトと仮想ファイルシステムがあります。ディレクトリの階層をサポートするために多次元であるファイルシステムをシミュレートするために shelve モジュールを使用しています。ただし、「cd」コマンドの実装に問題があります。プログラムを最初に起動したときに作成された小さなディレクトリのセットがありますが、ディレクトリに出入りする方法がわかりません。これが私のコードです:

import shelve

fs = shelve.open('filesystem.fs')
directory = 'root'
raw_dir = None
est_dir = None

def install(fs):
    fs['System'] = {}
    fs['Users'] = {}
    username = raw_input('What do you want your username to be? ')
    fs['Users'][username] = {}

try:
    test = fs['runbefore']
    del test
except:
    fs['runbefore'] = None
    install(fs)

def ls(args):
    print 'Contents of directory', directory + ':'
    if raw_dir:
        for i in fs[raw_dir[0]][raw_dir[1]][raw_dir[2]][raw_dir[3]]:
            print i
    else:
        for i in fs:
            print i

def cd(args):
    if len(args.split()) > 1:
        if args.split()[1] == '..':
            if raw_dir[3]:
                raw_dir[3] = 0
            elif raw_dir[2]:
                raw_dir[2] = 0
            elif raw_dir[1]:
                raw_dir[1] = 0
            else:
                print "cd : cannot go above root"

COMMANDS = {'ls' : ls}

while True:
    raw = raw_input('> ')
    cmd = raw.split()[0]
    if cmd in COMMANDS:
        COMMANDS[cmd](raw)

#Use break instead of exit, so you will get to this point.
raw_input('Press the Enter key to shutdown...')

エラーが発生していません。その方法がわからず、「python shelve file system」以外に何を検索すればよいかわかりません。それでは何も役に立ちません。

4

1 に答える 1

9

以下に役立つコードをいくつか提供しますが、最初に、設計に役立つ全体的なアドバイスをいくつか示します。

  • ディレクトリの変更に問題があるのは、現在のディレクトリ変数を間違った方法で表現しているためです。現在のディレクトリは、最上位ディレクトリから現在のディレクトリまでのリストのようなものでなければなりません。それができたら、ディレクトリに基づいて shelve を使用してファイルを保存する方法を選択するだけです (Shelve のすべてのキーは文字列でなければならないことを考慮してください)。

  • ファイルシステムを一連のネストされた辞書として表現することを計画していたようです。これは良い選択です。ただし、 で可変オブジェクトを変更する場合shelveは、a) writeback を True に設定し、b) fs.sync() を呼び出してそれらを設定する必要があることに注意してください。

  • 一連の関数ではなく、クラスでファイルシステム全体を構築する必要があります。共有データを整理しておくのに役立ちます。以下のコードはそれに従っていませんが、考える価値があります。

それで、私は修正しcd、初歩的な mkdir コマンドも書きました。それらを機能させるために重要なことは、上で述べたように、 current_dir を現在のパスを示すcurrent_dictionaryリストにし、そのリストから適切なファイルシステム ディレクトリに移動する簡単な方法 (関数) を用意することです。

それでは、開始するためのコードを次に示します。

import shelve

fs = shelve.open('filesystem.fs', writeback=True)
current_dir = []

def install(fs):
    # create root and others
    username = raw_input('What do you want your username to be? ')

    fs[""] = {"System": {}, "Users": {username: {}}}

def current_dictionary():
    """Return a dictionary representing the files in the current directory"""
    d = fs[""]
    for key in current_dir:
        d = d[key]
    return d

def ls(args):
    print 'Contents of directory', "/" + "/".join(current_dir) + ':'
    for i in current_dictionary():
        print i

def cd(args):
    if len(args) != 1:
        print "Usage: cd <directory>"
        return

    if args[0] == "..":
        if len(current_dir) == 0:
            print "Cannot go above root"
        else:
            current_dir.pop()
    elif args[0] not in current_dictionary():
        print "Directory " + args[0] + " not found"
    else:
        current_dir.append(args[0])


def mkdir(args):
    if len(args) != 1:
        print "Usage: mkdir <directory>"
        return
    # create an empty directory there and sync back to shelve dictionary!
    d = current_dictionary()[args[0]] = {}
    fs.sync()

COMMANDS = {'ls' : ls, 'cd': cd, 'mkdir': mkdir}

install(fs)

while True:
    raw = raw_input('> ')
    cmd = raw.split()[0]
    if cmd in COMMANDS:
        COMMANDS[cmd](raw.split()[1:])

#Use break instead of exit, so you will get to this point.
raw_input('Press the Enter key to shutdown...')

そして、ここにデモンストレーションがあります:

What do you want your username to be? David
> ls
Contents of directory /:
System
Users
> cd Users
> ls
Contents of directory /Users:
David
> cd David
> ls
Contents of directory /Users/David:
> cd ..
> ls
Contents of directory /Users:
David
> cd ..
> mkdir Other
> ls
Contents of directory /:
System
Users
Other
> cd Other
> ls
Contents of directory /Other:
> mkdir WithinOther
> ls
Contents of directory /Other:
WithinOther

これはまだおもちゃにすぎないことに注意することが重要です。まだやらなければならないことがたくさんあります。以下にいくつかの例を示します。

  • 現在、ディレクトリのようなものだけがあり、通常のファイルはありません。

  • mkdirディレクトリがすでに存在するかどうかをチェックせず、空のディレクトリで上書きします。

  • ls特定のディレクトリを引数として (のように) 使用することはできませんls Users。現在のディレクトリのみです。

それでも、これは現在のディレクトリを追跡するための設計の例を示しているはずです。幸運を!

于 2012-01-11T04:31:27.397 に答える