4

私はPythonの組み込みのshelveモジュールを使用して、いくつかの単純な辞書を管理しています。私が抱えている問題は、使用したいのですwith shelve.open(filename) as f:が、試してみると、DbfilenameShelfには属性がないと主張しています__exit__

したがって、これを行う最も簡単な方法は、別のクラスでラップし、__exit__そのラッパーに関数を追加することだと思います。私はこれを試しました:

class Wrapper(shelve.DbfilenameShelf):
    def __exit__(self):
        self.close()
    def __init__(self, filename, writeback=False):
        shelve.DbfilenameShelf.__init__(self, filename, flag='c', protocol=None, writeback=False)

しかし、そのようにラッパーをインスタンス化しようとするwrapped = Wrapper(filename)と、無効な引数を指定していることがわかります。

要求に応じたエラー:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 5, in __init__
File "C:\Python27\Lib\shelve.py", line 223, in __init__
Shelf.__init__(self, anydbm.open(filename, flag), protocol, writeback)
File "C:\Python27\Lib\anydbm.py", line 85, in open
return mod.open(file, flag, mode)
File "C:\Python27\Lib\dbhash.py", line 18, in open
return bsddb.hashopen(file, flag, mode)
File "C:\Python27\Lib\bsddb\__init__.py", line 364, in hashopen
d.open(file, db.DB_HASH, flags, mode)
DBInvalidArgError: (22, 'Invalid argument')    
4

2 に答える 2

12

サブクラス化しないでください。close()Pythonには、自動的に呼び出すためのツールが付属していますcontextlib.closing

from contextlib import closing
with closing(shelve.open(filename)) as f:
    # your 'with' block here

ブロックの最後にclose()返されるオブジェクトのメソッドを自動的に呼び出します。shelve.open(filename)with

于 2011-09-20T18:17:46.250 に答える
2

間違ったものをサブクラス化していて、メソッドがありません__enter__。あなたはおそらくこれが欲しいでしょう:

class contextShelf(shelve.shelve):
  def __enter__(self):
    return self

  def __exit__(self, exc_type, exc_value, exc_trace):
    self.close()

メソッドを追加しているが、__init__署名を変更したり、追加のステップを追加したりしていないため、を再定義する必要はありません__init__。基本クラス'__init__が自動的に呼び出されます。

于 2011-09-20T18:17:11.920 に答える